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

O3-1157: Added field to search and select patient identifiers from implementer tools #380

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React, { useState, useMemo } from "react";
import uniqueId from "lodash-es/uniqueId";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the UUID library that we use site-wide?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is just for generating a unique id for the Input field.
The site-wide application is UUID version 4, afaik.

import {
usePatientIdentifierTypes,
PatientIdentifierType,
} from "./patient-identifier-type.resource";
import styles from "./uuid-search.scss";
import { useTranslation } from "react-i18next";
import {
Search,
StructuredListCell,
StructuredListRow,
StructuredListWrapper,
} from "carbon-components-react";

interface PatientIdentifierTypeSearchBoxProps {
value: string;
setPatientIdentifierTypeUuid: (patientIdentifierTypeUuid) => void;
}

export function PatientIdentifierTypeSearchBox({
setPatientIdentifierTypeUuid,
value,
}: PatientIdentifierTypeSearchBoxProps) {
const [searchTerm, setSearchTerm] = useState("");
const { data: patientIdentifierTypes, isLoading } =
usePatientIdentifierTypes();
const [activePatientIdentifierTypeUuid, setActivePatientIdentifierTypeUuid] =
useState<any>(value);
const { t } = useTranslation();

const id = useMemo(() => uniqueId(), []);

const handleUuidChange = (patientIdentifierType: PatientIdentifierType) => {
setActivePatientIdentifierTypeUuid(patientIdentifierType.uuid);
setPatientIdentifierTypeUuid(patientIdentifierType.uuid);
setSearchTerm("");
};

const handleSearchTermChange = (evt) => setSearchTerm(evt.target.value);

const filteredResults: Array<PatientIdentifierType> | undefined =
useMemo(() => {
if (!isLoading && searchTerm && searchTerm !== "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"" is considered false, so searchTerm and searchTerm !== "" is redundant

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, thanks, I'll just update it.

return patientIdentifierTypes?.filter((type) =>
type.display.toLowerCase().includes(searchTerm.toLowerCase())
);
} else {
return undefined;
}
}, [isLoading, searchTerm, patientIdentifierTypes]);

return (
<div>
{activePatientIdentifierTypeUuid && (
<p className={styles.activeUuid}>{activePatientIdentifierTypeUuid}</p>
)}
<div className={styles.autocomplete}>
<Search
id={`search-input-${id}`}
labelText=""
type="text"
size="sm"
placeholder={
!isLoading
? t(
"searchPersonAttributeHelperText",
"Person attribute type name"
)
: t("loading", "Loading")
}
onChange={handleSearchTermChange}
value={searchTerm}
disabled={isLoading}
/>
{searchTerm ? (
filteredResults?.length ? (
<StructuredListWrapper
selection
className={styles.listbox}
id={`searchbox-${id}`}
>
{filteredResults?.map((patientIdentifierType) => (
<StructuredListRow
key={patientIdentifierType.uuid}
role="option"
onClick={() => {
handleUuidChange(patientIdentifierType);
}}
aria-selected="true"
>
<StructuredListCell className={styles.smallListCell}>
{patientIdentifierType.display}
</StructuredListCell>
</StructuredListRow>
))}
</StructuredListWrapper>
) : (
<p className={styles.bodyShort01}>
{t("noPersonAttributeFoundText", "No matching results found")}
</p>
)
) : null}
</div>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import useSWR from "swr";
import { FetchResponse, openmrsFetch } from "@openmrs/esm-framework";
import { useMemo } from "react";

export interface PatientIdentifierType {
uuid: string;
display: string;
}

interface PatientIdentifierTypeResponse {
results: Array<PatientIdentifierType>;
}

export function usePatientIdentifierTypes(): {
data: Array<PatientIdentifierType> | undefined;
isLoading: boolean;
} {
const { data, error } = useSWR<
FetchResponse<PatientIdentifierTypeResponse>,
Error
>(`/ws/rest/v1/patientidentifiertype`, openmrsFetch);
const memoisedPatientIdentifierTypeData = useMemo(
() => ({
data: data?.data?.results,
isLoading: !data && !error,
}),
[data, error]
);

return memoisedPatientIdentifierTypeData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ExtensionSlotRemove } from "./extension-slot-remove";
import { ObjectEditor } from "./object-editor";
import { ExtensionSlotOrder } from "./extension-slot-order";
import { PersonAttributeTypeSearchBox } from "./person-attribute-search";
import { PatientIdentifierTypeSearchBox } from "./patient-identifier-type-search";

export interface ValueEditorFieldProps {
element: ConfigValueDescriptor;
Expand Down Expand Up @@ -57,6 +58,11 @@ export function ValueEditorField({
onChange(personAttributeTypeUuid)
}
/>
) : valueType === Type.PatientIdentifierTypeUuid ? (
<PatientIdentifierTypeSearchBox
value={value}
setPatientIdentifierTypeUuid={(uuid) => onChange(uuid)}
/>
) : valueType === Type.Number ? (
<NumberInput
id={id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ function checkType(keyPath: string, _type: Type | undefined, value: any) {
String: isString,
UUID: isUuid,
PersonAttributeTypeUuid: isUuid,
PatientIdentifierTypeUuid: isUuid,
};
runValidators(keyPath, [validator[_type]], value);
}
Expand Down
1 change: 1 addition & 0 deletions packages/framework/esm-config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum Type {
String = "String",
UUID = "UUID",
PersonAttributeTypeUuid = "PersonAttributeTypeUuid",
PatientIdentifierTypeUuid = "PatientIdentifierTypeUuid",
}

// Full-powered typing for Config and Schema trees depends on being able to
Expand Down