|
| 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, useCallback } from 'react'; |
| 9 | +import { |
| 10 | + Button, |
| 11 | + Dialog, |
| 12 | + DialogActions, |
| 13 | + DialogContent, |
| 14 | + DialogContentText, |
| 15 | + DialogTitle, |
| 16 | +} from '@mui/material'; |
| 17 | +import { FormattedMessage, useIntl } from 'react-intl'; |
| 18 | +import { UserInfos } from '../../services'; |
| 19 | + |
| 20 | +export interface DeleteUserDialogProps { |
| 21 | + open: boolean; |
| 22 | + setOpen: (open: boolean) => void; |
| 23 | + usersInfos: UserInfos[]; |
| 24 | + deleteUsers: () => void; |
| 25 | +} |
| 26 | + |
| 27 | +const DeleteUserDialog: FunctionComponent<DeleteUserDialogProps> = (props) => { |
| 28 | + const { open, setOpen, deleteUsers, usersInfos } = props; |
| 29 | + const intl = useIntl(); |
| 30 | + |
| 31 | + const handleClose = useCallback(() => { |
| 32 | + setOpen(false); |
| 33 | + }, [setOpen]); |
| 34 | + |
| 35 | + const buildTitle = useCallback( |
| 36 | + (users: UserInfos[]) => { |
| 37 | + const hasMultipleItems = users.length > 1; |
| 38 | + const descriptor = { |
| 39 | + id: hasMultipleItems |
| 40 | + ? 'users.form.delete.multiple.dialog.title' |
| 41 | + : 'users.form.delete.dialog.title', |
| 42 | + }; |
| 43 | + return intl.formatMessage( |
| 44 | + descriptor, |
| 45 | + hasMultipleItems ? { itemsCount: users.length } : undefined |
| 46 | + ); |
| 47 | + }, |
| 48 | + [intl] |
| 49 | + ); |
| 50 | + const onSubmit = useCallback(() => { |
| 51 | + deleteUsers(); |
| 52 | + setOpen(false); |
| 53 | + }, [deleteUsers, setOpen]); |
| 54 | + |
| 55 | + const buildContent = useCallback( |
| 56 | + (users: UserInfos[]) => { |
| 57 | + const hasMultipleItems = users.length > 1; |
| 58 | + const descriptor = { |
| 59 | + id: hasMultipleItems |
| 60 | + ? 'users.form.delete.multiple.dialog.message' |
| 61 | + : 'users.form.delete.dialog.message', |
| 62 | + }; |
| 63 | + if (hasMultipleItems) { |
| 64 | + return intl.formatMessage(descriptor, { |
| 65 | + itemsCount: users.length, |
| 66 | + }); |
| 67 | + } |
| 68 | + return intl.formatMessage(descriptor, { |
| 69 | + itemName: users.length === 1 && users[0].sub, |
| 70 | + }); |
| 71 | + }, |
| 72 | + [intl] |
| 73 | + ); |
| 74 | + |
| 75 | + return ( |
| 76 | + <Dialog open={open} onClose={handleClose}> |
| 77 | + <DialogTitle>{buildTitle(usersInfos)}</DialogTitle> |
| 78 | + <DialogContent> |
| 79 | + <DialogContentText> |
| 80 | + {buildContent(usersInfos)} |
| 81 | + </DialogContentText> |
| 82 | + </DialogContent> |
| 83 | + <DialogActions> |
| 84 | + <Button onClick={handleClose}> |
| 85 | + <FormattedMessage id="cancel" /> |
| 86 | + </Button> |
| 87 | + <Button type="submit" onClick={onSubmit}> |
| 88 | + <FormattedMessage id="ok" /> |
| 89 | + </Button> |
| 90 | + </DialogActions> |
| 91 | + </Dialog> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export default DeleteUserDialog; |
0 commit comments