Skip to content
Snippets Groups Projects
Editing.tsx 4.85 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useCallback, useContext, useEffect, useState } from 'react'
    
    import DateSelector from '../DateSelector/DateSelector'
    import './editing.scss'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { MonthlyNewsService } from '../../services/monthlyNews.service'
    
    import { UserContext, UserContextProps } from '../../hooks/userContext'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IMonthlyNews } from '../../models/monthlyNews.model'
    
    import Poll from '../Poll/Poll'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import MonthlyNews from '../MonthlyNews/MonthlyNews'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IPoll } from '../../models/poll.model'
    
    import Loader from '../Loader/Loader'
    
    
    const Editing: React.FC = () => {
    
      const [date, setDate] = useState<Date>(new Date())
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
      const [header, setHeader] = useState<string>('')
      const [quote, setQuote] = useState<string>('')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [question, setQuestion] = useState<string>('')
      const [link, setLink] = useState<string>('')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [isTouched, setIsTouched] = useState<boolean>(false)
    
      const [refreshData, setRefreshData] = useState(false)
      const [isLoading, setisLoading] = useState<boolean>(false)
    
      const { user }: Partial<UserContextProps> = useContext(UserContext)
    
      const monthlyNewsService = new MonthlyNewsService()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const handleSaveMonthlyNews = async (): Promise<void> => {
    
        if (user) {
          const monthlyNewsService = new MonthlyNewsService()
          await monthlyNewsService.createMonthlyReport(
            date,
            header,
            quote,
            user.xsrftoken
          )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          setIsTouched(false)
        }
      }
      const handleSavePoll = async (): Promise<void> => {
        if (user) {
          await monthlyNewsService.createPoll(date, question, link, user.xsrftoken)
          setIsTouched(false)
    
      const handleCancel = useCallback(() => {
        setRefreshData(true)
    
      const handleDeleteMonthlyNews = async (): Promise<void> => {
        if (user) {
          await monthlyNewsService.deleteMonthlyNews(
            date.getFullYear(),
            date.getMonth(),
            user.xsrftoken
          )
          setRefreshData(true)
        }
      }
      const handleDeletePoll = async (): Promise<void> => {
        if (user) {
          await monthlyNewsService.deletePoll(
            date.getFullYear(),
            date.getMonth(),
            user.xsrftoken
          )
          setRefreshData(true)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
      const isEmpty = (): boolean => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        if ((quote !== '' || header !== '') && isTouched) {
    
          return false
        } else return true
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      const handleEditorChange = (
        value: string,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        type: 'header' | 'quote' | 'question' | 'link'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): void => {
        setIsTouched(true)
        if (type === 'header') {
          setHeader(value)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
        if (type === 'quote') {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          setQuote(value)
        }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        if (type === 'question') {
          setQuestion(value)
        }
        if (type === 'link') {
          setLink(value)
        }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const resetFields = useCallback(() => {
        setQuote('')
        setHeader('')
        setLink('')
        setQuestion('')
      }, [])
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      useEffect(() => {
    
        let subscribed = true
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        resetFields()
    
        setisLoading(true)
    
        async function getCurrentMonthlyNews() {
    
          if (user) {
            const monthlyNewsService = new MonthlyNewsService()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            const montlhyNews: IMonthlyNews | null =
    
              await monthlyNewsService.getSingleMonthlyReport(
                date.getFullYear(),
                date.getMonth(),
                user.xsrftoken
              )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            const poll: IPoll | null = await monthlyNewsService.getSinglePoll(
              date.getFullYear(),
              date.getMonth(),
              user.xsrftoken
            )
            if (montlhyNews) {
              setHeader(montlhyNews.header)
              setQuote(montlhyNews.quote)
    
              setIsTouched(false)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            }
            if (poll) {
              setLink(poll.link)
              setQuestion(poll.question)
              setIsTouched(false)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
        if (subscribed) {
          getCurrentMonthlyNews()
        }
        return () => {
          subscribed = false
    
          setRefreshData(false)
    
      }, [date, user, refreshData])
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    
      return (
        <>
    
          <div className="header">
    
            <p className="title pagetitle">
    
              Édition des informations et de la citation du mois
            </p>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            <DateSelector date={date} setDate={setDate} isEmpty={isEmpty} />
    
          {isLoading ? (
            <Loader />
          ) : (
            <div className="content">
              <MonthlyNews
                header={header}
                quote={quote}
                onSave={handleSaveMonthlyNews}
                onCancel={handleCancel}
                handleChange={handleEditorChange}
                onDelete={handleDeleteMonthlyNews}
              />
              <hr />
              <Poll
                question={question}
                link={link}
                handleChange={handleEditorChange}
                onSave={handleSavePoll}
                onCancel={handleCancel}
                onDelete={handleDeletePoll}
              />
            </div>
          )}