Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 19 additions & 4 deletions src/components/dialogs/commons/prefilled-name-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useEffect, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { ElementType, FieldConstants, UniqueNameInput } from '@gridsuite/commons-ui';
import { ElementType, FieldConstants, UniqueNameInput, useSnackMessage } from '@gridsuite/commons-ui';
import { useSelector } from 'react-redux';
import { elementExists, getBaseName } from '../../../utils/rest-api';
import { AppState } from '../../../redux/types';
Expand All @@ -25,12 +25,14 @@ export interface PrefilledNameInputProps {
export default function PrefilledNameInput({ label, name, elementType }: Readonly<PrefilledNameInputProps>) {
const {
setValue,
getValues,
clearErrors,
watch,
formState: { errors },
} = useFormContext();

const [modifiedByUser, setModifiedByUser] = useState(false);
const { snackError } = useSnackMessage();

const caseFile = watch(FieldConstants.CASE_FILE) as File;
const caseFileErrorMessage = errors.caseFile?.message;
Expand All @@ -42,8 +44,9 @@ export default function PrefilledNameInput({ label, name, elementType }: Readonl
// we replace the name only if some conditions are respected
if (caseFile && !modifiedByUser && !apiCallErrorMessage && !caseFileErrorMessage) {
const { name: caseName } = caseFile;
const currentCaseName = getValues(name);

if (caseName) {
if (caseName && caseName !== currentCaseName) {
Copy link
Contributor

Choose a reason for hiding this comment

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

caseName = basename + extension (PtFige-20241120-1520-enrichi_N1.zip )
currentCaseName = basename (PtFige-20241120-1520-enrichi_N1)

caseName !== currentCaseName is always true.

Copy link
Contributor Author

@basseche basseche Jan 3, 2025

Choose a reason for hiding this comment

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

yes you are right. I wanted only to check if file doesn't change (we select the same file)

clearErrors(name);
getBaseName(caseName)
.then((response) => {
Expand All @@ -52,11 +55,23 @@ export default function PrefilledNameInput({ label, name, elementType }: Readonl
});
})
.catch((error) => {
console.error('Error fetching base name:', error);
snackError({
messageTxt: error.message,
});
});
}
}
}, [caseFile, modifiedByUser, apiCallErrorMessage, caseFileErrorMessage, setValue, clearErrors, name]);
}, [
caseFile,
modifiedByUser,
apiCallErrorMessage,
caseFileErrorMessage,
setValue,
getValues,
clearErrors,
name,
snackError,
]);

return (
<UniqueNameInput
Expand Down
17 changes: 1 addition & 16 deletions src/components/dialogs/commons/upload-new-case.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function UploadNewCase({
name: FieldConstants.CASE_UUID,
});

const { clearErrors, setError, getValues, setValue } = useFormContext();
const { clearErrors, setError, getValues } = useFormContext();

const caseFile = value as File;
const { name: caseFileName } = caseFile || {};
Expand Down Expand Up @@ -72,8 +72,6 @@ export default function UploadNewCase({
if (currentFile.size <= MAX_FILE_SIZE_IN_BYTES) {
onValueChange(currentFile);

const { name: currentCaseFileName } = currentFile;

if (isNewStudyCreation) {
// Create new case
setCaseFileLoading(true);
Expand All @@ -95,19 +93,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
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default function CreateCaseDialog({ onClose, open }: Readonly<CreateCaseD
</Grid>
</Grid>
<ErrorInput name={FieldConstants.CASE_FILE} InputField={FieldErrorAlert} />
<UploadNewCase />
<UploadNewCase isNewStudyCreation />
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you add isNewStudyCreation to the create-case-dialog? The UploadNewCase component is shared between case and study, and this variable is used to distinguish between the two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

</CustomMuiDialog>
);
}
Loading