-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Visualize composite modifications (#538)
Signed-off-by: Mathieu DEHARBE <[email protected]>
- Loading branch information
1 parent
fe600d3
commit ce0e79a
Showing
8 changed files
with
259 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...nts/dialogs/network-modification/composite-modification/composite-modification-dialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
import { SyntheticEvent, useEffect, useState } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import Box from '@mui/material/Box'; | ||
import Divider from '@mui/material/Divider'; | ||
import { useForm } from 'react-hook-form'; | ||
import { useIntl } from 'react-intl'; | ||
import { List, ListItem } from '@mui/material'; | ||
import { | ||
CustomMuiDialog, | ||
FieldConstants, | ||
NetworkModificationMetadata, | ||
NO_SELECTION_FOR_COPY, | ||
unscrollableDialogStyles, | ||
useModificationLabelComputer, | ||
useSnackMessage, | ||
yupConfig as yup, | ||
} from '@gridsuite/commons-ui'; | ||
import { yupResolver } from '@hookform/resolvers/yup'; | ||
import { AppState } from '../../../../redux/types'; | ||
import { useParameterState } from '../../use-parameters-dialog'; | ||
import { PARAM_LANGUAGE } from '../../../../utils/config-params'; | ||
import { fetchCompositeModificationContent, saveCompositeModification } from '../../../../utils/rest-api'; | ||
import CompositeModificationForm from './composite-modification-form'; | ||
import { setSelectionForCopy } from '../../../../redux/actions'; | ||
|
||
const schema = yup.object().shape({ | ||
[FieldConstants.NAME]: yup.string().trim().required('nameEmpty'), | ||
}); | ||
|
||
const emptyFormData = (name?: string) => ({ | ||
[FieldConstants.NAME]: name, | ||
}); | ||
|
||
interface FormData { | ||
[FieldConstants.NAME]: string; | ||
} | ||
|
||
interface CompositeModificationDialogProps { | ||
compositeModificationId: string; | ||
open: boolean; | ||
onClose: (event?: SyntheticEvent) => void; | ||
titleId: string; | ||
name: string; | ||
broadcastChannel: BroadcastChannel; | ||
} | ||
|
||
export default function CompositeModificationDialog({ | ||
compositeModificationId, | ||
open, | ||
onClose, | ||
titleId, | ||
name, | ||
broadcastChannel, | ||
}: Readonly<CompositeModificationDialogProps>) { | ||
const intl = useIntl(); | ||
const [languageLocal] = useParameterState(PARAM_LANGUAGE); | ||
const [isFetching, setIsFetching] = useState(!!compositeModificationId); | ||
const { snackError } = useSnackMessage(); | ||
const selectionForCopy = useSelector((state: AppState) => state.selectionForCopy); | ||
const [modifications, setModifications] = useState<NetworkModificationMetadata[]>([]); | ||
const dispatch = useDispatch(); | ||
|
||
const methods = useForm<FormData>({ | ||
defaultValues: emptyFormData(name), | ||
resolver: yupResolver(schema), | ||
}); | ||
|
||
const { computeLabel } = useModificationLabelComputer(); | ||
const getModificationLabel = (modif: NetworkModificationMetadata) => { | ||
if (!modif) { | ||
return null; | ||
} | ||
const labelData = { | ||
...modif, | ||
...computeLabel(modif), | ||
}; | ||
return intl.formatMessage({ id: `network_modifications.${modif.type}` }, labelData); | ||
}; | ||
|
||
const generateNetworkModificationsList = () => { | ||
return ( | ||
<List sx={unscrollableDialogStyles.scrollableContent}> | ||
{modifications && | ||
modifications.map((modification: NetworkModificationMetadata) => ( | ||
<Box key={modification.uuid}> | ||
<ListItem> | ||
<Box>{getModificationLabel(modification)}</Box> | ||
</ListItem> | ||
<Divider component="li" /> | ||
</Box> | ||
))} | ||
</List> | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
setIsFetching(true); | ||
fetchCompositeModificationContent(compositeModificationId) | ||
.then((response) => { | ||
if (response) { | ||
setModifications(response); | ||
} | ||
}) | ||
.catch((error) => { | ||
snackError({ | ||
messageTxt: error.message, | ||
headerId: 'retrieveCompositeModificationError', | ||
}); | ||
}) | ||
.finally(() => setIsFetching(false)); | ||
}, [compositeModificationId, name, snackError]); | ||
|
||
const closeAndClear = (event?: SyntheticEvent) => { | ||
onClose(event); | ||
}; | ||
|
||
const onSubmit = (formData: FormData) => { | ||
saveCompositeModification(compositeModificationId, formData[FieldConstants.NAME]) | ||
.then(() => { | ||
if (selectionForCopy.sourceItemUuid === compositeModificationId) { | ||
dispatch(setSelectionForCopy(NO_SELECTION_FOR_COPY)); | ||
broadcastChannel.postMessage({ | ||
NO_SELECTION_FOR_COPY, | ||
}); | ||
} | ||
closeAndClear(); | ||
}) | ||
.catch((errorMessage) => { | ||
snackError({ | ||
messageTxt: errorMessage, | ||
headerId: 'compositeModificationEditingError', | ||
headerValues: { name }, | ||
}); | ||
}); | ||
}; | ||
|
||
return ( | ||
<CustomMuiDialog | ||
open={open} | ||
onClose={closeAndClear} | ||
titleId={titleId} | ||
onSave={onSubmit} | ||
removeOptional | ||
isDataFetching={isFetching} | ||
language={languageLocal} | ||
formSchema={schema} | ||
formMethods={methods} | ||
unscrollableFullHeight | ||
> | ||
{!isFetching && ( | ||
<Box sx={unscrollableDialogStyles.unscrollableContainer}> | ||
<CompositeModificationForm /> | ||
{generateNetworkModificationsList()} | ||
</Box> | ||
)} | ||
</CustomMuiDialog> | ||
); | ||
} |
27 changes: 27 additions & 0 deletions
27
...nents/dialogs/network-modification/composite-modification/composite-modification-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { UniqueNameInput, ElementType, FieldConstants } from '@gridsuite/commons-ui'; | ||
import { elementExists } from 'utils/rest-api'; | ||
import { useSelector } from 'react-redux'; | ||
import { AppState } from 'redux/types'; | ||
import Box from '@mui/material/Box'; | ||
|
||
export default function CompositeModificationForm() { | ||
const activeDirectory = useSelector((state: AppState) => state.activeDirectory); | ||
return ( | ||
<Box> | ||
<UniqueNameInput | ||
name={FieldConstants.NAME} | ||
label="nameProperty" | ||
elementType={ElementType.MODIFICATION} | ||
activeDirectory={activeDirectory} | ||
elementExists={elementExists} | ||
/> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters