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

MultiValueSetField for Medication Request Instructions #9996

Merged
merged 15 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,7 @@
"noshow": "No-show",
"not_eligible": "Not Eligible",
"not_specified": "Not Specified",
"note": "Note",
"notes": "Notes",
"notes_placeholder": "Type your Notes",
"notice_board": "Notice Board",
Expand Down Expand Up @@ -1640,6 +1641,7 @@
"priority": "Priority",
"prn_prescription": "PRN Prescription",
"prn_prescriptions": "PRN Prescriptions",
"prn_reason": "PRN Reason",
"procedure_suggestions": "Procedure Suggestions",
"procedures_select_placeholder": "Select procedures to add details",
"professional_info": "Professional Information",
Expand Down
183 changes: 183 additions & 0 deletions src/components/Questionnaire/DualValueSetSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { useQuery } from "@tanstack/react-query";
amjithtitus09 marked this conversation as resolved.
Show resolved Hide resolved
import { X } from "lucide-react";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";

import { cn } from "@/lib/utils";

import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";

import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import { Code, ValueSetSystem } from "@/types/questionnaire/code";

type TabIndex = 0 | 1;

interface DualValueSetSelectProps {
systems: [ValueSetSystem, ValueSetSystem];
values?: [Code | null, Code | null];
onSelect: (index: TabIndex, value: Code | null) => void;
placeholders?: [string, string];
disabled?: boolean;
count?: number;
searchPostFix?: string;
labels: [string, string];
}

export function DualValueSetSelect({
systems,
values = [null, null],
onSelect,
placeholders = ["Search...", "Search..."],
amjithtitus09 marked this conversation as resolved.
Show resolved Hide resolved
disabled,
count = 10,
searchPostFix = "",
labels,
}: DualValueSetSelectProps) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [activeTab, setActiveTab] = useState<TabIndex>(0);
const [search, setSearch] = useState("");

