Skip to content

Commit 0d89d0e

Browse files
authored
CustomMuiDialog can have a confirmation dialog (#548)
use case : print a warning in a confirmation pop-up while importing data into open-source env like "demo". Signed-off-by: David BRAQUART <[email protected]>
1 parent 9b14036 commit 0d89d0e

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

src/components/dialogs/custom-mui-dialog.tsx

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import React from 'react';
8+
import React, { useCallback, useState } from 'react';
99
import { FieldErrors, UseFormReturn } from 'react-hook-form';
1010
import { FormattedMessage } from 'react-intl';
1111
import { Dialog, DialogActions, DialogContent, DialogTitle, Grid, LinearProgress } from '@mui/material';
1212
import * as yup from 'yup';
1313
import SubmitButton from '../inputs/react-hook-form/utils/submit-button';
1414
import CancelButton from '../inputs/react-hook-form/utils/cancel-button';
1515
import CustomFormProvider, { MergedFormContextProps } from '../inputs/react-hook-form/provider/custom-form-provider';
16+
import PopupConfirmationDialog from './popup-confirmation-dialog';
1617

1718
interface ICustomMuiDialog {
1819
open: boolean;
@@ -28,6 +29,7 @@ interface ICustomMuiDialog {
2829
children: React.ReactNode;
2930
isDataFetching?: boolean;
3031
language?: string;
32+
confirmationMessageKey?: string;
3133
}
3234

3335
const styles = {
@@ -54,13 +56,19 @@ function CustomMuiDialog({
5456
onCancel,
5557
children,
5658
language,
59+
confirmationMessageKey,
5760
}: ICustomMuiDialog) {
61+
const [openConfirmationPopup, setOpenConfirmationPopup] = useState(false);
62+
const [validatedData, setValidatedData] = useState(undefined);
5863
const { handleSubmit } = formMethods;
5964

60-
const handleCancel = (event: React.MouseEvent) => {
61-
onCancel?.();
62-
onClose(event);
63-
};
65+
const handleCancel = useCallback(
66+
(event: React.MouseEvent) => {
67+
onCancel?.();
68+
onClose(event);
69+
},
70+
[onCancel, onClose]
71+
);
6472

6573
const handleClose = (event: React.MouseEvent, reason?: string) => {
6674
if (reason === 'backdropClick' && onCancel) {
@@ -69,10 +77,30 @@ function CustomMuiDialog({
6977
onClose(event);
7078
};
7179

72-
const handleValidate = (data: any) => {
73-
onSave(data);
74-
onClose(data);
75-
};
80+
const validate = useCallback(
81+
(data: any) => {
82+
onSave(data);
83+
onClose(data);
84+
},
85+
[onClose, onSave]
86+
);
87+
88+
const handleValidate = useCallback(
89+
(data: any) => {
90+
if (confirmationMessageKey) {
91+
setValidatedData(data);
92+
setOpenConfirmationPopup(true);
93+
} else {
94+
validate(data);
95+
}
96+
},
97+
[confirmationMessageKey, validate]
98+
);
99+
100+
const handlePopupConfirmation = useCallback(() => {
101+
setOpenConfirmationPopup(false);
102+
validate(validatedData);
103+
}, [validate, validatedData]);
76104

77105
const handleValidationError = (errors: FieldErrors) => {
78106
onValidationError?.(errors);
@@ -102,6 +130,14 @@ function CustomMuiDialog({
102130
/>
103131
</DialogActions>
104132
</Dialog>
133+
{confirmationMessageKey && (
134+
<PopupConfirmationDialog
135+
message={confirmationMessageKey}
136+
openConfirmationPopup={openConfirmationPopup}
137+
setOpenConfirmationPopup={setOpenConfirmationPopup}
138+
handlePopupConfirmation={handlePopupConfirmation}
139+
/>
140+
)}
105141
</CustomFormProvider>
106142
);
107143
}

src/components/dialogs/popup-confirmation-dialog.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import CancelButton from '../inputs/react-hook-form/utils/cancel-button';
1616

1717
export interface PopupConfirmationDialogProps {
1818
message: string;
19-
validateButtonLabel: string;
19+
validateButtonLabel?: string;
2020
openConfirmationPopup: boolean;
2121
setOpenConfirmationPopup: (value: boolean) => void;
2222
handlePopupConfirmation: () => void;
@@ -33,9 +33,7 @@ function PopupConfirmationDialog({
3333
<Dialog open={openConfirmationPopup} aria-labelledby="dialog-title-change-equipment-type">
3434
<DialogTitle id="dialog-title-change-equipment-type">Confirmation</DialogTitle>
3535
<DialogContent>
36-
<DialogContentText>
37-
<FormattedMessage id={message} />
38-
</DialogContentText>
36+
<DialogContentText>{message && <FormattedMessage id={message} />}</DialogContentText>
3937
</DialogContent>
4038
<DialogActions>
4139
<CancelButton onClick={() => setOpenConfirmationPopup(false)} />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
import { useEffect, useState } from 'react';
9+
import { fetchEnv } from '../services';
10+
11+
const useConfidentialityWarning = () => {
12+
const [confidentialityWarningKey, setConfidentialityWarningKey] = useState<string>();
13+
14+
useEffect(() => {
15+
fetchEnv().then((res) => {
16+
setConfidentialityWarningKey(res?.confidentialityMessageKey);
17+
});
18+
}, []);
19+
20+
return confidentialityWarningKey;
21+
};
22+
23+
export default useConfidentialityWarning;

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export { default as useIntlRef } from './hooks/useIntlRef';
163163
export { useSnackMessage } from './hooks/useSnackMessage';
164164
export { default as useDebounce } from './hooks/useDebounce';
165165
export { default as usePrevious } from './hooks/usePrevious';
166+
export { default as useConfidentialityWarning } from './hooks/useConfidentialityWarning';
166167
export { default as SelectClearable } from './components/inputs/select-clearable';
167168
export { default as useCustomFormContext } from './components/inputs/react-hook-form/provider/use-custom-form-context';
168169
export { default as CustomFormProvider } from './components/inputs/react-hook-form/provider/custom-form-provider';

src/services/apps-metadata.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type Url = string | URL;
1313
export type Env = {
1414
appsMetadataServerUrl?: Url;
1515
mapBoxToken?: string;
16+
confidentialityMessageKey?: string;
1617
// https://github.com/gridsuite/deployment/blob/main/docker-compose/env.json
1718
// https://github.com/gridsuite/deployment/blob/main/k8s/live/azure-dev/env.json
1819
// https://github.com/gridsuite/deployment/blob/main/k8s/live/azure-integ/env.json

0 commit comments

Comments
 (0)