From dabd7f4baf39c73b159dea0dbd8efb3ce0ab7fab Mon Sep 17 00:00:00 2001
From: "guilhem.carron" <gcarron@grandlyon.com>
Date: Wed, 28 Jul 2021 10:02:12 +0200
Subject: [PATCH] Fix modal + navigation

---
 src/App.tsx                                   |  1 -
 src/components/DateSelector/DateSelector.tsx  | 48 ++++++++++++++++---
 src/components/DateSelector/dateSelector.scss |  3 ++
 src/components/Modal/AlertModal.tsx           | 19 ++++++++
 src/components/Modal/modal.scss               | 34 +++++++++++++
 5 files changed, 98 insertions(+), 7 deletions(-)
 create mode 100644 src/components/Modal/AlertModal.tsx
 create mode 100644 src/components/Modal/modal.scss

diff --git a/src/App.tsx b/src/App.tsx
index ecf84216..5ccd7ef4 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -12,5 +12,4 @@ function App() {
     </BrowserRouter>
   )
 }
-
 export default App
diff --git a/src/components/DateSelector/DateSelector.tsx b/src/components/DateSelector/DateSelector.tsx
index 0cce0223..6387e242 100644
--- a/src/components/DateSelector/DateSelector.tsx
+++ b/src/components/DateSelector/DateSelector.tsx
@@ -1,7 +1,8 @@
-import React from 'react'
+import React, { useState } from 'react'
 import './dateSelector.scss'
 import leftChevron from '../../assets/icons/left-chevron.svg'
 import rightChevron from '../../assets/icons/right-chevron.svg'
+import Modal from '../Modal/AlertModal'
 
 interface DateSelectorProps {
   date: Date
@@ -14,15 +15,32 @@ const DateSelector: React.FC<DateSelectorProps> = ({
   setDate,
   isEmpty,
 }: DateSelectorProps) => {
+  const [openModal, setopenModal] = useState<boolean>(false)
+  const [isPrev, setisPrev] = useState<boolean>(false)
+  const toggleOpenModal = () => setopenModal((prev) => !prev)
+
   const handleNextMonth = () => {
-    const incremented: Date = new Date(date.setMonth(date.getMonth() + 1))
-    console.log('incremented', incremented)
-    if (isEmpty()) setDate(incremented)
+    setisPrev(false)
+    if (isEmpty() || openModal) {
+      const incremented: Date = new Date(date.setMonth(date.getMonth() + 1))
+      setDate(incremented)
+    } else {
+      toggleOpenModal()
+    }
   }
 
   const handlePrevMonth = () => {
-    const decremented: Date = new Date(date.setMonth(date.getMonth() - 1))
-    if (isEmpty()) setDate(decremented)
+    setisPrev(true)
+    if (isEmpty() || openModal) {
+      const decremented: Date = new Date(date.setMonth(date.getMonth() - 1))
+      setDate(decremented)
+    } else {
+      toggleOpenModal()
+    }
+  }
+  const handleConfirmAlert = () => {
+    isPrev ? handlePrevMonth() : handleNextMonth()
+    setopenModal(false)
   }
   return (
     <div className="date-selector">
@@ -41,6 +59,24 @@ const DateSelector: React.FC<DateSelectorProps> = ({
         onClick={handleNextMonth}
         className="arrow"
       />
+      {openModal && (
+        <Modal>
+          <>
+            <div className="modal-text">
+              Attention, vous n'avez pas enregistré vos modifications. Celles-ci
+              seront annulées en changeant de mois. Voulez-vous continuer ?
+            </div>
+            <div className="buttons">
+              <button className="btnCancel" onClick={() => setopenModal(false)}>
+                Annuler
+              </button>
+              <button className="btnValid" onClick={handleConfirmAlert}>
+                Continuer
+              </button>
+            </div>
+          </>
+        </Modal>
+      )}
     </div>
   )
 }
diff --git a/src/components/DateSelector/dateSelector.scss b/src/components/DateSelector/dateSelector.scss
index bbc98d03..4b4539cf 100644
--- a/src/components/DateSelector/dateSelector.scss
+++ b/src/components/DateSelector/dateSelector.scss
@@ -32,3 +32,6 @@
     }
   }
 }
+.modal-text {
+  text-align: center;
+}
diff --git a/src/components/Modal/AlertModal.tsx b/src/components/Modal/AlertModal.tsx
new file mode 100644
index 00000000..fde74896
--- /dev/null
+++ b/src/components/Modal/AlertModal.tsx
@@ -0,0 +1,19 @@
+import React, { ReactChild } from 'react'
+import { createPortal } from 'react-dom'
+import './modal.scss'
+interface ModalProps {
+  children: ReactChild
+}
+
+const Modal: React.FC<ModalProps> = ({ children }: ModalProps) => {
+  return createPortal(
+    <div className="modal-bg">
+      <div className="modal-container">
+        <div className="modal-content">{children}</div>
+      </div>
+    </div>,
+    document.body
+  )
+}
+
+export default Modal
diff --git a/src/components/Modal/modal.scss b/src/components/Modal/modal.scss
new file mode 100644
index 00000000..11500dab
--- /dev/null
+++ b/src/components/Modal/modal.scss
@@ -0,0 +1,34 @@
+@import '../../styles/config/colors';
+
+.modal-bg {
+  background-color: rgba(0, 0, 0, 0.7);
+  display: flex;
+  height: 100vh;
+  left: 0;
+  position: fixed;
+  top: 0;
+  width: 100%;
+  z-index: 999;
+
+  .modal-container {
+    align-items: center;
+    display: flex;
+    justify-content: center;
+    overflow-y: auto;
+    width: 100%;
+  }
+  .modal-content {
+    background: $grey-dark;
+    border-radius: 10px;
+    box-shadow: 0px 4px 8px rgba(43, 45, 92, 0.25),
+      0px 2px 4px rgba(43, 45, 92, 0.25);
+    padding: 1rem;
+    position: relative;
+    max-width: 350px;
+    width: 100%;
+  }
+  .buttons {
+    display: flex;
+    justify-content: center;
+  }
+}
-- 
GitLab