|
| 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 | +import { SyntheticEvent, useEffect, useState } from 'react'; |
| 8 | +import { useDispatch, useSelector } from 'react-redux'; |
| 9 | +import Box from '@mui/material/Box'; |
| 10 | +import Divider from '@mui/material/Divider'; |
| 11 | +import { useForm } from 'react-hook-form'; |
| 12 | +import { useIntl } from 'react-intl'; |
| 13 | +import { List, ListItem } from '@mui/material'; |
| 14 | +import { |
| 15 | + CustomMuiDialog, |
| 16 | + FieldConstants, |
| 17 | + NetworkModificationMetadata, |
| 18 | + NO_SELECTION_FOR_COPY, |
| 19 | + unscrollableDialogStyles, |
| 20 | + useModificationLabelComputer, |
| 21 | + useSnackMessage, |
| 22 | + yupConfig as yup, |
| 23 | +} from '@gridsuite/commons-ui'; |
| 24 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 25 | +import { AppState } from '../../../../redux/types'; |
| 26 | +import { useParameterState } from '../../use-parameters-dialog'; |
| 27 | +import { PARAM_LANGUAGE } from '../../../../utils/config-params'; |
| 28 | +import { fetchCompositeModificationContent, saveCompositeModification } from '../../../../utils/rest-api'; |
| 29 | +import CompositeModificationForm from './composite-modification-form'; |
| 30 | +import { setSelectionForCopy } from '../../../../redux/actions'; |
| 31 | + |
| 32 | +const schema = yup.object().shape({ |
| 33 | + [FieldConstants.NAME]: yup.string().trim().required('nameEmpty'), |
| 34 | +}); |
| 35 | + |
| 36 | +const emptyFormData = (name?: string) => ({ |
| 37 | + [FieldConstants.NAME]: name, |
| 38 | +}); |
| 39 | + |
| 40 | +interface FormData { |
| 41 | + [FieldConstants.NAME]: string; |
| 42 | +} |
| 43 | + |
| 44 | +interface CompositeModificationDialogProps { |
| 45 | + compositeModificationId: string; |
| 46 | + open: boolean; |
| 47 | + onClose: (event?: SyntheticEvent) => void; |
| 48 | + titleId: string; |
| 49 | + name: string; |
| 50 | + broadcastChannel: BroadcastChannel; |
| 51 | +} |
| 52 | + |
| 53 | +export default function CompositeModificationDialog({ |
| 54 | + compositeModificationId, |
| 55 | + open, |
| 56 | + onClose, |
| 57 | + titleId, |
| 58 | + name, |
| 59 | + broadcastChannel, |
| 60 | +}: Readonly<CompositeModificationDialogProps>) { |
| 61 | + const intl = useIntl(); |
| 62 | + const [languageLocal] = useParameterState(PARAM_LANGUAGE); |
| 63 | + const [isFetching, setIsFetching] = useState(!!compositeModificationId); |
| 64 | + const { snackError } = useSnackMessage(); |
| 65 | + const selectionForCopy = useSelector((state: AppState) => state.selectionForCopy); |
| 66 | + const [modifications, setModifications] = useState<NetworkModificationMetadata[]>([]); |
| 67 | + const dispatch = useDispatch(); |
| 68 | + |
| 69 | + const methods = useForm<FormData>({ |
| 70 | + defaultValues: emptyFormData(name), |
| 71 | + resolver: yupResolver(schema), |
| 72 | + }); |
| 73 | + |
| 74 | + const { computeLabel } = useModificationLabelComputer(); |
| 75 | + const getModificationLabel = (modif: NetworkModificationMetadata) => { |
| 76 | + if (!modif) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + const labelData = { |
| 80 | + ...modif, |
| 81 | + ...computeLabel(modif), |
| 82 | + }; |
| 83 | + return intl.formatMessage({ id: `network_modifications.${modif.type}` }, labelData); |
| 84 | + }; |
| 85 | + |
| 86 | + const generateNetworkModificationsList = () => { |
| 87 | + return ( |
| 88 | + <List sx={unscrollableDialogStyles.scrollableContent}> |
| 89 | + {modifications && |
| 90 | + modifications.map((modification: NetworkModificationMetadata) => ( |
| 91 | + <Box key={modification.uuid}> |
| 92 | + <ListItem> |
| 93 | + <Box>{getModificationLabel(modification)}</Box> |
| 94 | + </ListItem> |
| 95 | + <Divider component="li" /> |
| 96 | + </Box> |
| 97 | + ))} |
| 98 | + </List> |
| 99 | + ); |
| 100 | + }; |
| 101 | + |
| 102 | + useEffect(() => { |
| 103 | + setIsFetching(true); |
| 104 | + fetchCompositeModificationContent(compositeModificationId) |
| 105 | + .then((response) => { |
| 106 | + if (response) { |
| 107 | + setModifications(response); |
| 108 | + } |
| 109 | + }) |
| 110 | + .catch((error) => { |
| 111 | + snackError({ |
| 112 | + messageTxt: error.message, |
| 113 | + headerId: 'retrieveCompositeModificationError', |
| 114 | + }); |
| 115 | + }) |
| 116 | + .finally(() => setIsFetching(false)); |
| 117 | + }, [compositeModificationId, name, snackError]); |
| 118 | + |
| 119 | + const closeAndClear = (event?: SyntheticEvent) => { |
| 120 | + onClose(event); |
| 121 | + }; |
| 122 | + |
| 123 | + const onSubmit = (formData: FormData) => { |
| 124 | + saveCompositeModification(compositeModificationId, formData[FieldConstants.NAME]) |
| 125 | + .then(() => { |
| 126 | + if (selectionForCopy.sourceItemUuid === compositeModificationId) { |
| 127 | + dispatch(setSelectionForCopy(NO_SELECTION_FOR_COPY)); |
| 128 | + broadcastChannel.postMessage({ |
| 129 | + NO_SELECTION_FOR_COPY, |
| 130 | + }); |
| 131 | + } |
| 132 | + closeAndClear(); |
| 133 | + }) |
| 134 | + .catch((errorMessage) => { |
| 135 | + snackError({ |
| 136 | + messageTxt: errorMessage, |
| 137 | + headerId: 'compositeModificationEditingError', |
| 138 | + headerValues: { name }, |
| 139 | + }); |
| 140 | + }); |
| 141 | + }; |
| 142 | + |
| 143 | + return ( |
| 144 | + <CustomMuiDialog |
| 145 | + open={open} |
| 146 | + onClose={closeAndClear} |
| 147 | + titleId={titleId} |
| 148 | + onSave={onSubmit} |
| 149 | + removeOptional |
| 150 | + isDataFetching={isFetching} |
| 151 | + language={languageLocal} |
| 152 | + formSchema={schema} |
| 153 | + formMethods={methods} |
| 154 | + unscrollableFullHeight |
| 155 | + > |
| 156 | + {!isFetching && ( |
| 157 | + <Box sx={unscrollableDialogStyles.unscrollableContainer}> |
| 158 | + <CompositeModificationForm /> |
| 159 | + {generateNetworkModificationsList()} |
| 160 | + </Box> |
| 161 | + )} |
| 162 | + </CustomMuiDialog> |
| 163 | + ); |
| 164 | +} |
0 commit comments