Skip to content

Commit

Permalink
feat: adding the discard modal
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosSoares committed Dec 7, 2022
1 parent b86d456 commit 4e0a1a5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/components/CardEditable/components/UnsavedChanges/index.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Modal
title={<Title level={2}>{t('overlay.unsaved.title')}</Title>}
open
onOk={onConfirm}
onCancel={onClose}
cancelText={t('overlay.unsaved.cancel')}
centered
okText={t('overlay.unsaved.confirm')}
>
{t('overlay.unsaved.text')}
</Modal>
);
};
26 changes: 24 additions & 2 deletions src/components/CardEditable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,9 +25,11 @@ interface CardEditableProps {
formProp?: FormInstance;
onAddMode?: boolean;
tooltip?: React.ReactChild;
allowUnsavedChanges?: boolean;
}

export const CardEditable = ({
allowUnsavedChanges,
className,
isLoading,
initialValues,
Expand All @@ -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,
Expand All @@ -55,6 +61,7 @@ export const CardEditable = ({
(formData) => {
onSave(formData);
setEditing(false);
setHasChanges(false);
},
[onSave],
);
Expand Down Expand Up @@ -85,6 +92,7 @@ export const CardEditable = ({
labelWrap
layout="vertical"
form={form}
onValuesChange={() => setHasChanges(true)}
onFinish={onFormSubmit}
disabled={!editing}
initialValues={initialValues}
Expand All @@ -99,13 +107,27 @@ export const CardEditable = ({
<Button
item={cancelEditButton}
buttonHandle={() => {
form.resetFields();
setEditing(false);
if (allowUnsavedChanges && hasChanges) {
setShowUnsavedChangesModal(true);
} else {
form.resetFields();
setEditing(false);
}
}}
/>
<Button item={saveEditButton} buttonHandle={() => form.submit()} />
</div>
)}
{allowUnsavedChanges && showUnsavedChangesModal && (
<UnsavedChangesModal
onConfirm={() => setShowUnsavedChangesModal(false)}
onClose={() => {
form.resetFields();
setEditing(false);
setShowUnsavedChangesModal(false);
}}
/>
)}
</Box>
);
};
4 changes: 4 additions & 0 deletions src/locales/de/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@
"twoFactorAuth.switch.type.EMAIL": "E-Mail",
"twoFactorAuth.switch.type.APP": "App",
"overlay.step.headline.prefix": ". Schritt | ",
"overlay.unsaved.title": "Änderungen verwerfen?",
"overlay.unsaved.text": "Es sind ungespeicherte Änderungen vorhanden. Wollen Sie diese verwerfen und Ihre Bearbeitung ohne zu Speichern beenden?",
"overlay.unsaved.cancel": "Abbrechen",
"overlay.unsaved.confirm": "Verwerfen",
"topic": "Thema",
"topics.title": "Themen",
"topics.title.text": "Erstellen und bearbeiten Sie Themen",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const LegalText = ({ fieldName, titleKey, subTitle, placeHolderKey, showC
return (
<>
<CardEditable
allowUnsavedChanges
isLoading={isLoading}
initialValues={{ ...data }}
titleKey={titleKey}
Expand Down

0 comments on commit 4e0a1a5

Please sign in to comment.