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 1 commit
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.

151 changes: 151 additions & 0 deletions src/components/dialogs/copy-to-script-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/**
* 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;
onClick: (...args: any[]) => void;
currentName: string;
title: string;
directoryUuid: string;
elementType: ElementType;
handleError: (...args: any[]) => void;
}

/**
* 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 onClick 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.FC<CopyToScriptDialogProps> = ({
achour94 marked this conversation as resolved.
Show resolved Hide resolved
id,
open,
onClose,
onClick,
achour94 marked this conversation as resolved.
Show resolved Hide resolved
currentName,
title,
directoryUuid,
elementType,
handleError,
}) => {
const [loading, setLoading] = useState(false);
const intl = useIntl();
const methods = useForm({
defaultValues: emptyFormData,
resolver: yupResolver(schema),
});

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

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

const onSubmit = (data: { [NAME]: string; [key: string]: any }) => {
achour94 marked this conversation as resolved.
Show resolved Hide resolved
onClick(id, data[NAME]);
};

const handleClose = () => {
reset(emptyFormData);
achour94 marked this conversation as resolved.
Show resolved Hide resolved
onClose();
};

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

useEffect(() => {
setLoading(true);
getNameCandidate(directoryUuid, currentName, elementType)
.then((newName) => {
setValue(NAME, newName, { shouldDirty: true });
})
.catch((error) => {
handleGenerateNameError(error.message);
})
.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;
7 changes: 4 additions & 3 deletions src/components/menus/content-contextual-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 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} : {errorMessage}",
achour94 marked this conversation as resolved.
Show resolved Hide resolved
"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} : {errorMessage}",
"caseName": "nom de la situation",
"uploadCase": "télécharger la situation",
"uploadErrorMsg": "Veuillez télécharger la situation",
Expand Down
Loading