Skip to content
Snippets Groups Projects
monthlyNews.service.ts 3.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    import axios from 'axios'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IMonthlyNews } from '../models/monthlyNews.model'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IPoll } from '../models/poll.model'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { toast } from 'react-toastify'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    export class MonthlyNewsService {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Creates a quotation and header for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       * @param date
       * @param header
       * @param quote
       */
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      public createMonthlyReport = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        header: string,
    
        quote: string,
        token: string
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<void> => {
        try {
    
            `/api/admin/monthlyNews`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth(),
              year: date.getFullYear(),
              header: header,
              quote: quote,
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            }
          )
    
          toast.success('Monthly news succesfully saved !')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          toast.error('Failed to create monthly news')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          console.log(e)
        }
      }
    
    
      /**
       * Gets a quotation and header for selected month
       */
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      public getSingleMonthlyReport = async (
    
        year: number,
        month: number,
        token: string
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<IMonthlyNews | null> => {
    
          const { data } = await axios.get(
    
            `/api/admin/monthlyNews/${year}/${month}`,
    
              headers: {
                'XSRF-TOKEN': token,
              },
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return data as IMonthlyNews
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.log('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
      /**
       * Gets a poll with question and link for selected month
       */
      public getSinglePoll = async (
        year: number,
        month: number,
        token: string
      ): Promise<IPoll | null> => {
        try {
          const { data } = await axios.get(
    
            `/api/admin/poll/${year}/${month}`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
          return data as IPoll
        } catch (e) {
          console.log('error', e)
          return null
        }
      }
    
      /**
       * Creates a poll with question and link for selected month
       * @param date
       * @param question
       * @param link
       */
      public createPoll = async (
        date: Date,
        question: string,
        link: string,
        token: string
      ): Promise<void> => {
        try {
    
            `/api/admin/poll`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
              month: date.getMonth(),
              year: date.getFullYear(),
              link: link,
              question: question,
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          toast.success('Poll successfully saved !')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          toast.error('Failed to create poll')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          console.log(e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
       * Deletes a poll for selected month
       * @param month
       * @param year
       * @param token
       */
      public deletePoll = async (
        year: number,
        month: number,
        token: string
      ): Promise<void> => {
        try {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          await axios.delete(
    
            `/api/admin/poll/${year}/${month}`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          toast.success('Poll succesfully deleted !')
        } catch (e) {
          toast.error('Failed to delete poll')
          console.log(e)
        }
      }
      /**
       * Deletes a Monthly News for selected month
       * @param year
       * @param month
       * @param token
       */
      public deleteMonthlyNews = async (
        year: number,
        month: number,
        token: string
      ): Promise<void> => {
        try {
          await axios.delete(
    
            `/api/admin/monthlyNews/${year}/${month}`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
          toast.success('Monthly news succesfully deleted !')
        } catch (e) {
          toast.error('Failed to delete monthly news')
          console.log(e)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    }