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

Merged
merged 23 commits into from
Feb 6, 2025
Merged
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
21 changes: 11 additions & 10 deletions src/components/ui/autocomplete.tsx
Original file line number Diff line number Diff line change
@@ -49,27 +49,28 @@ 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)}
>
<span className="overflow-hidden">
{value
? options.find((option) => option.value === value)?.label
: placeholder}
<span
className={cn(
"truncate",
!selectedOption && "text-muted-foreground",
Copy link
Member

Choose a reason for hiding this comment

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

are you sure this is a valid tailwind class?

)}
>
{selectedOption ? selectedOption.label : placeholder}
</span>
<CaretSortIcon className="ml-2 size-4 shrink-0 opacity-50" />
</Button>
@@ -98,7 +99,7 @@ export default function Autocomplete({
(option) =>
option.label.toLowerCase() === v.toLowerCase(),
)?.value || "";
onChange(currentValue === value ? "" : currentValue);
onChange(currentValue);
setOpen(false);
}}
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);
}

>
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";
@@ -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);
};

@@ -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");
Copy link
Member

Choose a reason for hiding this comment

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

please check all translations

};

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic is what should be in a function called handleEdit 👍

Separate out the concerns, code readability is important (check GovtOrganizationSelector for example for similar logic).

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")}
Copy link
Member

Choose a reason for hiding this comment

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

refer translations guidelines in README. this is not how i18n should be done.

</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>
</>
);