From a1964920eb6d0dd233c9b7e82012545e32ba71ad Mon Sep 17 00:00:00 2001 From: Theo Macron Date: Wed, 24 Jan 2024 17:34:30 +0100 Subject: [PATCH] wip --- .../BootstrapSNCF/ModalSNCF/ModalProvider.tsx | 22 +- .../components/AddOrEditScenarioModal.tsx | 309 ++++++++++-------- 2 files changed, 173 insertions(+), 158 deletions(-) diff --git a/front/src/common/BootstrapSNCF/ModalSNCF/ModalProvider.tsx b/front/src/common/BootstrapSNCF/ModalSNCF/ModalProvider.tsx index 87cbdc87102..a7ae85f5644 100644 --- a/front/src/common/BootstrapSNCF/ModalSNCF/ModalProvider.tsx +++ b/front/src/common/BootstrapSNCF/ModalSNCF/ModalProvider.tsx @@ -12,7 +12,6 @@ import React, { } from 'react'; import { Outlet, useLocation } from 'react-router-dom'; import cx from 'classnames'; -import ConfirmModal from './ConfirmModal'; /** * Type of the modal context @@ -48,11 +47,7 @@ export const ModalContext = createContext(initialModalContext); export const ModalSNCF: FC = () => { const modalRef = useRef(null); const { isOpen, content, closeModal, size, className } = useContext(ModalContext); - const [isConfirmModalOpen, setConfirmModalOpen] = useState(false); - /** - * Register click outside event to close the modal. - */ useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if ( @@ -62,12 +57,7 @@ export const ModalSNCF: FC = () => { !(event.target as HTMLElement).classList.contains('no-close-modal') && !modalRef.current.contains(event.target as HTMLElement) ) { - const isXlModal = modalRef.current.classList.contains('modal-xl'); - if (isXlModal) { - setConfirmModalOpen(true); - } else if (!isXlModal) { - closeModal(); - } + closeModal(); } }; @@ -77,15 +67,6 @@ export const ModalSNCF: FC = () => { }; }, [isOpen, closeModal]); - const handleConfirm = () => { - setConfirmModalOpen(false); - closeModal(); - }; - - const handleCancel = () => { - setConfirmModalOpen(false); - }; - if (!content) { return null; } @@ -97,7 +78,6 @@ export const ModalSNCF: FC = () => { role="dialog" aria-hidden="true" > - {isConfirmModalOpen && }
(null); + + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if ( + event.target && + isOpen && + modalRef.current && + !(event.target as HTMLElement).classList.contains('no-close-modal') && + !modalRef.current.contains(event.target as HTMLElement) + ) { + setClickedOutside(true); + } + }; + + document.body.addEventListener('mousedown', handleClickOutside); + return function cleanup() { + document.body.removeEventListener('mousedown', handleClickOutside); + }; + }, [isOpen, clickedOutside]); + + const resetClickedOutside = () => { + setClickedOutside(false); + }; + return ( -
- -

- {editionMode ? t('scenarioModificationTitle') : t('scenarioCreationTitle')} -

-
- -
-
-
- - - - - {t('scenarioName')} +
+ {clickedOutside ? ( + + ) : ( + <> + +

+ {editionMode ? t('scenarioModificationTitle') : t('scenarioCreationTitle')} +

+
+ +
+
+
+ + + + + {t('scenarioName')} +
+ } + value={currentScenario.name || ''} + onChange={(e) => + setCurrentScenario({ ...currentScenario, name: e.target.value }) + } + isInvalid={displayErrors && !currentScenario.name} + errorMsg={ + displayErrors && !currentScenario.name ? t('scenarioNameMissing') : undefined + } + /> +
+
+ + + + + {t('scenarioDescription')} +
+ } + value={currentScenario.description || ''} + onChange={(e) => + setCurrentScenario({ ...currentScenario, description: e.target.value }) + } + /> +
+ {!editionMode && electricalProfilOptions.length > 1 && ( +
+ + + + + {t('electricalProfileSet')} +
+ } + value={selectedValue} + options={electricalProfilOptions.map((e) => ({ + id: `${e.key}`, + label: e.value, + }))} + onChange={(e) => + setCurrentScenario({ + ...currentScenario, + electrical_profile_set_id: e?.id ? +e.id : undefined, + }) + } + />
- } - value={currentScenario.name || ''} - onChange={(e) => setCurrentScenario({ ...currentScenario, name: e.target.value })} - isInvalid={displayErrors && !currentScenario.name} - errorMsg={ - displayErrors && !currentScenario.name ? t('scenarioNameMissing') : undefined - } - /> -
-
- - - - - {t('scenarioDescription')} -
- } - value={currentScenario.description || ''} - onChange={(e) => - setCurrentScenario({ ...currentScenario, description: e.target.value }) - } - /> -
- {!editionMode && electricalProfilOptions.length > 1 && ( -
- - - - - {t('electricalProfileSet')} -
- } - value={selectedValue} - options={electricalProfilOptions.map((e) => ({ - id: `${e.key}`, - label: e.value, - }))} - onChange={(e) => - setCurrentScenario({ - ...currentScenario, - electrical_profile_set_id: e?.id ? +e.id : undefined, - }) - } + )} +
- )} - -
- {!editionMode && ( -
-
- -
+ {!editionMode && ( +
+
+ +
+
+ )} +
+ + +
+ {editionMode && ( + + )} + + {editionMode ? ( + + ) : ( + + )}
- )} -
- - -
- {editionMode && ( - - )} - - {editionMode ? ( - - ) : ( - - )} -
-
+ + + )} ); }