Skip to content
Snippets Groups Projects
Editing.tsx 7.65 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useCallback, useContext, useEffect, useState } from 'react'
    
    import DateSelector from '../DateSelector/DateSelector'
    
    import { NewsletterService } from '../../services/newsletter.service'
    
    import { UserContext, UserContextProps } from '../../hooks/userContext'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IMonthlyNews } from '../../models/monthlyNews.model'
    
    import { IMonthlyInfo } from '../../models/monthlyInfo.model'
    import { IPoll } from '../../models/poll.model'
    
    import Poll from '../Poll/Poll'
    
    import MonthlyInfo from '../MonthlyInfo/MonthlyInfo'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import MonthlyNews from '../MonthlyNews/MonthlyNews'
    
    import Loader from '../Loader/Loader'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import Modal from '../Modal/Modal'
    
    export type ContentItems = 'monthlyInfo' | 'monthlyNews' | 'poll' | ''
    
    
    const Editing: React.FC = () => {
    
      const [date, setDate] = useState<Date>(new Date())
    
      const [info, setInfo] = useState<string>('')
      const [title, setTitle] = useState<string>('')
      const [content, setContent] = 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)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const [warningModal, setwarningModal] = useState<boolean>(false)
      const [toDelete, settoDelete] = useState<ContentItems>('')
    
      const { user }: Partial<UserContextProps> = useContext(UserContext)
    
      const newsletterService = new NewsletterService()
    
      const handleSaveMonthlyInfo = async (): Promise<void> => {
        if (user) {
          const newsletterService = new NewsletterService()
          await newsletterService.saveMonthlyInfo(date, info, user.xsrftoken)
          setIsTouched(false)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const handleSaveMonthlyNews = async (): Promise<void> => {
    
        if (user) {
    
          const newsletterService = new NewsletterService()
          await newsletterService.saveMonthlyNews(
    
            user.xsrftoken
          )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          setIsTouched(false)
        }
      }
      const handleSavePoll = async (): Promise<void> => {
        if (user) {
    
          await newsletterService.savePoll(date, question, link, user.xsrftoken)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          setIsTouched(false)
    
      const handleCancel = useCallback(() => {
        setRefreshData(true)
    
      const handleDeleteMonthlyInfo = async (): Promise<void> => {
        if (user) {
          await newsletterService.deleteMonthlyInfo(
            date.getFullYear(),
            date.getMonth(),
            user.xsrftoken
          )
          setRefreshData(true)
        }
      }
    
      const handleDeleteMonthlyNews = async (): Promise<void> => {
        if (user) {
    
          await newsletterService.deleteMonthlyNews(
    
            date.getFullYear(),
            date.getMonth(),
            user.xsrftoken
          )
          setRefreshData(true)
        }
      }
      const handleDeletePoll = async (): Promise<void> => {
        if (user) {
    
          await newsletterService.deletePoll(
    
            date.getFullYear(),
            date.getMonth(),
            user.xsrftoken
          )
          setRefreshData(true)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      const handleOpenDeleteModal = (target: ContentItems) => {
        settoDelete(target)
        setwarningModal(true)
      }
      const handleConfirmAlert = () => {
    
        if (toDelete === 'monthlyInfo') {
          handleDeleteMonthlyInfo()
        }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        if (toDelete === 'monthlyNews') {
          handleDeleteMonthlyNews()
        }
        if (toDelete === 'poll') {
          handleDeletePoll()
        }
        setwarningModal(false)
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    
      const isEmpty = (): boolean => {
    
          (info !== '' ||
            title !== '' ||
            content !== '' ||
            question !== '' ||
            link !== '') &&
    
          return false
        } else return true
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      const handleEditorChange = (
        value: string,
    
        type: 'info' | 'title' | 'content' | 'question' | 'link'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): void => {
        setIsTouched(true)
    
        if (type === 'info') {
          setInfo(value)
        }
        if (type === 'title') {
          setTitle(value)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
        if (type === 'content') {
          setContent(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(() => {
    
        setInfo('')
        setTitle('')
        setContent('')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        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 newsletterService = new NewsletterService()
            const montlhyInfo: IMonthlyInfo | null =
              await newsletterService.getSingleMonthlyInfo(
                date.getFullYear(),
                date.getMonth(),
                user.xsrftoken
              )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            const montlhyNews: IMonthlyNews | null =
    
              await newsletterService.getSingleMonthlyNews(
    
                date.getFullYear(),
                date.getMonth(),
                user.xsrftoken
              )
    
            const poll: IPoll | null = await newsletterService.getSinglePoll(
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              date.getFullYear(),
              date.getMonth(),
              user.xsrftoken
            )
    
            if (montlhyInfo) {
              setInfo(montlhyInfo.info)
              setIsTouched(false)
            }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            if (montlhyNews) {
    
              setTitle(montlhyNews.title)
              setContent(montlhyNews.content)
    
              setIsTouched(false)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            }
            if (poll) {
              setLink(poll.link)
              setQuestion(poll.question)
              setIsTouched(false)
    
          setisLoading(false)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
        if (subscribed) {
          getCurrentMonthlyNews()
        }
        return () => {
          subscribed = false
    
          setRefreshData(false)
    
      }, [date, user, refreshData, resetFields])
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    
      return (
        <>
    
          <div className="header">
    
            <p className="title pagetitle">Édition de la newsletter</p>
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            <DateSelector date={date} setDate={setDate} isEmpty={isEmpty} />
    
          {isLoading ? (
            <Loader />
          ) : (
            <div className="content">
    
              <MonthlyInfo
                info={info}
                onSave={handleSaveMonthlyInfo}
                onCancel={handleCancel}
                handleChange={handleEditorChange}
                onDelete={handleOpenDeleteModal}
              />
              <hr />
    
                title={title}
                content={content}
    
                onSave={handleSaveMonthlyNews}
                onCancel={handleCancel}
                handleChange={handleEditorChange}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                onDelete={handleOpenDeleteModal}
    
              />
              <hr />
              <Poll
                question={question}
                link={link}
                onSave={handleSavePoll}
                onCancel={handleCancel}
    
                handleChange={handleEditorChange}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                onDelete={handleOpenDeleteModal}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          {warningModal && (
            <Modal>
              <>
                <div className="modal-text">
                  Etes-vous sûr de vouloir supprimer{' '}
    
                  {toDelete === 'monthlyInfo'
                    ? 'cette info mensuelle '
                    : toDelete === 'monthlyNews'
                    ? 'cette news mensuelle'
                    : 'ce sondage'}{' '}
                  ?
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                </div>
                <div className="buttons">
                  <button
                    className="btnCancel"
                    onClick={() => setwarningModal(false)}
                  >
                    Annuler
                  </button>
                  <button className="btnValid" onClick={handleConfirmAlert}>
                    Continuer
                  </button>
                </div>
              </>
            </Modal>
          )}