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

Fix: Facility Organization Dropdown #10104

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@
"has_allergies": "Has Allergies",
"has_domestic_healthcare_support": "Has domestic healthcare support?",
"has_sari": "Has SARI (Severe Acute Respiratory illness)?",
"has_sub_departments": "Has sub-departments",
"health-profile": "Health Profile",
"health_facility__config_registration_error": "Health ID registration failed",
"health_facility__config_update_error": "Health Facility config update failed",
Expand Down Expand Up @@ -1459,6 +1460,7 @@
"normal": "Normal",
"noshow": "No-show",
"not_eligible": "Not Eligible",
"not_found": "Not Found",
"not_specified": "Not Specified",
"not_taken": "Not Taken",
"note": "Note",
Expand Down Expand Up @@ -1687,6 +1689,7 @@
"prn_reason": "PRN Reason",
"procedure_suggestions": "Procedure Suggestions",
"procedures_select_placeholder": "Select procedures to add details",
"proceed": "Proceed",
"professional_info": "Professional Information",
"professional_info_note": "View or update user's professional information",
"professional_info_note_self": "View or update your professional information",
Expand Down Expand Up @@ -1925,6 +1928,7 @@
"select_site": "Select site",
"select_skills": "Select and add some skills",
"select_status": "Select Status",
"select_sub_department": "Select sub-department",
"select_time": "Select time",
"select_time_slot": "Select time slot",
"select_user": "Select user",
Expand Down Expand Up @@ -2011,6 +2015,7 @@
"start_time": "Start Time",
"start_time_must_be_before_end_time": "Start time must be before end time",
"state": "State",
"state_reason_for_archiving": "State reason for archiving <strong>{{name}}</strong> file?",
"status": "Status",
"stop": "Stop",
"stop_recording": "Stop Recording",
Expand Down
16 changes: 6 additions & 10 deletions src/components/ui/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,23 @@ export default function Autocomplete({
"data-cy": dataCy,
}: AutocompleteProps) {
const [open, setOpen] = React.useState(false);
const selectedOption = options.find((option) => option.value === value);

return (
<Popover open={open} onOpenChange={setOpen} modal={true}>
<PopoverTrigger asChild className={popoverClassName}>
<Button
title={
value
? options.find((option) => option.value === value)?.label
: undefined
}
title={selectedOption?.label}
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
disabled={disabled}
data-cy={dataCy}
onClick={() => setOpen(true)}
Comment on lines +58 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix dropdown toggle behavior.

The current onClick implementation doesn't properly handle the toggle case, which explains the reported issue where "dropdown closes when clicked with a value selected". The dropdown should toggle between open/closed states.

Apply this fix:

-  onClick={() => setOpen(true)}
+  onClick={() => setOpen(!open)}
📝 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
title={selectedOption?.label}
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
disabled={disabled}
data-cy={dataCy}
onClick={() => setOpen(true)}
title={selectedOption?.label}
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
disabled={disabled}
data-cy={dataCy}
onClick={() => setOpen(!open)}

>
<span className="overflow-hidden">
{value
? options.find((option) => option.value === value)?.label
: placeholder}
<span className={cn("truncate", !selectedOption && "text-gray-500")}>
{selectedOption ? selectedOption.label : placeholder}
</span>
<CaretSortIcon className="ml-2 size-4 shrink-0 opacity-50" />
</Button>
Expand Down Expand Up @@ -98,7 +94,7 @@ export default function Autocomplete({
(option) =>
option.label.toLowerCase() === v.toLowerCase(),
)?.value || "";
onChange(currentValue === value ? "" : currentValue);
onChange(currentValue);
setOpen(false);
}}
Comment on lines +97 to 99
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Optimize onSelect handler to prevent unnecessary updates.

The current implementation doesn't check if the selected value is different from the current value, which could lead to unnecessary re-renders and might contribute to the reported dropdown behavior issues.

Apply this optimization:

-  onChange(currentValue);
-  setOpen(false);
+  if (currentValue !== value) {
+    onChange(currentValue);
+    setOpen(false);
+  }
📝 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
onChange(currentValue);
setOpen(false);
}}
if (currentValue !== value) {
onChange(currentValue);
setOpen(false);
}

>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useQuery } from "@tanstack/react-query";
import { Building } from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";

import CareIcon from "@/CAREUI/icons/CareIcon";

import Autocomplete from "@/components/ui/autocomplete";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";

import routes from "@/Utils/request/api";
Expand Down Expand Up @@ -79,8 +78,6 @@ export default function FacilityOrganizationSelector(
newLevels.push(selectedOrg);
setSelectedLevels(newLevels);
setSelectedOrganization(selectedOrg);

// Always update the selected value, regardless of children
onChange(selectedOrg.id);
};

