|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { FunctionComponent, RefObject, useCallback } from 'react'; |
| 9 | +import { |
| 10 | + Button, |
| 11 | + Dialog, |
| 12 | + DialogActions, |
| 13 | + DialogContent, |
| 14 | + DialogContentText, |
| 15 | + DialogTitle, |
| 16 | + InputAdornment, |
| 17 | + TextField, |
| 18 | +} from '@mui/material'; |
| 19 | +import { FormattedMessage } from 'react-intl'; |
| 20 | +import { Controller, useForm } from 'react-hook-form'; |
| 21 | +import { ManageAccounts } from '@mui/icons-material'; |
| 22 | +import { UserAdminSrv, UserProfile } from '../../services'; |
| 23 | +import { useSnackMessage } from '@gridsuite/commons-ui'; |
| 24 | +import { GridTableRef } from '../../components/Grid'; |
| 25 | +import PaperForm from '../common/paper-form'; |
| 26 | + |
| 27 | +export interface AddProfileDialogProps { |
| 28 | + gridRef: RefObject<GridTableRef<UserProfile>>; |
| 29 | + open: boolean; |
| 30 | + setOpen: (open: boolean) => void; |
| 31 | +} |
| 32 | + |
| 33 | +const AddProfileDialog: FunctionComponent<AddProfileDialogProps> = (props) => { |
| 34 | + const { snackError } = useSnackMessage(); |
| 35 | + |
| 36 | + const { handleSubmit, control, reset, clearErrors } = useForm<{ |
| 37 | + name: string; |
| 38 | + }>({ |
| 39 | + defaultValues: { name: '' }, //need default not undefined value for html input, else react error at runtime |
| 40 | + }); |
| 41 | + |
| 42 | + const addProfile = useCallback( |
| 43 | + (name: string) => { |
| 44 | + const profileData: UserProfile = { |
| 45 | + name: name, |
| 46 | + }; |
| 47 | + UserAdminSrv.addProfile(profileData) |
| 48 | + .catch((error) => |
| 49 | + snackError({ |
| 50 | + messageTxt: error.message, |
| 51 | + headerId: 'profiles.table.error.add', |
| 52 | + }) |
| 53 | + ) |
| 54 | + .then(() => props.gridRef?.current?.context?.refresh?.()); |
| 55 | + }, |
| 56 | + [props.gridRef, snackError] |
| 57 | + ); |
| 58 | + |
| 59 | + const handleClose = useCallback(() => { |
| 60 | + props.setOpen(false); |
| 61 | + reset(); |
| 62 | + clearErrors(); |
| 63 | + }, [clearErrors, props, reset]); |
| 64 | + |
| 65 | + const onSubmit = useCallback( |
| 66 | + (data: { name: string }) => { |
| 67 | + addProfile(data.name.trim()); |
| 68 | + handleClose(); |
| 69 | + }, |
| 70 | + [addProfile, handleClose] |
| 71 | + ); |
| 72 | + |
| 73 | + return ( |
| 74 | + <Dialog |
| 75 | + open={props.open} |
| 76 | + onClose={handleClose} |
| 77 | + PaperComponent={(props) => ( |
| 78 | + <PaperForm |
| 79 | + untypedProps={props} |
| 80 | + onSubmit={handleSubmit(onSubmit)} |
| 81 | + /> |
| 82 | + )} |
| 83 | + > |
| 84 | + <DialogTitle> |
| 85 | + <FormattedMessage id="profiles.form.title" /> |
| 86 | + </DialogTitle> |
| 87 | + <DialogContent> |
| 88 | + <DialogContentText> |
| 89 | + <FormattedMessage id="profiles.form.content" /> |
| 90 | + </DialogContentText> |
| 91 | + <Controller |
| 92 | + name="name" |
| 93 | + control={control} |
| 94 | + rules={{ required: true, minLength: 1 }} |
| 95 | + render={({ field, fieldState, formState }) => ( |
| 96 | + <TextField |
| 97 | + {...field} |
| 98 | + autoFocus |
| 99 | + required |
| 100 | + margin="dense" |
| 101 | + label={ |
| 102 | + <FormattedMessage id="profiles.form.field.profilename.label" /> |
| 103 | + } |
| 104 | + type="text" |
| 105 | + fullWidth |
| 106 | + variant="standard" |
| 107 | + inputMode="text" |
| 108 | + InputProps={{ |
| 109 | + startAdornment: ( |
| 110 | + <InputAdornment position="start"> |
| 111 | + <ManageAccounts /> |
| 112 | + </InputAdornment> |
| 113 | + ), |
| 114 | + }} |
| 115 | + error={fieldState?.invalid} |
| 116 | + helperText={fieldState?.error?.message} |
| 117 | + /> |
| 118 | + )} |
| 119 | + /> |
| 120 | + </DialogContent> |
| 121 | + <DialogActions> |
| 122 | + <Button onClick={handleClose}> |
| 123 | + <FormattedMessage id="cancel" /> |
| 124 | + </Button> |
| 125 | + <Button type="submit"> |
| 126 | + <FormattedMessage id="ok" /> |
| 127 | + </Button> |
| 128 | + </DialogActions> |
| 129 | + </Dialog> |
| 130 | + ); |
| 131 | +}; |
| 132 | +export default AddProfileDialog; |
0 commit comments