From 73a16644d88ba80da4407c441e2650dacf01bd1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Mon, 11 Oct 2021 15:06:57 +0200
Subject: [PATCH] fix: changed month range from [0;11] to [1;12]

---
 src/components/Editing/Editing.tsx | 33 ++--------
 src/services/newsletter.service.ts | 98 ++++++++++++++++--------------
 2 files changed, 58 insertions(+), 73 deletions(-)

diff --git a/src/components/Editing/Editing.tsx b/src/components/Editing/Editing.tsx
index 90158488..df2e87c5 100644
--- a/src/components/Editing/Editing.tsx
+++ b/src/components/Editing/Editing.tsx
@@ -67,31 +67,19 @@ const Editing: React.FC = () => {
 
   const handleDeleteMonthlyInfo = async (): Promise<void> => {
     if (user) {
-      await newsletterService.deleteMonthlyInfo(
-        date.getFullYear(),
-        date.getMonth(),
-        user.xsrftoken
-      )
+      await newsletterService.deleteMonthlyInfo(date, user.xsrftoken)
       setRefreshData(true)
     }
   }
   const handleDeleteMonthlyNews = async (): Promise<void> => {
     if (user) {
-      await newsletterService.deleteMonthlyNews(
-        date.getFullYear(),
-        date.getMonth(),
-        user.xsrftoken
-      )
+      await newsletterService.deleteMonthlyNews(date, user.xsrftoken)
       setRefreshData(true)
     }
   }
   const handleDeletePoll = async (): Promise<void> => {
     if (user) {
-      await newsletterService.deletePoll(
-        date.getFullYear(),
-        date.getMonth(),
-        user.xsrftoken
-      )
+      await newsletterService.deletePoll(date, user.xsrftoken)
       setRefreshData(true)
     }
   }
@@ -167,20 +155,11 @@ const Editing: React.FC = () => {
       if (user) {
         const newsletterService = new NewsletterService()
         const montlhyInfo: IMonthlyInfo | null =
-          await newsletterService.getSingleMonthlyInfo(
-            date.getFullYear(),
-            date.getMonth(),
-            user.xsrftoken
-          )
+          await newsletterService.getSingleMonthlyInfo(date, user.xsrftoken)
         const montlhyNews: IMonthlyNews | null =
-          await newsletterService.getSingleMonthlyNews(
-            date.getFullYear(),
-            date.getMonth(),
-            user.xsrftoken
-          )
+          await newsletterService.getSingleMonthlyNews(date, user.xsrftoken)
         const poll: IPoll | null = await newsletterService.getSinglePoll(
-          date.getFullYear(),
-          date.getMonth(),
+          date,
           user.xsrftoken
         )
         if (montlhyInfo) {
diff --git a/src/services/newsletter.service.ts b/src/services/newsletter.service.ts
index d0f4515a..6d6a3159 100644
--- a/src/services/newsletter.service.ts
+++ b/src/services/newsletter.service.ts
@@ -19,7 +19,7 @@ export class NewsletterService {
       await axios.put(
         `/api/admin/monthlyInfo`,
         {
-          month: date.getMonth(),
+          month: date.getMonth() + 1,
           year: date.getFullYear(),
           info: info,
           image: image,
@@ -39,15 +39,16 @@ export class NewsletterService {
 
   /**
    * Gets the information for selected month
+   * @param date
+   * @param token
    */
   public getSingleMonthlyInfo = async (
-    year: number,
-    month: number,
+    date: Date,
     token: string
   ): Promise<IMonthlyInfo | null> => {
     try {
       const { data } = await axios.get(
-        `/api/admin/monthlyInfo/${year}/${month}`,
+        `/api/admin/monthlyInfo/${date.getFullYear()}/${date.getMonth() + 1}`,
         {
           headers: {
             'XSRF-TOKEN': token,
@@ -63,21 +64,22 @@ export class NewsletterService {
 
   /**
    * Deletes a Monthly Info for selected month
-   * @param year
-   * @param month
+   * @param date
    * @param token
    */
   public deleteMonthlyInfo = async (
-    year: number,
-    month: number,
+    date: Date,
     token: string
   ): Promise<void> => {
     try {
-      await axios.delete(`/api/admin/monthlyInfo/${year}/${month}`, {
-        headers: {
-          'XSRF-TOKEN': token,
-        },
-      })
+      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')
@@ -101,7 +103,7 @@ export class NewsletterService {
       await axios.put(
         `/api/admin/monthlyNews`,
         {
-          month: date.getMonth(),
+          month: date.getMonth() + 1,
           year: date.getFullYear(),
           title: title,
           content: content,
@@ -121,15 +123,16 @@ export class NewsletterService {
 
   /**
    * Gets a news title and content for selected month
+   * @param date
+   * @param token
    */
   public getSingleMonthlyNews = async (
-    year: number,
-    month: number,
+    date: Date,
     token: string
   ): Promise<IMonthlyNews | null> => {
     try {
       const { data } = await axios.get(
-        `/api/admin/monthlyNews/${year}/${month}`,
+        `/api/admin/monthlyNews/${date.getFullYear()}/${date.getMonth() + 1}`,
         {
           headers: {
             'XSRF-TOKEN': token,
@@ -145,21 +148,22 @@ export class NewsletterService {
 
   /**
    * Deletes a Monthly News for selected month
-   * @param year
-   * @param month
+   * @param date
    * @param token
    */
   public deleteMonthlyNews = async (
-    year: number,
-    month: number,
+    date: Date,
     token: string
   ): Promise<void> => {
     try {
-      await axios.delete(`/api/admin/monthlyNews/${year}/${month}`, {
-        headers: {
-          'XSRF-TOKEN': token,
-        },
-      })
+      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')
@@ -183,7 +187,7 @@ export class NewsletterService {
       await axios.put(
         `/api/admin/poll`,
         {
-          month: date.getMonth(),
+          month: date.getMonth() + 1,
           year: date.getFullYear(),
           link: link,
           question: question,
@@ -203,18 +207,22 @@ export class NewsletterService {
 
   /**
    * Gets a poll with question and link for selected month
+   * @param date
+   * @param token
    */
   public getSinglePoll = async (
-    year: number,
-    month: number,
+    date: Date,
     token: string
   ): Promise<IPoll | null> => {
     try {
-      const { data } = await axios.get(`/api/admin/poll/${year}/${month}`, {
-        headers: {
-          'XSRF-TOKEN': token,
-        },
-      })
+      const { data } = await axios.get(
+        `/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
+        {
+          headers: {
+            'XSRF-TOKEN': token,
+          },
+        }
+      )
       return data as IPoll
     } catch (e) {
       console.error('error', e)
@@ -224,21 +232,19 @@ export class NewsletterService {
 
   /**
    * Deletes a poll for selected month
-   * @param month
-   * @param year
+   * @param date
    * @param token
    */
-  public deletePoll = async (
-    year: number,
-    month: number,
-    token: string
-  ): Promise<void> => {
+  public deletePoll = async (date: Date, token: string): Promise<void> => {
     try {
-      await axios.delete(`/api/admin/poll/${year}/${month}`, {
-        headers: {
-          'XSRF-TOKEN': token,
-        },
-      })
+      await axios.delete(
+        `/api/admin/poll/${date.getFullYear()}/${date.getMonth() + 1}`,
+        {
+          headers: {
+            'XSRF-TOKEN': token,
+          },
+        }
+      )
       toast.success('Poll succesfully deleted !')
     } catch (e) {
       toast.error('Failed to delete poll')
-- 
GitLab