Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use CustomMuiDialog and get initial name from directory-server #285

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 0 additions & 93 deletions src/components/dialogs/copy-to-script-dialog.js

This file was deleted.

150 changes: 150 additions & 0 deletions src/components/dialogs/copy-to-script-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2023, 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 React, { useCallback, useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import yup from 'components/utils/yup-config';
import { NAME } from 'components/utils/field-constants';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import CustomMuiDialog from './commons/custom-mui-dialog/custom-mui-dialog';
import { CircularProgress, Grid } from '@mui/material';
import { UniqueNameInput } from './commons/unique-name-input';
import { ElementType } from 'utils/elementType';
import { getNameCandidate } from 'utils/rest-api';

const schema = yup.object().shape({
[NAME]: yup.string().trim().required('nameEmpty'),
});

const emptyFormData = {
[NAME]: '',
};

interface CopyToScriptDialogProps {
id: string;
open: boolean;
onClose: (...args: any[]) => void;
onValidate: (...args: any[]) => void;
currentName: string;
title: string;
directoryUuid: string;
elementType: ElementType;
handleError: (...args: any[]) => void;
}

interface IFormData {
flomillot marked this conversation as resolved.
Show resolved Hide resolved
[NAME]: string;
}

/**
* Dialog to copy a filters contingency list to a script contingency list or a filter to a script
* @param id id of list or filter to edit
* @param open Is the dialog open ?
* @param onClose Event to close the dialog
* @param onValidate Function to call to perform copy
* @param currentName Name before renaming
* @param title Title of the dialog
* @param directoryUuid Directory uuid of the list or filter to copy
* @param elementType Type of the element to copy
* @param handleError Function to call to handle error
*/
const CopyToScriptDialog: React.FunctionComponent<CopyToScriptDialogProps> = ({
id,
open,
onClose,
onValidate,
currentName,
title,
directoryUuid,
elementType,
handleError,
}) => {
const [loading, setLoading] = useState(false);
const intl = useIntl();
const methods = useForm<IFormData>({
defaultValues: emptyFormData,
resolver: yupResolver(schema),
});

const {
formState: { errors },
setValue,
} = methods;

const nameError = errors[NAME];
const isValidating = errors.root?.isValidating;

const onSubmit = (data: IFormData) => {
onValidate(id, data[NAME]);
};

const handleClose = () => {
onClose();
};

const handleGenerateNameError = useCallback(() => {
return handleError(
intl.formatMessage(
{ id: 'generateCopyScriptNameError' },
{
itemName: currentName,
}
)
);
}, [currentName, handleError, intl]);

useEffect(() => {
setLoading(true);
getNameCandidate(directoryUuid, currentName, elementType)
.then((newName) => {
let generatedName: string = newName || '';
setValue(NAME, generatedName, { shouldDirty: true });
})
.catch(() => {
handleGenerateNameError();
})
.finally(() => {
setLoading(false);
});
}, [
handleGenerateNameError,
setValue,
currentName,
elementType,
directoryUuid,
]);

return (
<CustomMuiDialog
open={open}
onClose={handleClose}
onSave={onSubmit}
formSchema={schema}
formMethods={methods}
titleId={title}
removeOptional={true}
disabledSave={!!nameError || !!isValidating}
>
<Grid container spacing={2}>
<Grid item xs={12}>
{loading ? (
<CircularProgress />
) : (
<UniqueNameInput
name={NAME}
label={'nameProperty'}
elementType={ElementType.CONTINGENCY_LIST}
autoFocus
/>
)}
</Grid>
</Grid>
</CustomMuiDialog>
);
};

export default CopyToScriptDialog;
16 changes: 10 additions & 6 deletions src/components/menus/content-contextual-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ const ContentContextualMenu = (props) => {
id={activeElement ? activeElement.elementUuid : ''}
open={true}
onClose={handleCloseDialog}
onClick={(id, newName) =>
onValidate={(id, newName) =>
newScriptFromFiltersContingencyListCB(
id,
newName,
Expand All @@ -568,9 +568,10 @@ const ContentContextualMenu = (props) => {
currentName={
activeElement ? activeElement.elementName : ''
}
title={intl.formatMessage({
id: 'copyToScriptList',
})}
title={'copyToScriptList'}
directoryUuid={selectedDirectory?.elementUuid}
elementType={activeElement?.type}
handleError={handleLastError}
/>
);
case DialogsId.REPLACE_FILTER_BY_SCRIPT:
Expand All @@ -594,7 +595,7 @@ const ContentContextualMenu = (props) => {
id={activeElement ? activeElement.elementUuid : ''}
open={true}
onClose={handleCloseDialog}
onClick={(id, newName) =>
onValidate={(id, newName) =>
newScriptFromFilterCB(
id,
newName,
Expand All @@ -604,7 +605,10 @@ const ContentContextualMenu = (props) => {
currentName={
activeElement ? activeElement.elementName : ''
}
title={intl.formatMessage({ id: 'copyToScriptList' })}
title={'copyToScriptList'}
directoryUuid={selectedDirectory?.elementUuid}
elementType={activeElement?.type}
handleError={handleLastError}
/>
);
case DialogsId.ADD_NEW_STUDY_FROM_CASE:
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"deleteElementsFailure": "Problem encountered while deleting {stn, plural, one {an element} other {elements}}. Not deleted : {problematic}",
"moveElementsFailure": "Problem encountered while moving {stn, plural, one {an element} other {elements}}. Not moved : {problematic}",
"duplicateElementFailure": "An error occured while duplicating the element {itemName} : {errorMessage}",
"generateCopyScriptNameError" : "An error occured while generating the copy script name of the element {itemName}",
"caseName": "case name",
"uploadCase": "Upload case",
"uploadErrorMsg": "Please upload the case file",
Expand Down
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"deleteElementsFailure": "Problème à la destruction {stn, plural, one {d'un élément} other {d'éléments}}. Pas {pbn, plural, one {détruit} other {détruits}} : {problematic}",
"moveElementsFailure": "Problème au déplacement {stn, plural, one {d'un élément} other {d'éléments}}. Pas {pbn, plural, one {déplacé} other {déplacés}} : {problematic}",
"duplicateElementFailure": "Une erreur est survenue lors de la duplication de l'élément {itemName} : {errorMessage}",
"generateCopyScriptNameError" : "Une erreur s'est produite lors de la génération du nom du script de copie de l'élément {itemName}",
"caseName": "nom de la situation",
"uploadCase": "télécharger la situation",
"uploadErrorMsg": "Veuillez télécharger la situation",
Expand Down
Loading