From 4e0a1a57f569c4907ec5e2162fa118474de5a4a0 Mon Sep 17 00:00:00 2001 From: Carlos Soares Date: Wed, 7 Dec 2022 15:17:58 +0000 Subject: [PATCH] feat: adding the discard modal --- .../components/UnsavedChanges/index.tsx | 26 +++++++++++++++++++ src/components/CardEditable/index.tsx | 26 +++++++++++++++++-- src/locales/de/translation.json | 4 +++ .../components/LegalText/index.tsx | 1 + 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/components/CardEditable/components/UnsavedChanges/index.tsx diff --git a/src/components/CardEditable/components/UnsavedChanges/index.tsx b/src/components/CardEditable/components/UnsavedChanges/index.tsx new file mode 100644 index 00000000..e94b807a --- /dev/null +++ b/src/components/CardEditable/components/UnsavedChanges/index.tsx @@ -0,0 +1,26 @@ +import { Modal } from 'antd'; +import { useTranslation } from 'react-i18next'; +import Title from 'antd/lib/typography/Title'; + +export interface UnsavedChangesProps { + onConfirm: () => void; + onClose: () => void; +} + +export const UnsavedChangesModal = ({ onConfirm, onClose }: UnsavedChangesProps) => { + const { t } = useTranslation(); + + return ( + {t('overlay.unsaved.title')}} + open + onOk={onConfirm} + onCancel={onClose} + cancelText={t('overlay.unsaved.cancel')} + centered + okText={t('overlay.unsaved.confirm')} + > + {t('overlay.unsaved.text')} + + ); +}; diff --git a/src/components/CardEditable/index.tsx b/src/components/CardEditable/index.tsx index 1f0e57af..aea410e4 100644 --- a/src/components/CardEditable/index.tsx +++ b/src/components/CardEditable/index.tsx @@ -9,6 +9,7 @@ import Pencil from '../CustomIcons/Pencil'; import { Tooltip } from '../tooltip/Tooltip'; import styles from './styles.module.scss'; import { ReactComponent as InfoIcon } from '../../resources/img/svg/i.svg'; +import { UnsavedChangesModal } from './components/UnsavedChanges'; interface CardEditableProps { className?: string; @@ -24,9 +25,11 @@ interface CardEditableProps { formProp?: FormInstance; onAddMode?: boolean; tooltip?: React.ReactChild; + allowUnsavedChanges?: boolean; } export const CardEditable = ({ + allowUnsavedChanges, className, isLoading, initialValues, @@ -41,6 +44,9 @@ export const CardEditable = ({ const [form] = Form.useForm(formProp); const { t } = useTranslation(); const [editing, setEditing] = useState(onAddMode); + const [hasChanges, setHasChanges] = useState(false); + const [showUnsavedChangesModal, setShowUnsavedChangesModal] = useState(false); + const cancelEditButton: ButtonItem = { label: t('agency.edit.general.general_information.cancel'), type: BUTTON_TYPES.LINK, @@ -55,6 +61,7 @@ export const CardEditable = ({ (formData) => { onSave(formData); setEditing(false); + setHasChanges(false); }, [onSave], ); @@ -85,6 +92,7 @@ export const CardEditable = ({ labelWrap layout="vertical" form={form} + onValuesChange={() => setHasChanges(true)} onFinish={onFormSubmit} disabled={!editing} initialValues={initialValues} @@ -99,13 +107,27 @@ export const CardEditable = ({