diff --git a/src/components/dialogs/commons/prefilled-name-input.tsx b/src/components/dialogs/commons/prefilled-name-input.tsx deleted file mode 100644 index c7a17a6c5..000000000 --- a/src/components/dialogs/commons/prefilled-name-input.tsx +++ /dev/null @@ -1,72 +0,0 @@ -/** - * 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 { useEffect, useState } from 'react'; -import { useFormContext } from 'react-hook-form'; -import { ElementType, FieldConstants, UniqueNameInput } from '@gridsuite/commons-ui'; -import { useSelector } from 'react-redux'; -import { elementExists, getBaseName } from '../../../utils/rest-api'; -import { AppState } from '../../../redux/types'; - -export interface PrefilledNameInputProps { - label: string; - name: string; - elementType: ElementType; -} - -/** - * Input component that automatically fill the field when a case is uploaded - * Used for CreateCaseDialog and CreateStudyDialog - */ -export default function PrefilledNameInput({ label, name, elementType }: Readonly) { - const { - setValue, - clearErrors, - watch, - formState: { errors }, - } = useFormContext(); - - const [modifiedByUser, setModifiedByUser] = useState(false); - - const caseFile = watch(FieldConstants.CASE_FILE) as File; - const caseFileErrorMessage = errors.caseFile?.message; - const apiCallErrorMessage = errors.root?.apiCall?.message; - - const activeDirectory = useSelector((state: AppState) => state.activeDirectory); - - useEffect(() => { - // we replace the name only if some conditions are respected - if (caseFile && !modifiedByUser && !apiCallErrorMessage && !caseFileErrorMessage) { - const { name: caseName } = caseFile; - - if (caseName) { - clearErrors(name); - getBaseName(caseName) - .then((response) => { - setValue(name, response, { - shouldDirty: true, - }); - }) - .catch((error) => { - console.error('Error fetching base name:', error); - }); - } - } - }, [caseFile, modifiedByUser, apiCallErrorMessage, caseFileErrorMessage, setValue, clearErrors, name]); - - return ( - setModifiedByUser(true)} - /> - ); -} diff --git a/src/components/dialogs/commons/upload-new-case.tsx b/src/components/dialogs/commons/upload-new-case.tsx index a3c062829..71ba51ebb 100644 --- a/src/components/dialogs/commons/upload-new-case.tsx +++ b/src/components/dialogs/commons/upload-new-case.tsx @@ -9,11 +9,12 @@ import { ChangeEvent, useMemo, useState } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { Button, CircularProgress, Grid, Input } from '@mui/material'; import { useController, useFormContext } from 'react-hook-form'; -import { FieldConstants } from '@gridsuite/commons-ui'; +import { FieldConstants, useSnackMessage } from '@gridsuite/commons-ui'; import { UUID } from 'crypto'; -import { createCaseWithoutDirectoryElementCreation, deleteCase } from '../../../utils/rest-api'; +import { createCaseWithoutDirectoryElementCreation, deleteCase, getBaseName } from '../../../utils/rest-api'; export interface UploadNewCaseProps { + modifiedByUser?: boolean; isNewStudyCreation?: boolean; getCurrentCaseImportParams?: (uuid: UUID) => void; handleApiCallError?: ErrorCallback; @@ -23,12 +24,13 @@ const MAX_FILE_SIZE_IN_MO = 100; const MAX_FILE_SIZE_IN_BYTES = MAX_FILE_SIZE_IN_MO * 1024 * 1024; export default function UploadNewCase({ + modifiedByUser = false, isNewStudyCreation = false, getCurrentCaseImportParams, handleApiCallError, }: Readonly) { const intl = useIntl(); - + const { snackError } = useSnackMessage(); const [caseFileLoading, setCaseFileLoading] = useState(false); const { @@ -58,6 +60,24 @@ export default function UploadNewCase({ return ; }, [caseFileLoading, caseFileName]); + const fetchBaseName = (currentFile: any) => { + const { name: currentCaseFileName } = currentFile; + const name = isNewStudyCreation ? FieldConstants.STUDY_NAME : FieldConstants.CASE_NAME; + if (currentCaseFileName && !modifiedByUser) { + getBaseName(currentCaseFileName) + .then((response) => { + setValue(name, response, { + shouldValidate: true, + }); + }) + .catch((error) => { + snackError({ + messageTxt: error.message, + }); + }); + } + }; + const onChange = (event: ChangeEvent) => { event.preventDefault(); @@ -71,9 +91,7 @@ export default function UploadNewCase({ if (currentFile.size <= MAX_FILE_SIZE_IN_BYTES) { onValueChange(currentFile); - - const { name: currentCaseFileName } = currentFile; - + fetchBaseName(currentFile); if (isNewStudyCreation) { // Create new case setCaseFileLoading(true); @@ -95,19 +113,6 @@ export default function UploadNewCase({ .finally(() => { setCaseFileLoading(false); }); - } else { - const caseName = getValues(FieldConstants.CASE_NAME); - if (currentCaseFileName && caseName !== currentCaseFileName) { - clearErrors(FieldConstants.CASE_NAME); - setValue( - FieldConstants.CASE_NAME, - currentCaseFileName.substring(0, currentCaseFileName.indexOf('.')), - { - shouldDirty: true, - shouldValidate: true, - } - ); - } } } else { setError(FieldConstants.CASE_FILE, { diff --git a/src/components/dialogs/create-case-dialog/create-case-dialog.tsx b/src/components/dialogs/create-case-dialog/create-case-dialog.tsx index 08fb21ffd..4dea31998 100644 --- a/src/components/dialogs/create-case-dialog/create-case-dialog.tsx +++ b/src/components/dialogs/create-case-dialog/create-case-dialog.tsx @@ -18,10 +18,12 @@ import { FieldErrorAlert, isObjectEmpty, keyGenerator, + UniqueNameInput, useConfidentialityWarning, useSnackMessage, } from '@gridsuite/commons-ui'; -import { createCase } from '../../../utils/rest-api'; +import { useState } from 'react'; +import { createCase, elementExists } from '../../../utils/rest-api'; import { HTTP_UNPROCESSABLE_ENTITY_STATUS } from '../../../utils/UIconstants'; import { addUploadingElement, removeUploadingElement } from '../../../redux/actions'; import UploadNewCase from '../commons/upload-new-case'; @@ -29,7 +31,6 @@ import { createCaseDialogFormValidationSchema, getCreateCaseDialogFormValidationDefaultValues, } from './create-case-dialog-utils'; -import PrefilledNameInput from '../commons/prefilled-name-input'; import { handleMaxElementsExceededError } from '../../utils/rest-errors'; import { AppDispatch } from '../../../redux/store'; import { AppState, UploadingElement } from '../../../redux/types'; @@ -57,12 +58,14 @@ export default function CreateCaseDialog({ onClose, open }: Readonly state.activeDirectory); const userId = useSelector((state: AppState) => state.user?.profile.sub); + const [modifiedByUser, setModifiedByUser] = useState(false); const handleCreateNewCase = ({ caseName, description, caseFile }: IFormData): void => { const uploadingCase: UploadingElement = { @@ -100,6 +103,7 @@ export default function CreateCaseDialog({ onClose, open }: Readonly - setModifiedByUser(true)} /> @@ -126,7 +134,7 @@ export default function CreateCaseDialog({ onClose, open }: Readonly - + ); } diff --git a/src/components/dialogs/create-study-dialog/create-study-dialog.tsx b/src/components/dialogs/create-study-dialog/create-study-dialog.tsx index 0fcead6b3..083210092 100644 --- a/src/components/dialogs/create-study-dialog/create-study-dialog.tsx +++ b/src/components/dialogs/create-study-dialog/create-study-dialog.tsx @@ -7,7 +7,7 @@ import { useForm } from 'react-hook-form'; import { Grid } from '@mui/material'; import { useIntl } from 'react-intl'; -import { useCallback, useEffect } from 'react'; +import { useCallback, useEffect, useState } from 'react'; import { CustomMuiDialog, DescriptionField, @@ -20,6 +20,7 @@ import { keyGenerator, ModifyElementSelection, Parameter, + UniqueNameInput, useConfidentialityWarning, useSnackMessage, } from '@gridsuite/commons-ui'; @@ -27,7 +28,7 @@ import { useDispatch, useSelector } from 'react-redux'; import { yupResolver } from '@hookform/resolvers/yup/dist/yup'; import { UUID } from 'crypto'; import UploadNewCase from '../commons/upload-new-case'; -import { createStudy, deleteCase, getCaseImportParameters } from '../../../utils/rest-api'; +import { createStudy, deleteCase, elementExists, getCaseImportParameters } from '../../../utils/rest-api'; import { HTTP_CONNECTION_FAILED_MESSAGE, HTTP_UNPROCESSABLE_ENTITY_STATUS } from '../../../utils/UIconstants'; import ImportParametersSection from './importParametersSection'; import { addUploadingElement, removeUploadingElement, setActiveDirectory } from '../../../redux/actions'; @@ -36,7 +37,6 @@ import { CreateStudyDialogFormValues, getCreateStudyDialogFormDefaultValues, } from './create-study-dialog-utils'; -import PrefilledNameInput from '../commons/prefilled-name-input'; import { handleMaxElementsExceededError } from '../../utils/rest-errors'; import { AppState, UploadingElement } from '../../../redux/types'; @@ -77,7 +77,7 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase const activeDirectory = useSelector((state: AppState) => state.activeDirectory); const selectedDirectory = useSelector((state: AppState) => state.selectedDirectory); const userId = useSelector((state: AppState) => state.user?.profile.sub); - + const [modifiedByUser, setModifiedByUser] = useState(false); const { elementUuid, elementName } = providedExistingCase || {}; const createStudyFormMethods = useForm({ @@ -237,7 +237,7 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase getCurrentCaseImportParams(providedExistingCase.elementUuid); } }, [getCurrentCaseImportParams, providedExistingCase, setValue]); - + const caseFileStudy = getValues(FieldConstants.CASE_FILE); return ( - setModifiedByUser(true)} /> @@ -272,6 +276,7 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase /> ) : (