From fdc92e7e588a043aa87c364ab78c850a109dd278 Mon Sep 17 00:00:00 2001 From: James Brunskill Date: Fri, 2 Feb 2024 14:07:20 +1300 Subject: [PATCH] Save Interactions --- .../Interactions/DrugOrGroupSelector.tsx | 8 +- .../Interactions/InteractionEditModal.tsx | 111 +++++++++++++++--- .../InteractionGroupEditModal.tsx | 5 +- .../Interactions/InteractionGroupTab.tsx | 3 +- .../Admin/Interactions/InteractionsTab.tsx | 54 ++++++++- .../api/hooks/useDeleteInteraction.ts | 13 ++ .../api/hooks/useUpsertInteraction.ts | 15 +++ 7 files changed, 180 insertions(+), 29 deletions(-) create mode 100644 frontend/system/src/Admin/Interactions/api/hooks/useDeleteInteraction.ts create mode 100644 frontend/system/src/Admin/Interactions/api/hooks/useUpsertInteraction.ts diff --git a/frontend/system/src/Admin/Interactions/DrugOrGroupSelector.tsx b/frontend/system/src/Admin/Interactions/DrugOrGroupSelector.tsx index 505ea659a..4c6c5e117 100644 --- a/frontend/system/src/Admin/Interactions/DrugOrGroupSelector.tsx +++ b/frontend/system/src/Admin/Interactions/DrugOrGroupSelector.tsx @@ -42,10 +42,10 @@ export const DrugOrGroupSelector: FC = ({ const groupOptions = useMemo(() => { if (!groups) return []; - return groups.map(drug => ({ - id: drug.id, - name: drug.description ?? 'Unknown', - label: drug.name ?? 'Unknown', + return groups.map(group => ({ + id: group.id, + name: group.name ?? 'Unknown', + label: group.name ?? 'Unknown', })); }, [groups]); diff --git a/frontend/system/src/Admin/Interactions/InteractionEditModal.tsx b/frontend/system/src/Admin/Interactions/InteractionEditModal.tsx index 0854c18ce..2d66f4d58 100644 --- a/frontend/system/src/Admin/Interactions/InteractionEditModal.tsx +++ b/frontend/system/src/Admin/Interactions/InteractionEditModal.tsx @@ -19,6 +19,9 @@ import { useAllDrugInteractionGroups } from './api'; import { DrugOrGroupSelector } from './DrugOrGroupSelector'; import { DrugInteractionSeverityNode } from '@common/types'; import { DrugInteractionFragment } from './api/operations.generated'; +import { useUpsertDrugInteraction } from './api/hooks/useUpsertInteraction'; +import Alert from '@mui/material/Alert'; +import AlertTitle from '@mui/material/AlertTitle'; type InteractionEditModalProps = { isOpen: boolean; @@ -50,6 +53,8 @@ export const InteractionEditModal = ({ group2: interaction?.group2, }); + const [addDrugInteration, invalidateQueries] = useUpsertDrugInteraction(); + const { data: drugs, isLoading: drugListLoading } = useEntities({ filter: { categories: ['drug'], @@ -66,10 +71,10 @@ export const InteractionEditModal = ({ const { data: groups, isLoading: groupListLoading } = useAllDrugInteractionGroups(); - const { Modal } = useDialog({ isOpen, onClose }); + const [isLoading, setIsLoading] = useState(false); + const [errorMessage, setErrorMessage] = useState(''); - // TODO: set from queries - const isLoading = false; + const { Modal } = useDialog({ isOpen, onClose }); const isInvalid = !draft.name || !draft.description || !draft.severity; const modalWidth = Math.min(window.innerWidth - 200, 800); @@ -81,9 +86,33 @@ export const InteractionEditModal = ({ { - // TODO: save and handle errors - console.log('value to be saved:', draft); - onClose(); + addDrugInteration({ + input: { + id: draft.id, + name: draft.name, + description: draft.description ?? '', + action: draft.action ?? '', + reference: draft.reference ?? '', + severity: draft.severity, + drug1: draft.drug1?.code, + drug2: draft.drug2?.code, + group1: draft.group1?.id, + group2: draft.group2?.id, + }, + }) + .catch(err => { + setIsLoading(false); + if (!err || !err.message) { + err = { message: t('messages.unknown-error') }; + } + setErrorMessage(err.message); + }) + .then(() => { + invalidateQueries(); + setIsLoading(false); + onClose(); + }); + setIsLoading(false); }} isLoading={isLoading} startIcon={} @@ -110,7 +139,6 @@ export const InteractionEditModal = ({ value={draft?.name ?? ''} onChange={e => setDraft({ ...draft, name: e.currentTarget.value })} /> - - Interacts with - -