Expand All @@ -96,100 +93,89 @@ export default function FacilityOrganizationSelector(
};

const handleEdit = (level: number) => {
const newLevels = selectedLevels.slice(0, level);
setSelectedLevels(newLevels);
if (newLevels.length > 0) {
const lastOrg = newLevels[newLevels.length - 1];
setSelectedOrganization(lastOrg);
onChange(lastOrg.id);
} else {
setSelectedOrganization(null);
}
const orgList =
level === 0
? getAllOrganizations?.results
: currentLevelOrganizations?.results;

const getDropdownLabel = () => {
if (level < selectedLevels.length) {
return selectedLevels[level].name;
}
return level === 0 ? t("select_department") : t("select_sub_department");
};

return (
<div className="group flex items-center gap-1.5">
{level > 0 && (
<CareIcon
icon="l-arrow-right"
className="h-3.5 w-3.5 text-gray-400 flex-shrink-0"
/>
)}
<div className="flex-1 flex items-center gap-2">
<div className="flex-1">
<Autocomplete
value={selectedLevels[level]?.id}
options={getOrganizationOptions(orgList)}
onChange={(value) => handleLevelChange(value, level)}
placeholder={getDropdownLabel()}
/>
</div>
{level > 0 && level < selectedLevels.length && (
<div
className="cursor-pointer p-1 hover:bg-gray-100 rounded-sm opacity-0 group-hover:opacity-100 transition-opacity"
onClick={() => {
const newLevels = selectedLevels.slice(0, level);
setSelectedLevels(newLevels);
if (newLevels.length > 0) {
const lastOrg = newLevels[newLevels.length - 1];
setSelectedOrganization(lastOrg);
onChange(lastOrg.id);
} else {
setSelectedOrganization(null);
onChange("");
}
}}
>
<CareIcon icon="l-pen" className="h-4 w-4 text-gray-500" />
</div>
)}
</div>
</div>
);
};

return (
<>
<Label className="mb-2">
<Label className="mb-2 block">
{t("select_department")}
{required && <span className="text-red-500">*</span>}
</Label>
<div className="space-y-4">
{/* Selected Organization Display */}
<div className="space-y-3">
{selectedOrganization && (
<div className="rounded-md border p-3 bg-gray-50">
<div className="flex items-center justify-between">
<div>
<p className="font-medium">{selectedOrganization.name}</p>
{selectedOrganization.has_children && (
<p className="text-sm text-gray-500">
You can select a sub-department or keep this selection
</p>
)}
</div>
<div className="flex items-center gap-3 rounded-md border border-sky-100 bg-sky-50/50 p-2.5">
<Building className="h-4 w-4 text-sky-600 flex-shrink-0" />
<div className="flex-1 min-w-0">
<p className="font-medium text-sm text-sky-900 truncate">
{selectedOrganization.name}
</p>
{selectedOrganization.has_children && (
<Badge variant="outline">Has Sub-departments</Badge>
<p className="text-xs text-sky-600">
{t("has_sub_departments")}
</p>
)}
</div>
</div>
)}

{/* Organization Hierarchy */}
<div className="space-y-2">
<div className="space-y-1.5">
{selectedLevels.map((org, index) => (
<div key={org.id} className="flex items-center gap-2">
{index > 0 && (
<CareIcon
icon="l-arrow-right"
className="h-4 w-4 text-gray-400"
/>
)}
<div className="flex-1">
<div className="flex gap-2">
<div className="flex items-center h-9 w-full rounded-md border border-gray-200 bg-white px-3 py-1 text-base shadow-sm">
{org.name}
</div>
<Button
variant="ghost"
size="icon"
onClick={() => handleEdit(index)}
type="button"
>
<CareIcon icon="l-pen" className="h-4 w-4" />
</Button>
</div>
</div>
</div>
<div key={org.id}>{handleEdit(index)}</div>
))}
{(!selectedLevels.length ||
selectedLevels[selectedLevels.length - 1]?.has_children) &&
handleEdit(selectedLevels.length)}
</div>

{/* Next Selection */}
{(!selectedLevels.length ||
selectedLevels[selectedLevels.length - 1]?.has_children) && (
<div className="flex items-center gap-2">
{selectedLevels.length > 0 && (
<CareIcon
icon="l-arrow-right"
className="h-4 w-4 text-gray-400"
/>
)}
<div className="flex-1">
<Autocomplete
value=""
options={getOrganizationOptions(
selectedLevels.length === 0
? getAllOrganizations?.results
: currentLevelOrganizations?.results,
)}
onChange={(value: string) =>
handleLevelChange(value, selectedLevels.length)
}
placeholder={`Select ${
selectedLevels.length ? "sub-department" : "Department"
}...`}
/>
</div>
</div>
)}
</div>
</>
);
Expand Down
Loading