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

Replaced the older components with ShadCN components in the ResourceCreate Page #10367

1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,7 @@
"start_review": "Start Review",
"start_time": "Start Time",
"start_time_must_be_before_end_time": "Start time must be before end time",
"start_typing_to_search": "Start typing to search...",
"state": "State",
"state_reason_for_archiving": "State reason for archiving <strong>{{name}}</strong> file?",
"status": "Status",
Expand Down
108 changes: 0 additions & 108 deletions src/components/Common/FacilitySelect.tsx

This file was deleted.

42 changes: 36 additions & 6 deletions src/components/Resource/ResourceCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Card from "@/CAREUI/display/Card";
import CareIcon from "@/CAREUI/icons/CareIcon";

import { Alert, AlertDescription } from "@/components/ui/alert";
import Autocomplete from "@/components/ui/autocomplete";
import { Button } from "@/components/ui/button";
import {
Form,
Expand All @@ -34,7 +35,6 @@ import {
import { Separator } from "@/components/ui/separator";
import { Textarea } from "@/components/ui/textarea";

import { FacilitySelect } from "@/components/Common/FacilitySelect";
import Loading from "@/components/Common/Loading";
import Page from "@/components/Common/Page";

Expand All @@ -46,7 +46,10 @@ import { RESOURCE_CATEGORY_CHOICES } from "@/common/constants";
import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import request from "@/Utils/request/request";
import { PaginatedResponse } from "@/Utils/request/types";
import validators from "@/Utils/validators";
import { FacilityData } from "@/types/facility/facility";
import facilityApi from "@/types/facility/facilityApi";
import { CreateResourceRequest } from "@/types/resourceRequest/resourceRequest";

interface ResourceProps {
Expand All @@ -57,6 +60,7 @@ export default function ResourceCreate(props: ResourceProps) {
const { goBack } = useAppHistory();
const { facilityId } = props;
const { t } = useTranslation();
const [qParams, _] = useQueryParams();
const [isLoading, setIsLoading] = useState(false);
const [{ related_patient }] = useQueryParams();
const authUser = useAuthUser();
Expand Down Expand Up @@ -90,6 +94,26 @@ export default function ResourceCreate(props: ResourceProps) {
enabled: !!facilityId,
});

const { data: facilitiesResponse } = useQuery<
PaginatedResponse<FacilityData>
>({
queryKey: ["facilities", qParams],
queryFn: query.debounced(facilityApi.getAllFacilities, {
queryParams: {
name: qParams.name,
...(qParams.facility_type && { facility_type: qParams.facility_type }),
...(qParams.organization && {
organization: qParams.organization,
}),
},
}),
});

const facilityOptions = facilitiesResponse?.results.map((facility) => ({
label: facility.name,
value: facility.id,
}));

const form = useForm<ResourceFormValues>({
resolver: zodResolver(resourceFormSchema),
defaultValues: {
Expand Down Expand Up @@ -199,11 +223,17 @@ export default function ResourceCreate(props: ResourceProps) {
{t("facility_for_care_support")}
</FormLabel>
<FormControl>
<FacilitySelect
multiple={false}
name="assigned_facility"
selected={field.value}
setSelected={field.onChange}
<Autocomplete
options={facilityOptions ?? []}
value={field.value?.id ?? ""}
placeholder={t("start_typing_to_search")}
onChange={(value) => {
const facility =
facilitiesResponse?.results.find(
(f) => f.id === value,
) ?? null;
form.setValue("assigned_facility", facility);
}}
/>
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 facility selection.

The facility lookup in onChange could be optimized and should include error handling:

  <Autocomplete
    options={facilityOptions ?? []}
    value={field.value?.id ?? ""}
    placeholder={t("start_typing_to_search")}
    onChange={(value) => {
+     if (!value) {
+       form.setValue("assigned_facility", null);
+       return;
+     }
      const facility =
        facilitiesResponse?.results.find(
          (f) => f.id === value,
        ) ?? null;
+     if (!facility) {
+       console.error("Selected facility not found in results");
+       return;
+     }
      form.setValue("assigned_facility", facility);
    }}
  />
📝 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
<Autocomplete
options={facilityOptions ?? []}
value={field.value?.id ?? ""}
placeholder={t("start_typing_to_search")}
onChange={(value) => {
const facility =
facilitiesResponse?.results.find(
(f) => f.id === value,
) ?? null;
form.setValue("assigned_facility", facility);
}}
/>
<Autocomplete
options={facilityOptions ?? []}
value={field.value?.id ?? ""}
placeholder={t("start_typing_to_search")}
onChange={(value) => {
if (!value) {
form.setValue("assigned_facility", null);
return;
}
const facility =
facilitiesResponse?.results.find(
(f) => f.id === value,
) ?? null;
if (!facility) {
console.error("Selected facility not found in results");
return;
}
form.setValue("assigned_facility", facility);
}}
/>

</FormControl>
<FormDescription>
Expand Down
Loading