Skip to content
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

6177 create patient from prescription window #6269

Merged
merged 9 commits into from
Jan 30, 2025
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useState } from 'react';
import { AppRoute } from '@openmsupply-client/config';
import {
DownloadIcon,
Expand All @@ -16,8 +16,13 @@ import {
Platform,
useNavigate,
RouteBuilder,
useCallbackWithPermission,
UserPermission,
} from '@openmsupply-client/common';
import { PatientSearchModal } from '@openmsupply-client/system';
import {
CreatePatientModal,
PatientSearchModal,
} from '@openmsupply-client/system';
import { ListParams, usePrescriptionList, usePrescription } from '../api';
import { prescriptionToCsv } from '../../utils';

Expand All @@ -28,13 +33,21 @@ export const AppBarButtonsComponent: FC<{
const t = useTranslation();
const navigate = useNavigate();
const { success, error } = useNotification();
const [patientModalOpen, setPatientModalOpen] = useState(false);

const {
create: { create },
} = usePrescription();

const {
query: { data, isLoading },
} = usePrescriptionList(listParams);

const onCreatePatient = useCallbackWithPermission(
UserPermission.PatientMutate,
() => setPatientModalOpen(true)
);

const csvExport = async () => {
if (!data || !data?.nodes.length) {
error(t('error.no-data'))();
Expand Down Expand Up @@ -78,6 +91,7 @@ export const AppBarButtonsComponent: FC<{
errorSnack();
}
}}
openPatientModal={onCreatePatient}
/>
<LoadingButton
startIcon={<DownloadIcon />}
Expand All @@ -87,6 +101,9 @@ export const AppBarButtonsComponent: FC<{
disabled={EnvUtils.platform === Platform.Android}
label={t('button.export')}
/>
{patientModalOpen && (
<CreatePatientModal onClose={() => setPatientModalOpen(false)} />
)}
</Grid>
</AppBarButtonsPortal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
AutocompleteList,
BasicModal,
Box,
ButtonWithIcon,
createQueryParamsStore,
ModalTitle,
PlusCircleIcon,
QueryParamsProvider,
Typography,
useTranslation,
Expand All @@ -23,6 +25,7 @@ const PatientSearchComponent: FC<PatientSearchModalProps> = ({
open,
onClose,
onChange,
openPatientModal,
}) => {
const t = useTranslation();
const PatientOptionRenderer = getPatientOptionRenderer();
Expand All @@ -36,6 +39,11 @@ const PatientSearchComponent: FC<PatientSearchModalProps> = ({
onClose();
};

function handlePatientModalClick() {
handleClose();
openPatientModal();
}
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved

return (
<BasicModal open={open} onClose={handleClose} height={modalHeight}>
<ModalTitle title={t('label.patients')} />
Expand Down Expand Up @@ -65,6 +73,13 @@ const PatientSearchComponent: FC<PatientSearchModalProps> = ({
noOptionsText=""
/>
</Box>
<Box p={2}>
<ButtonWithIcon
Icon={<PlusCircleIcon />}
label={'Create new patient'}
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved
onClick={handlePatientModalClick}
/>
</Box>
</BasicModal>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
useTranslation,
useDebounceCallback,
DocumentRegistryCategoryNode,
useLocation,
RouteBuilder,
} from '@openmsupply-client/common';
import { AppRoute } from '@openmsupply-client/config';
import { PatientFormTab } from './PatientFormTab';
import { PatientResultsTab } from './PatientResultsTab';
import {
Expand All @@ -36,9 +39,7 @@ export const CreatePatientModal: FC<CreatePatientModal> = ({ onClose }) => {
useDocumentRegistry.get.documentRegistries({
filter: { category: { equalTo: DocumentRegistryCategoryNode.Patient } },
});

const [hasError, setHasError] = useState(false);

const [, setDocumentRegistry] = useState<
DocumentRegistryFragment | undefined
>();
Expand All @@ -47,6 +48,7 @@ export const CreatePatientModal: FC<CreatePatientModal> = ({ onClose }) => {
onClose,
});
const navigate = useNavigate();
const location = useLocation();
const { createNewPatient, setCreateNewPatient } = usePatientStore();
const t = useTranslation();

Expand All @@ -56,7 +58,19 @@ export const CreatePatientModal: FC<CreatePatientModal> = ({ onClose }) => {

const onOk = () => {
if (createNewPatient) {
navigate(createNewPatient.id);
const urlSegments = location.pathname.split('/');

if (urlSegments.includes(AppRoute.Patients))
navigate(createNewPatient.id);

if (urlSegments.includes(AppRoute.Prescription))
navigate(
RouteBuilder.create(AppRoute.Dispensary)
.addPart(AppRoute.Patients)
.addPart(createNewPatient.id)
.addQuery({ previousPath: AppRoute.Prescription })
.build()
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved
);
}
};

Expand Down
33 changes: 32 additions & 1 deletion client/packages/system/src/Patient/PatientView/PatientView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
BasicSpinner,
DocumentRegistryCategoryNode,
useNavigate,
useSearchParams,
RouteBuilder,
FnUtils,
} from '@openmsupply-client/common';
import { AppRoute } from '@openmsupply-client/config';
import { usePatient } from '../api';
import { AppBarButtons } from './AppBarButtons';
import { PatientSummary } from './PatientSummary';
Expand All @@ -40,6 +44,7 @@ import { ContactTraceListView, CreateContactTraceModal } from '../ContactTrace';
import defaultPatientSchema from './DefaultPatientSchema.json';
import defaultPatientUISchema from './DefaultPatientUISchema.json';
import { VaccinationCardsListView } from '../VaccinationCard/ListView';
import { usePrescription } from '@openmsupply-client/invoices/src/Prescriptions';

const DEFAULT_SCHEMA: SchemaData = {
formSchemaId: undefined,
Expand Down Expand Up @@ -98,6 +103,12 @@ const PatientDetailView = ({
createNewPatient,
setCreateNewPatient,
} = usePatientStore();

const navigate = useNavigate();
const [searchParams] = useSearchParams();
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved
const fromPrescription =
searchParams.get('previousPath') === AppRoute.Prescription;

const patientId = usePatient.utils.id();
const { data: currentPatient, isLoading: isCurrentPatientLoading } =
usePatient.document.get(patientId);
Expand All @@ -107,8 +118,11 @@ const PatientDetailView = ({
category: { equalTo: DocumentRegistryCategoryNode.Patient },
},
});
const {
create: { create: createPrescription },
} = usePrescription();

const isLoading = isCurrentPatientLoading || isPatientRegistryLoading;
const navigate = useNavigate();

const patientRegistry = patientRegistries?.nodes[0];
const isCreatingPatient = !!createNewPatient;
Expand Down Expand Up @@ -230,6 +244,23 @@ const PatientDetailView = ({
onEdit(isDirty);
}, [isDirty, onEdit]);

useEffect(() => {
(async () => {
if (fromPrescription === true && currentPatient != null) {
const invoiceNumber = await createPrescription({
id: FnUtils.generateUUID(),
patientId: currentPatient.id,
});
navigate(
RouteBuilder.create(AppRoute.Dispensary)
.addPart(AppRoute.Prescription)
.addPart(String(invoiceNumber))
.build()
);
}
})();
}, [currentPatient]);
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved

const showSaveConfirmation = useConfirmationModal({
onConfirm: save,
message: t('messages.confirm-save-generic'),
Expand Down
1 change: 1 addition & 0 deletions client/packages/system/src/Patient/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface PatientSearchModalProps {
open: boolean;
onClose: () => void;
onChange: (name: SearchInputPatient) => void;
openPatientModal: () => void;
}

export const basicFilterOptions = {
Expand Down
Loading