Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
72 changes: 0 additions & 72 deletions src/components/dialogs/commons/prefilled-name-input.tsx

This file was deleted.

43 changes: 24 additions & 19 deletions src/components/dialogs/commons/upload-new-case.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
modifiedByUser = false,
nameModifiedByUser = false,

isNewStudyCreation = false,
getCurrentCaseImportParams,
handleApiCallError,
}: Readonly<UploadNewCaseProps>) {
const intl = useIntl();

const { snackError } = useSnackMessage();
const [caseFileLoading, setCaseFileLoading] = useState(false);

const {
Expand Down Expand Up @@ -58,6 +60,24 @@ export default function UploadNewCase({
return <FormattedMessage id="uploadMessage" />;
}, [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,
Copy link
Contributor

@basseche basseche Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should not use shouldValidate : true, it will removes check on duplicate name

});
})
.catch((error) => {
snackError({
messageTxt: error.message,
});
});
}
};

const onChange = (event: ChangeEvent<HTMLInputElement>) => {
event.preventDefault();

Expand All @@ -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);
Expand All @@ -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, {
Expand Down
16 changes: 12 additions & 4 deletions src/components/dialogs/create-case-dialog/create-case-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ 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';
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';
Expand Down Expand Up @@ -57,12 +58,14 @@ export default function CreateCaseDialog({ onClose, open }: Readonly<CreateCaseD

const {
formState: { errors, isValid },
getValues,
} = createCaseFormMethods;

const isFormValid = isObjectEmpty(errors) && isValid;

const activeDirectory = useSelector((state: AppState) => state.activeDirectory);
const userId = useSelector((state: AppState) => state.user?.profile.sub);
const [modifiedByUser, setModifiedByUser] = useState(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [modifiedByUser, setModifiedByUser] = useState(false);
const [nameModifiedByUser, setNameModifiedByUser] = useState(false);


const handleCreateNewCase = ({ caseName, description, caseFile }: IFormData): void => {
const uploadingCase: UploadingElement = {
Expand Down Expand Up @@ -100,6 +103,7 @@ export default function CreateCaseDialog({ onClose, open }: Readonly<CreateCaseD
// the uploadingCase ghost element will be removed when directory content updated by fetch
dispatch(addUploadingElement(uploadingCase));
};
const caseFileStudy = getValues(FieldConstants.CASE_FILE);

return (
<CustomMuiDialog
Expand All @@ -115,18 +119,22 @@ export default function CreateCaseDialog({ onClose, open }: Readonly<CreateCaseD
>
<Grid container spacing={2} marginTop="auto" direction="column">
<Grid item>
<PrefilledNameInput
<UniqueNameInput
name={FieldConstants.CASE_NAME}
label="nameProperty"
elementType={ElementType.CASE}
elementExists={elementExists}
activeDirectory={activeDirectory}
autoFocus={!caseFileStudy}
onManualChangeCallback={() => setModifiedByUser(true)}
/>
</Grid>
<Grid item>
<DescriptionField />
</Grid>
</Grid>
<ErrorInput name={FieldConstants.CASE_FILE} InputField={FieldErrorAlert} />
<UploadNewCase />
<UploadNewCase modifiedByUser={modifiedByUser} />
</CustomMuiDialog>
);
}
17 changes: 11 additions & 6 deletions src/components/dialogs/create-study-dialog/create-study-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,14 +20,15 @@ import {
keyGenerator,
ModifyElementSelection,
Parameter,
UniqueNameInput,
useConfidentialityWarning,
useSnackMessage,
} from '@gridsuite/commons-ui';
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';
Expand All @@ -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';

Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not a good thing to put the state of the child here, you render the whole form when the state of the child changes !

const { elementUuid, elementName } = providedExistingCase || {};

const createStudyFormMethods = useForm<CreateStudyDialogFormValues>({
Expand Down Expand Up @@ -237,7 +237,7 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase
getCurrentCaseImportParams(providedExistingCase.elementUuid);
}
}, [getCurrentCaseImportParams, providedExistingCase, setValue]);

const caseFileStudy = getValues(FieldConstants.CASE_FILE);
return (
<CustomMuiDialog
titleId="createNewStudy"
Expand All @@ -253,10 +253,14 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase
>
<Grid container spacing={2} marginTop="auto" direction="column">
<Grid item>
<PrefilledNameInput
<UniqueNameInput
name={FieldConstants.STUDY_NAME}
label="nameProperty"
elementType={ElementType.STUDY}
elementExists={elementExists}
activeDirectory={activeDirectory}
autoFocus={!caseFileStudy}
onManualChangeCallback={() => setModifiedByUser(true)}
/>
</Grid>
<Grid item>
Expand All @@ -272,6 +276,7 @@ export default function CreateStudyDialog({ open, onClose, providedExistingCase
/>
) : (
<UploadNewCase
modifiedByUser={modifiedByUser}
isNewStudyCreation
getCurrentCaseImportParams={getCurrentCaseImportParams}
handleApiCallError={handleApiCallError}
Expand Down
Loading