|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | + |
| 3 | +import { useState, useCallback } from 'react'; |
| 4 | +import { Modal, Button, Text } from '@mantine/core'; |
| 5 | +import { useDisclosure } from '@mantine/hooks'; |
| 6 | +import { notifications } from '@mantine/notifications'; |
| 7 | + |
| 8 | +import { useAppContext } from '#app/AppContext'; |
| 9 | +import AllergiesTableRow from './AllergiesTableRow'; |
| 10 | +import DataTable from '#components/DataTable/DataTable'; |
| 11 | +import { useDeleteAllergy } from './useDeleteAllergy'; |
| 12 | + |
| 13 | +const allergiesTableProps = { |
| 14 | + headers: PropTypes.arrayOf( |
| 15 | + PropTypes.shape({ |
| 16 | + key: PropTypes.string.isRequired, |
| 17 | + text: PropTypes.node, |
| 18 | + }) |
| 19 | + ), |
| 20 | + data: PropTypes.arrayOf( |
| 21 | + PropTypes.shape({ |
| 22 | + id: PropTypes.string.isRequired, |
| 23 | + name: PropTypes.string.isRequired, |
| 24 | + type: PropTypes.string.isRequired, |
| 25 | + }) |
| 26 | + ), |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Allergies table component |
| 31 | + * @param {PropTypes.InferProps<typeof allergiesTableProps>} props |
| 32 | + */ |
| 33 | +export default function AllergiesTable ({ headers, data }) { |
| 34 | + const [opened, { open, close }] = useDisclosure(false); |
| 35 | + const [allergy, setSelectedAllergy] = useState(null); |
| 36 | + const { mutateAsync: deleteAllergy, isPending } = useDeleteAllergy(); |
| 37 | + const { user } = useAppContext(); |
| 38 | + |
| 39 | + const showDeleteConfirmation = useCallback( |
| 40 | + (allergies) => { |
| 41 | + setSelectedAllergy(allergies); |
| 42 | + open(); |
| 43 | + }, |
| 44 | + [open] |
| 45 | + ); |
| 46 | + |
| 47 | + const confirmAllergyDeletion = async () => { |
| 48 | + try { |
| 49 | + await deleteAllergy(allergy.id); |
| 50 | + notifications.show({ |
| 51 | + title: 'Success', |
| 52 | + message: 'Allergy deleted successfully.', |
| 53 | + color: 'green', |
| 54 | + }); |
| 55 | + } catch (error) { |
| 56 | + console.error('Failed to delete Allergy:', error); |
| 57 | + notifications.show({ |
| 58 | + title: 'Error', |
| 59 | + message: 'Failed to delete Allergy.', |
| 60 | + color: 'red', |
| 61 | + }); |
| 62 | + } |
| 63 | + if (!isPending) { |
| 64 | + setSelectedAllergy(null); |
| 65 | + close(); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + const cancelAllergyDeletion = () => { |
| 70 | + setSelectedAllergy(null); |
| 71 | + close(); |
| 72 | + }; |
| 73 | + |
| 74 | + const renderRow = useCallback( |
| 75 | + (allergy) => ( |
| 76 | + <AllergiesTableRow |
| 77 | + key={allergy.id} |
| 78 | + allergy={allergy} |
| 79 | + headers={headers} |
| 80 | + onDelete={showDeleteConfirmation} |
| 81 | + showDeleteMenu={user?.role === 'ADMIN'} |
| 82 | + /> |
| 83 | + ), |
| 84 | + [headers, user?.role, showDeleteConfirmation] |
| 85 | + ); |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <DataTable |
| 90 | + headers={headers} |
| 91 | + data={data} |
| 92 | + renderRow={renderRow} |
| 93 | + emptyStateMessage='No allergies found.' |
| 94 | + /> |
| 95 | + <Modal |
| 96 | + opened={opened} |
| 97 | + onClose={close} |
| 98 | + title='Delete Allergy' |
| 99 | + > |
| 100 | + <Text fw={600}> |
| 101 | + Are you sure you want to delete this Allergy {allergy?.name}? |
| 102 | + </Text> |
| 103 | + <Button |
| 104 | + color='red' |
| 105 | + fullWidth |
| 106 | + onClick={confirmAllergyDeletion} |
| 107 | + loading={isPending} |
| 108 | + > |
| 109 | + Yes |
| 110 | + </Button> |
| 111 | + <Button |
| 112 | + color='blue' |
| 113 | + fullWidth |
| 114 | + onClick={cancelAllergyDeletion} |
| 115 | + disabled={isPending} |
| 116 | + > |
| 117 | + No |
| 118 | + </Button> |
| 119 | + </Modal> |
| 120 | + </> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +AllergiesTable.propTypes = allergiesTableProps; |
0 commit comments