Skip to content
Snippets Groups Projects
Editing.tsx 3.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • import React, { useCallback, useContext, useEffect, useState } from 'react'
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
    import { Editor } from '@tinymce/tinymce-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'
    
    import { MonthlyNews } from '../../models/monthlyNews.model'
    
    import EcolyoLink from '../EcolyoLink/EcolyoLink'
    import Poll from '../Poll/Poll'
    
    
    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 [isTouched, setIsTouched] = useState<boolean>(false)
    
      const { user }: Partial<UserContextProps> = useContext(UserContext)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
      const handleSave = async (): Promise<void> => {
    
        if (user) {
          const monthlyNewsService = new MonthlyNewsService()
          await monthlyNewsService.createMonthlyReport(
            date,
            header,
            quote,
            user.xsrftoken
          )
        }
    
      const handleCancel = useCallback(() => {
        setQuote('')
        setHeader('')
      }, [])
    
      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,
        type: 'header' | 'quote'
      ): void => {
        setIsTouched(true)
        if (type === 'header') {
          setHeader(value)
        } else {
          setQuote(value)
        }
      }
    
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      useEffect(() => {
    
        let subscribed = true
    
        async function getCurrentMonthlyNews() {
    
          if (user) {
            const monthlyNewsService = new MonthlyNewsService()
            const news: MonthlyNews =
              await monthlyNewsService.getSingleMonthlyReport(
                date.getFullYear(),
                date.getMonth(),
                user.xsrftoken
              )
            if (news) {
              setHeader(news.header)
              setQuote(news.quote)
              setIsTouched(false)
            }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
    
        if (subscribed) {
          getCurrentMonthlyNews()
        }
        return () => {
          subscribed = false
        }
      }, [date, user])
    
    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>
          </div>
          <div className="content">
    
            <DateSelector date={date} setDate={setDate} isEmpty={isEmpty} />
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            <div className="subtitle">
              <p className="title">Informations du mois</p>
            </div>
    
            <div>
              <Editor
                initialValue=""
                init={{
                  menubar: false,
                  toolbar:
                    'undo redo | bold italic underline | alignleft aligncenter alignright | code',
                }}
                value={header}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                onEditorChange={(newHeader, editor) =>
                  handleEditorChange(newHeader, 'header')
                }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              <div className="subtitle">
                <p className="title">Citation du mois</p>
              </div>
    
              <Editor
                initialValue=""
                init={{
                  menubar: false,
                  toolbar:
                    'undo redo | bold italic underline | alignleft aligncenter alignright | code',
                }}
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                onEditorChange={(newQuote, editor) =>
                  handleEditorChange(newQuote, 'quote')
                }
    
              <button className="btnCancel" onClick={handleCancel}>
                Annuler
              </button>
    
              <button className="btnValid" onClick={handleSave}>
    
            <EcolyoLink />
            <Poll />
    
    Rémi PAILHAREY's avatar
    Rémi PAILHAREY committed
          </div>
        </>
      )