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
Merged
3 changes: 2 additions & 1 deletion client/packages/common/src/intl/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"button.create-a-new-one": "Create a new one",
"button.create-log-reason": "Create log reason",
"button.create-new": "Create new",
"button.create-new-patient": "Create new patient",
"button.create-shipment": "Create Shipment",
"button.dashboard": "Dashboard",
"button.delete": "Delete",
Expand Down Expand Up @@ -1761,4 +1762,4 @@
"warning.caps-lock": "Warning: Caps lock is on",
"warning.field-not-parsed": "{{field}} not parsed",
"warning.nothing-to-supply": "Nothing left to supply!"
}
}
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 Down Expand Up @@ -65,6 +68,16 @@ const PatientSearchComponent: FC<PatientSearchModalProps> = ({
noOptionsText=""
/>
</Box>
<Box p={2}>
<ButtonWithIcon
Icon={<PlusCircleIcon />}
label={t('button.create-new-patient')}
onClick={() => {
handleClose();
openPatientModal();
}}
/>
</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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Box,
DataTable,
DownloadIcon,
FnUtils,
GenderType,
HomeIcon,
InfoTooltipIcon,
Expand All @@ -12,14 +13,17 @@ import {
noOtherVariants,
useColumns,
useFormatDateTime,
useLocation,
useNavigate,
useTranslation,
} from '@openmsupply-client/common';
import { PatientPanel } from './PatientPanel';
import { FetchPatientModal } from './FetchPatientModal';
import { usePatient } from '../api';
import { Gender, usePatientStore } from '@openmsupply-client/programs';
import { CentralPatientSearchResponse } from '../api/api';
import { AppRoute } from '@openmsupply-client/config';
import { Gender, usePatientStore } from '@openmsupply-client/programs';
import { usePrescription } from '@openmsupply-client/invoices/src/Prescriptions';

const genderToGenderType = (gender: Gender): GenderType => {
switch (gender) {
Expand Down Expand Up @@ -113,7 +117,11 @@ export const PatientResultsTab: FC<PatientPanel & { active: boolean }> = ({
const { setCreateNewPatient } = usePatientStore();
const t = useTranslation();
const navigate = useNavigate();
const location = useLocation();
const { localisedDate } = useFormatDateTime();
const {
create: { create: createPrescription },
} = usePrescription();

const columns = useColumns<PatientColumnData>([
{
Expand Down Expand Up @@ -185,6 +193,28 @@ export const PatientResultsTab: FC<PatientPanel & { active: boolean }> = ({
setFetchingPatient(undefined);
}, [search, searchParams]);

const handleRowClick = async (row: PatientColumnData): Promise<void> => {
const urlSegments = location.pathname.split('/');

if (row.isOnCentral) {
setFetchingPatient(row);
return;
}

setCreateNewPatient(undefined);

if (urlSegments.includes(AppRoute.Prescription)) {
const invoiceNumber = await createPrescription({
id: FnUtils.generateUUID(),
patientId: String(row.id),
});
navigate(String(invoiceNumber));
return;
}

navigate(String(row.id));
};

if (!active) {
return null;
}
Expand Down Expand Up @@ -241,14 +271,7 @@ export const PatientResultsTab: FC<PatientPanel & { active: boolean }> = ({
data={data}
columns={columns}
noDataMessage={t('messages.no-matching-patients')}
onRowClick={row => {
if (row.isOnCentral) {
setFetchingPatient(row);
} else {
setCreateNewPatient(undefined);
navigate(String(row.id));
}
}}
onRowClick={handleRowClick}
generateRowTooltip={({ firstName, lastName, isOnCentral }) => {
if (isOnCentral) {
return t('messages.click-to-fetch');
Expand Down
29 changes: 28 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,
RouteBuilder,
FnUtils,
useUrlQuery,
} 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,11 @@ const PatientDetailView = ({
createNewPatient,
setCreateNewPatient,
} = usePatientStore();

const navigate = useNavigate();
const { urlQuery } = useUrlQuery();
const fromPrescription = urlQuery['previousPath'] === AppRoute.Prescription;

const patientId = usePatient.utils.id();
const { data: currentPatient, isLoading: isCurrentPatientLoading } =
usePatient.document.get(patientId);
Expand All @@ -107,8 +117,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 @@ -218,6 +231,20 @@ const PatientDetailView = ({
if (savedDocument) {
setDocumentName(savedDocument.name);
}
// Creates a new prescription and redirects to the prescriptions page
// if the patient was created from there.
if (fromPrescription === true) {
GeronimoJohn marked this conversation as resolved.
Show resolved Hide resolved
const invoiceNumber = await createPrescription({
id: FnUtils.generateUUID(),
patientId,
});
navigate(
RouteBuilder.create(AppRoute.Dispensary)
.addPart(AppRoute.Prescription)
.addPart(String(invoiceNumber))
.build()
);
}
}, [saveData, setCreateNewPatient, setDocumentName]);

useEffect(() => {
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