Skip to content
Snippets Groups Projects
newsletter.service.ts 6.04 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'
    
    import { IMonthlyInfo } from '../models/monthlyInfo.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'
    
    export class NewsletterService {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Creates a monthlyInfo for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       * @param date
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public saveMonthlyInfo = async (
    
        token: string
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<void> => {
        try {
    
            `/api/admin/monthlyInfo`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth() + 1,
    
              year: date.getFullYear(),
    
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            }
          )
    
          toast.success('Monthly info succesfully saved !')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          toast.error('Failed to create monthly info')
          console.error(e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
      }
    
       * Gets the information for selected month
    
       * @param date
       * @param token
    
      public getSingleMonthlyInfo = async (
    
        token: string
    
      ): Promise<IMonthlyInfo | null> => {
    
          const { data } = await axios.get(
    
            `/api/admin/monthlyInfo/${date.getFullYear()}/${date.getMonth() + 1}`,
    
              headers: {
                'XSRF-TOKEN': token,
              },
    
          return data as IMonthlyInfo
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.error('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
      /**
    
       * Deletes a Monthly Info for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public deleteMonthlyInfo = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        token: string
    
      ): Promise<void> => {
        try {
    
          await axios.delete(
            `/api/admin/monthlyInfo/${date.getFullYear()}/${date.getMonth() + 1}`,
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          toast.success('Monthly info succesfully deleted !')
        } catch (e) {
          toast.error('Failed to delete monthly info')
          console.error(e)
        }
      }
    
      /**
       * Creates a monthlyNews for selected month
       * @param date
       * @param title
       * @param content
       */
      public saveMonthlyNews = async (
        date: Date,
        title: string,
        content: string,
        token: string
      ): Promise<void> => {
        try {
          await axios.put(
            `/api/admin/monthlyNews`,
            {
    
              month: date.getMonth() + 1,
    
              year: date.getFullYear(),
              title: title,
              content: content,
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
          toast.success('Monthly news succesfully saved !')
        } catch (e) {
          toast.error('Failed to create monthly news')
          console.error(e)
        }
      }
    
      /**
       * Gets a news title and content for selected month
    
       * @param date
       * @param token
    
       */
      public getSingleMonthlyNews = async (
    
        token: string
      ): Promise<IMonthlyNews | null> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
          const { data } = await axios.get(
    
            `/api/admin/monthlyNews/${date.getFullYear()}/${date.getMonth() + 1}`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          return data as IMonthlyNews
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.error('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
    
      /**
       * Deletes a Monthly News for selected month
    
       * @param token
       */
      public deleteMonthlyNews = async (
    
        token: string
      ): Promise<void> => {
        try {
    
          await axios.delete(
            `/api/admin/monthlyNews/${date.getFullYear()}/${date.getMonth() + 1}`,
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          toast.success('Monthly news succesfully deleted !')
        } catch (e) {
          toast.error('Failed to delete monthly news')
          console.error(e)
        }
      }
    
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
       * Creates a poll with question and link for selected month
       * @param date
       * @param question
       * @param link
       */
    
      public savePoll = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        date: Date,
        question: string,
        link: string,
        token: string
      ): Promise<void> => {
        try {
    
            `/api/admin/poll`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth() + 1,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              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
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Gets a poll with question and link for selected month
    
       * @param date
       * @param token
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       */
    
      public getSinglePoll = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        token: string
    
      ): Promise<IPoll | null> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
    
          const { data } = await axios.get(
            `/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          return data as IPoll
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.error('error', e)
          return null
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Deletes a poll for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       * @param token
       */
    
      public deletePoll = async (date: Date, token: string): Promise<void> => {
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        try {
    
          await axios.delete(
            `/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
    
          toast.success('Poll succesfully deleted !')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          toast.error('Failed to delete poll')
          console.error(e)
    
    
      /**
       * Gets the ecogesture images URLs
       */
      public getEcogestureImages = async (token: string): Promise<string[]> => {
        try {
          const { data: imageNames } = await axios.get(`/api/admin/imageNames`, {
            headers: {
              'XSRF-TOKEN': token,
            },
          })
          if (imageNames && imageNames !== null) {
            const imageURLs = imageNames.map((image: string) => {
    
              return `/assets/ecogesture/${image}`
    
            })
            return imageURLs
          }
          return []
        } catch (e) {
          console.error('error', e)
          return []
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    }