const searchQuery = useQuery({
queryKey: ["valueset", systems[activeTab], "expand", count, search],
queryFn: query.debounced(routes.valueset.expand, {
pathParams: { system: systems[activeTab] },
body: { count, search: search + searchPostFix },
}),
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add error handling for the search query.

The search query implementation should handle errors:

 const searchQuery = useQuery({
   queryKey: ["valueset", systems[activeTab], "expand", count, search],
   queryFn: query.debounced(routes.valueset.expand, {
     pathParams: { system: systems[activeTab] },
     body: { count, search: search + searchPostFix },
   }),
+  retry: 2,
+  onError: (error) => {
+    console.error('Failed to fetch value set:', error);
+    // Consider showing a user-friendly error message
+  }
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const searchQuery = useQuery({
queryKey: ["valueset", systems[activeTab], "expand", count, search],
queryFn: query.debounced(routes.valueset.expand, {
pathParams: { system: systems[activeTab] },
body: { count, search: search + searchPostFix },
}),
});
const searchQuery = useQuery({
queryKey: ["valueset", systems[activeTab], "expand", count, search],
queryFn: query.debounced(routes.valueset.expand, {
pathParams: { system: systems[activeTab] },
body: { count, search: search + searchPostFix },
}),
retry: 2,
onError: (error) => {
console.error('Failed to fetch value set:', error);
// Consider showing a user-friendly error message
}
});


useEffect(() => {
if (open) {
setSearch("");
}
}, [open, activeTab]);

const handleRemove = (index: TabIndex, e: React.MouseEvent) => {
e.stopPropagation();
onSelect(index, null);
};

const handleSelect = (value: Code) => {
onSelect(activeTab, {
code: value.code,
display: value.display || "",
system: value.system || "",
});

if (activeTab === 0) {
setActiveTab(1);
} else {
setOpen(false);
}
};

const hasValues = values.some((v) => v !== null);

return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild disabled={disabled}>
<Button
variant="outline"
role="combobox"
className={cn(
"w-full justify-start h-auto min-h-10 py-2",
"text-left",
!hasValues && "text-gray-400",
)}
>
{!hasValues ? (
<span>{placeholders[0]}</span>
) : (
<div className="flex flex-col gap-1 w-full">
{values.map(
(value, index) =>
value && (
<div
key={value.code}
className="flex items-center justify-between bg-gray-100 rounded px-2 py-1 min-w-0 cursor-pointer hover:bg-gray-200"
onClick={(e) => {
e.stopPropagation();
setActiveTab(index as TabIndex);
setOpen(true);
}}
>
<span className="text-sm truncate flex-1 mr-2">
{value.display}
</span>
<button
onClick={(e) => handleRemove(index as TabIndex, e)}
className="hover:text-gray-700 text-gray-500 shrink-0"
>
<X className="h-4 w-4" />
</button>
amjithtitus09 marked this conversation as resolved.
Show resolved Hide resolved
</div>
),
)}
</div>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-[300px] p-0" align="start">
<Tabs
value={activeTab.toString()}
onValueChange={(value) => setActiveTab(Number(value) as TabIndex)}
>
<TabsList className="w-full">
<TabsTrigger value="0" className="flex-1">
{labels[0]}
</TabsTrigger>
<TabsTrigger value="1" className="flex-1">
{labels[1]}
</TabsTrigger>
</TabsList>
<Command filter={() => 1}>
<CommandInput
placeholder={placeholders[activeTab]}
className="outline-none border-none ring-0 shadow-none"
onValueChange={setSearch}
/>
<CommandList>
<CommandEmpty>
{search.length < 3
? t("min_char_length_error", { min_length: 3 })
: searchQuery.isFetching
? t("searching")
: t("no_results_found")}
</CommandEmpty>
<CommandGroup>
{searchQuery.data?.results.map((option) => (
<CommandItem
key={option.code}
value={option.code}
onSelect={() => handleSelect(option)}
className={cn(
values[activeTab]?.code === option.code &&
"bg-primary/10 text-primary font-medium",
)}
>
<span>{option.display}</span>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</Tabs>
</PopoverContent>
</Popover>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
} from "@/components/ui/select";

import { ComboboxQuantityInput } from "@/components/Common/ComboboxQuantityInput";
import { DualValueSetSelect } from "@/components/Questionnaire/DualValueSetSelect";
import { NotesInput } from "@/components/Questionnaire/QuestionTypes/NotesInput";
import ValueSetSelect from "@/components/Questionnaire/ValueSetSelect";

import useBreakpoints from "@/hooks/useBreakpoints";
Expand Down Expand Up @@ -169,7 +171,7 @@ export function MedicationRequestQuestion({
})}
>
{/* Header - Only show on desktop */}
<div className="hidden lg:grid grid-cols-[280px,180px,170px,160px,300px,230px,180px,250px,180px,160px,48px] bg-gray-50 border-b text-sm font-medium text-gray-500">
<div className="hidden lg:grid grid-cols-[280px,180px,170px,160px,300px,180px,250px,180px,160px,200px,48px] bg-gray-50 border-b text-sm font-medium text-gray-500">
<div className="font-semibold text-gray-600 p-3 border-r">
{t("medicine")}
</div>
Expand All @@ -185,9 +187,6 @@ export function MedicationRequestQuestion({
<div className="font-semibold text-gray-600 p-3 border-r">
{t("instructions")}
</div>
<div className="font-semibold text-gray-600 p-3 border-r">
{t("additional_instructions")}
</div>
<div className="font-semibold text-gray-600 p-3 border-r">
{t("route")}
</div>
Expand All @@ -200,6 +199,9 @@ export function MedicationRequestQuestion({
<div className="font-semibold text-gray-600 p-3 border-r">
{t("intent")}
</div>
<div className="font-semibold text-gray-600 p-3 border-r">
{t("notes")}
</div>
<div className="font-semibold text-gray-600 p-3 sticky right-0 bg-gray-50 shadow-[-12px_0_15px_-4px_rgba(0,0,0,0.15)] w-12" />
</div>

Expand Down Expand Up @@ -278,7 +280,7 @@ export function MedicationRequestQuestion({
</div>
</div>
<CollapsibleContent>
<div className="p-4 space-y-4 bg-white mx-2 mb-1 rounded-md shadow-sm">
<div className="py-4 space-y-4 bg-white mx-2 mb-1">
<MedicationRequestGridRow
medication={medication}
disabled={disabled}
Expand Down Expand Up @@ -449,7 +451,7 @@ const MedicationRequestGridRow: React.FC<MedicationRequestGridRowProps> = ({
};

return (
<div className="grid grid-cols-1 lg:grid-cols-[280px,180px,170px,160px,300px,230px,180px,250px,180px,160px,48px] border-b hover:bg-gray-50/50">
<div className="grid grid-cols-1 lg:grid-cols-[280px,180px,170px,160px,300px,180px,250px,180px,160px,200px,48px] border-b hover:bg-gray-50/50">
{/* Medicine Name */}
<div className="lg:p-4 lg:px-2 lg:py-1 flex items-center justify-between lg:justify-start lg:col-span-1 lg:border-r font-medium overflow-hidden text-sm">
<span className="break-words line-clamp-2 hidden lg:block">
Expand Down Expand Up @@ -651,33 +653,47 @@ const MedicationRequestGridRow: React.FC<MedicationRequestGridRowProps> = ({
<Label className="mb-1.5 block text-sm lg:hidden">
{t("instructions")}
</Label>
<ValueSetSelect
system="system-as-needed-reason"
value={dosageInstruction?.as_needed_for}
onSelect={(reason) =>
handleUpdateDosageInstruction({ as_needed_for: reason })
}
placeholder={t("select_prn_reason")}
disabled={disabled || !dosageInstruction?.as_needed_boolean}
wrapTextForSmallScreen={true}
/>
</div>
{/* Additional Instructions */}
<div className="lg:px-2 lg:py-1 lg:border-r overflow-hidden">
<Label className="mb-1.5 block text-sm lg:hidden">
{t("additional_instructions")}
</Label>
<ValueSetSelect
system="system-additional-instruction"
value={dosageInstruction?.additional_instruction?.[0]}
onSelect={(instruction) =>
handleUpdateDosageInstruction({
additional_instruction: [instruction],
})
}
placeholder={t("select_additional_instructions")}
disabled={disabled}
/>
{dosageInstruction?.as_needed_boolean ? (
<DualValueSetSelect
systems={[
"system-as-needed-reason",
"system-additional-instruction",
]}
values={[
dosageInstruction?.as_needed_for || null,
dosageInstruction?.additional_instruction?.[0] || null,
]}
onSelect={(index, value) => {
if (index === 0) {
handleUpdateDosageInstruction({
as_needed_for: value || undefined,
});
} else {
handleUpdateDosageInstruction({
additional_instruction: value ? [value] : undefined,
});
}
}}
labels={[t("prn_reason"), t("additional_instructions")]}
placeholders={[
t("select_prn_reason"),
t("select_additional_instructions"),
]}
disabled={disabled}
/>
) : (
<ValueSetSelect
system="system-additional-instruction"
value={dosageInstruction?.additional_instruction?.[0]}
onSelect={(instruction) =>
handleUpdateDosageInstruction({
additional_instruction: instruction ? [instruction] : undefined,
})
}
placeholder={t("select_additional_instructions")}
disabled={disabled}
/>
)}
</div>
{/* Route */}
<div className="lg:px-2 lg:py-1 lg:border-r overflow-hidden">
Expand Down Expand Up @@ -739,6 +755,39 @@ const MedicationRequestGridRow: React.FC<MedicationRequestGridRowProps> = ({
</SelectContent>
</Select>
</div>
{/* Notes */}
<div className="lg:px-2 lg:py-1 lg:border-r overflow-hidden">
<Label className="mb-1.5 block text-sm lg:hidden">{t("notes")}</Label>
{desktopLayout ? (
<>
<Label className="mb-1.5 block text-sm lg:hidden">
{t("notes")}
</Label>
<Input
value={medication.note || ""}
onChange={(e) => onUpdate?.({ note: e.target.value })}
placeholder={t("add_notes")}
disabled={disabled}
className="h-9 text-sm"
/>
</>
) : (
<NotesInput
className="mt-2"
questionnaireResponse={{
question_id: "",
structured_type: "medication_request",
link_id: "",
values: [],
note: medication.note,
}}
updateQuestionnaireResponseCB={(response) => {
onUpdate?.({ note: response.note });
}}
disabled={disabled}
/>
)}
</div>
{/* Remove Button */}
<div className="hidden lg:flex lg:px-2 lg:py-1 items-center justify-center sticky right-0 bg-white shadow-[-12px_0_15px_-4px_rgba(0,0,0,0.15)] w-12">
<Button
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Encounters/PrintPrescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export const PrintPrescription = (props: {
const dosage = formatDosage(instruction);
const duration = instruction?.timing?.repeat?.bounds_duration;
const remarks = formatSig(instruction);

const notes = medication.note;
return (
<TableRow
key={medication.id}
Expand All @@ -230,6 +230,7 @@ export const PrintPrescription = (props: {
</TableCell>
<TableCell className="py-2 px-3">
{remarks || "-"}
{notes ? ` (${t("note")}: ${notes})` : ""}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Missing translations for "note" key in non-English locale files

The "note" translation key exists only in en.json but is missing in:

  • public/locale/hi.json
  • public/locale/kn.json
  • public/locale/ml.json
  • public/locale/mr.json
  • public/locale/ta.json
🔗 Analysis chain

Verify translation key exists.

The implementation looks good, but ensure the translation key "note" exists in the i18n files.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the "note" translation key exists
rg -g '*.json' '"note":\s*"' public/locale/

Length of output: 85


Script:

#!/bin/bash
# List all locale files and check for "note" key in each
fd . 'public/locale' -e json --exec-batch sh -c 'echo "=== Locale files ==="; cat "{}" | jq -r "select(.note != null) | input_filename"'

Length of output: 165


Script:

#!/bin/bash
# List all locale files and show content around "note" key
echo "=== Available locale files ==="
fd . 'public/locale' -e json
echo -e "\n=== Content check for 'note' key ==="
rg -g '*.json' -C 2 '"note":' public/locale/

Length of output: 633

</TableCell>
</TableRow>
);
Expand Down
Loading