-
Notifications
You must be signed in to change notification settings - Fork 2
Razwa/import study #946
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
Open
ghazwarhili
wants to merge
13
commits into
main
Choose a base branch
from
razwa/import-study
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Razwa/import study #946
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4b926ac
export study
ghazwarhili d16b39e
code review rabbit
ghazwarhili 742c7f7
replace with exportStudyArchiveFailed
ghazwarhili a00e005
code review remarks
ghazwarhili f264dd9
fix translation
ghazwarhili ca19eb3
Merge branch 'main' into razwa/export-study
ghazwarhili af6458f
import study
ghazwarhili ba5b516
fix to be zip folder
ghazwarhili b09c095
Merge branch 'main' into razwa/import-study
ghazwarhili 8b8de69
resolve conflicts
ghazwarhili 881ce07
fix prettier
ghazwarhili 90a2ee1
add uploadingStudy
ghazwarhili bd56887
Merge branch 'main' into razwa/import-study
ghazwarhili File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /** | ||
| * Copyright (c) 2026, 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 { ChangeEvent, useCallback } from 'react'; | ||
| import { FormattedMessage, useIntl } from 'react-intl'; | ||
| import { useSelector } from 'react-redux'; | ||
| import { | ||
| CustomMuiDialog, | ||
| DescriptionField, | ||
| ElementType, | ||
| ErrorInput, | ||
| extractErrorMessageDescriptor, | ||
| FieldConstants, | ||
| FieldErrorAlert, | ||
| isObjectEmpty, | ||
| MAX_CHAR_DESCRIPTION, | ||
| NAME_EMPTY, | ||
| useSnackMessage, | ||
| } from '@gridsuite/commons-ui'; | ||
| import { Button, Grid, Input, Stack } from '@mui/material'; | ||
| import { FieldValues, useController, useForm } from 'react-hook-form'; | ||
| import { yupResolver } from '@hookform/resolvers/yup'; | ||
| import * as yup from 'yup'; | ||
| import { AppState } from '../../redux/types'; | ||
| import { importStudy } from '../../utils/rest-api'; | ||
| import PrefilledNameInput from './commons/prefilled-name-input'; | ||
|
|
||
| interface ImportStudyDialogProps { | ||
| open: boolean; | ||
| onClose: () => void; | ||
| } | ||
|
|
||
| interface ImportStudyFormData { | ||
| [FieldConstants.NAME]: string; | ||
| [FieldConstants.DESCRIPTION]: string; | ||
| studyFiles?: FileList; | ||
| } | ||
|
|
||
| export default function ImportStudyDialog({ open, onClose }: Readonly<ImportStudyDialogProps>) { | ||
| const intl = useIntl(); | ||
| const { snackError } = useSnackMessage(); | ||
| const selectedDirectory = useSelector((state: AppState) => state.selectedDirectory); | ||
|
|
||
| const schema: yup.ObjectSchema<ImportStudyFormData> = yup.object().shape({ | ||
| [FieldConstants.NAME]: yup.string().trim().required(NAME_EMPTY), | ||
| [FieldConstants.DESCRIPTION]: yup.string().max(MAX_CHAR_DESCRIPTION), | ||
| studyFiles: yup | ||
| .mixed<FileList>() | ||
| .test('required', intl.formatMessage({ id: 'uploadStudyErrorMsg' }), (value) => { | ||
| return value !== undefined && value !== null && value.length > 0; | ||
| }) | ||
| .test('fileType', intl.formatMessage({ id: 'uploadStudyErrorMsg' }), (value) => { | ||
| if (!value || value.length === 0) return false; | ||
| const file = value[0] as File; | ||
| return file.name.endsWith('.zip'); | ||
| }), | ||
| }) as yup.ObjectSchema<ImportStudyFormData>; | ||
|
|
||
| const importStudyFormMethods = useForm<ImportStudyFormData>({ | ||
| mode: 'onChange', | ||
| resolver: yupResolver<ImportStudyFormData>(schema), | ||
| defaultValues: { | ||
| [FieldConstants.NAME]: '', | ||
| [FieldConstants.DESCRIPTION]: '', | ||
| }, | ||
| }); | ||
| const { | ||
| field: { ref, value: studyFiles, onChange: onStudyFilesChange }, | ||
| } = useController({ | ||
| name: 'studyFiles', | ||
| control: importStudyFormMethods.control, | ||
| }); | ||
|
|
||
| const studyFileName = (studyFiles as FileList | undefined)?.[0]?.name; | ||
|
|
||
| const { | ||
| formState: { errors, isValid }, | ||
| setError, | ||
| setValue, | ||
| } = importStudyFormMethods; | ||
|
|
||
| const onFileChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
| const files = event.target.files as FileList; | ||
| if (files && files.length > 0) { | ||
| onStudyFilesChange(event.target.files); | ||
| setValue(FieldConstants.NAME, files[0].name.replace(/\.zip$/i, ''), { shouldValidate: true }); | ||
| } | ||
| }; | ||
|
|
||
| const handleImportStudy = useCallback( | ||
| async (data: FieldValues) => { | ||
| if (!selectedDirectory?.elementUuid) { | ||
| snackError({ headerId: 'studyImportError' }); | ||
| return; | ||
| } | ||
| await importStudy( | ||
| data[FieldConstants.NAME], | ||
| data[FieldConstants.DESCRIPTION], | ||
| data.studyFiles?.[0] as File, | ||
| selectedDirectory.elementUuid | ||
| ) | ||
| .then(() => onClose()) | ||
| .catch((error) => { | ||
| const { descriptor, values } = extractErrorMessageDescriptor(error, 'studyImportError'); | ||
| setError(`root.${FieldConstants.API_CALL}`, { | ||
| message: intl.formatMessage(descriptor, values).toString(), | ||
| }); | ||
| }); | ||
| }, | ||
| [intl, onClose, selectedDirectory?.elementUuid, setError, snackError] | ||
| ); | ||
| const isFormValid = isObjectEmpty(errors) && isValid; | ||
| return ( | ||
| <CustomMuiDialog | ||
| titleId="importStudy" | ||
| formContext={{ | ||
| ...importStudyFormMethods, | ||
| validationSchema: schema, | ||
| removeOptional: true, | ||
| }} | ||
| open={open} | ||
| onClose={onClose} | ||
| onSave={handleImportStudy} | ||
| onCancel={onClose} | ||
| disabledSave={!isFormValid} | ||
| > | ||
| <Stack spacing={2} marginTop="auto"> | ||
| <Grid> | ||
| <PrefilledNameInput | ||
| name={FieldConstants.NAME} | ||
| label="nameProperty" | ||
| elementType={ElementType.STUDY} | ||
| /> | ||
| </Grid> | ||
| <Grid | ||
| sx={{ | ||
| opacity: 0.5, | ||
| pointerEvents: 'none', | ||
| }} | ||
| > | ||
| <DescriptionField /> | ||
| </Grid> | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| <Grid container alignItems="center" spacing={1} pt={1}> | ||
| <Grid> | ||
| <Button variant="contained" color="primary" component="label"> | ||
| <FormattedMessage id="uploadStudy" /> | ||
| <Input | ||
| ref={ref} | ||
| type="file" | ||
| name="studyFiles" | ||
| inputProps={{ accept: '.zip' }} | ||
| onChange={onFileChange} | ||
| sx={{ display: 'none' }} | ||
| data-testid="ArchiveFileUpload" | ||
| /> | ||
| </Button> | ||
| </Grid> | ||
| <Grid sx={{ fontWeight: 'bold' }}> | ||
| <p>{studyFileName ?? intl.formatMessage({ id: 'uploadMessage' })}</p> | ||
| </Grid> | ||
| </Grid> | ||
| </Stack> | ||
| <ErrorInput name="studyFiles" InputField={FieldErrorAlert} /> | ||
| </CustomMuiDialog> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.