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

Support For BULK beds creation in location #10815

Open
wants to merge 44 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
b7b861a
bulk beds creation
AdityaJ2305 Feb 25, 2025
f511fe0
a few changes
AdityaJ2305 Feb 25, 2025
672f64b
a few changes number type
AdityaJ2305 Feb 25, 2025
1740aad
merge develop
AdityaJ2305 Feb 25, 2025
a132d72
move out schema
AdityaJ2305 Feb 25, 2025
00e1dd6
move out schema
AdityaJ2305 Feb 25, 2025
ae84991
unwanted changes
AdityaJ2305 Feb 25, 2025
537c806
on error notify
AdityaJ2305 Feb 25, 2025
0eb4f45
typo fix
AdityaJ2305 Feb 25, 2025
bbf4206
typo fix
AdityaJ2305 Feb 25, 2025
ba1cead
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 26, 2025
74c7579
shared data and rm preview
AdityaJ2305 Feb 26, 2025
8e9b796
Merge branch 'bulk_beds_creation' of https://github.com/AdityaJ2305/c…
AdityaJ2305 Feb 26, 2025
7648bf4
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 27, 2025
58621f8
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 27, 2025
46c6c5f
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 27, 2025
3039f72
ref id updated
AdityaJ2305 Feb 27, 2025
74a8924
multiple input for beds
AdityaJ2305 Feb 27, 2025
3caf90c
hide inputs on empty name
AdityaJ2305 Feb 27, 2025
d53c120
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 27, 2025
181faa1
name trim space
AdityaJ2305 Feb 27, 2025
695dd54
Merge branch 'bulk_beds_creation' of https://github.com/AdityaJ2305/c…
AdityaJ2305 Feb 27, 2025
71c9642
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 27, 2025
f9becac
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 28, 2025
5346206
add accordion and ux update to support bulk beds creation
AdityaJ2305 Feb 28, 2025
97ef52c
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 28, 2025
cd7120a
full width when checkbox is hidden
AdityaJ2305 Feb 28, 2025
7ae7bfa
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Feb 28, 2025
7bda0ad
typo fix
AdityaJ2305 Mar 1, 2025
ec69f5f
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 1, 2025
9447a6d
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 3, 2025
509ec9f
rm onError
AdityaJ2305 Mar 3, 2025
7db4ee0
use useFieldArray
AdityaJ2305 Mar 3, 2025
bf75218
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 3, 2025
f20a3be
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 3, 2025
078150d
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 4, 2025
8172195
bed names updating on changing main name input
AdityaJ2305 Mar 4, 2025
cc1ad46
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 5, 2025
6de7dd9
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 6, 2025
305e1bf
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 6, 2025
a9ab2a2
minor css change
AdityaJ2305 Mar 6, 2025
68a1dc1
Merge branch 'develop' into bulk_beds_creation
AdityaJ2305 Mar 9, 2025
4d95cb8
rm accordion
AdityaJ2305 Mar 9, 2025
fa64ec4
undo lock file
AdityaJ2305 Mar 9, 2025
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
3 changes: 3 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,10 @@
"basic_info": "Basic Information",
"basic_information": "Basic Information",
"bed_capacity": "Bed Capacity",
"bed_count_validation_error": "Number of beds must be greater than 0",
"bed_created_notification_one": "{{count}} Bed created successfully",
"bed_created_notification_other": "{{count}} Beds created successfully",
"beds_creation_failed":"Failed to create beds",
"bed_history": "Bed History",
"bed_not_linked_to_camera": "This bed has not been linked to this camera.",
"bed_search_placeholder": "Search by beds name",
Expand Down Expand Up @@ -2293,6 +2295,7 @@
"tomorrow": "Tomorrow",
"total_amount": "Total Amount",
"total_beds": "Total Beds",
"total_number_of_beds": "Total number of beds",
"total_patients": "Total Patients",
"total_slots": "Total Slots",
"total_staff": "Total Staff",
Expand Down
101 changes: 91 additions & 10 deletions src/pages/Facility/settings/locations/LocationForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { t } from "i18next";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
Expand All @@ -25,6 +26,7 @@ import {
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";

import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import query from "@/Utils/request/query";
import {
Expand All @@ -34,20 +36,27 @@ import {
Status,
} from "@/types/location/location";
import locationApi from "@/types/location/locationApi";
import {
BatchRequestBody,
BatchSubmissionResult,
} from "@/types/questionnaire/batch";

const formSchema = z.object({
name: z.string().min(1, { message: "Name is required" }),
name: z.string().min(1, { message: t("field_required") }),
description: z.string().optional(),
status: z.enum(["active", "inactive", "unknown"] as const),
operational_status: z.enum(["C", "H", "O", "U", "K", "I"] as const),
form: z.enum(LocationFormOptions),
parent: z.string().optional().nullable(),
beds_count: z
.number()
.min(1, t("number_min_error", { min: 1 }))
.optional(),
organizations: z.array(z.string()).default([]),
availability_status: z.enum(["available", "unavailable"] as const),
});

type FormValues = z.infer<typeof formSchema>;

interface Props {
facilityId: string;
onSuccess?: () => void;
Expand All @@ -61,11 +70,11 @@ const defaultValues: FormValues = {
status: "active",
operational_status: "O",
form: "ro",
beds_count: 1,
parent: null,
organizations: [],
availability_status: "available",
};

export default function LocationForm({
facilityId,
onSuccess,
Expand All @@ -83,6 +92,8 @@ export default function LocationForm({
enabled: !!locationId,
});

const isEditMode = !!location?.id;

const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
Expand Down Expand Up @@ -115,20 +126,58 @@ export default function LocationForm({
pathParams: { facility_id: facilityId },
}),
onSuccess: () => {
toast.success(isEditMode ? t("location_updated") : t("location_created"));
queryClient.invalidateQueries({ queryKey: ["locations"] });

onSuccess?.();
},
});

const { mutate: submitBatch } = useMutation({
mutationFn: mutate(routes.batchRequest, { silent: true }),
onSuccess: (data: { results: BatchSubmissionResult[] }) => {
toast.success(
location?.id ? t("location_updated") : t("location_created"),
t("bed_created_notification", { count: data.results.length }),
);
queryClient.invalidateQueries({ queryKey: ["locations"] });

onSuccess?.();
},

onError: () => {
toast.error(t("beds_creation_failed"));
},
Copy link
Member

Choose a reason for hiding this comment

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

is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so, should I remove it ?

Copy link
Member

Choose a reason for hiding this comment

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

wouldn't the global error handler handle it? 🤔

});

function onSubmit(values: FormValues) {
if (values.form === "bd" && !isEditMode && (values.beds_count ?? 0) > 1) {
const data: LocationWrite = {
...values,
mode: "instance",
description: values.description || "",
organizations: values.organizations,
parent: values.parent || undefined,
};

const batchRequest: BatchRequestBody = {
requests: Array.from(
{ length: Number(values.beds_count) },
(_, index) => ({
url: `/api/v1/facility/${facilityId}/location/`,
method: "POST",
reference_id: `Location`,
body: {
...data,
name: `${values.name} ${index + 1}`,
},
}),
),
};
submitBatch(batchRequest);
return;
}
const locationData: LocationWrite = {
...values,
// Mode = instance only for beds
mode: values.form === "bd" ? "instance" : "kind",
mode: "kind",
description: values.description || "",
organizations: values.organizations,
parent: values.parent || undefined,
Expand Down Expand Up @@ -179,7 +228,20 @@ export default function LocationForm({
</FormItem>
)}
/>

{form.watch("form") === "bd" &&
(form.watch("beds_count") ?? 0) > 1 &&
form.watch("name")?.trim() !== "" && (
<span className="text-sm text-gray-500">
{Array.from(
{ length: Number(form.watch("beds_count")) },
(_, index) => (
<span key={index}>
{form.watch("name")}-{index + 1}{" "}
</span>
),
)}
</span>
)}
<FormField
control={form.control}
name="description"
Expand Down Expand Up @@ -224,6 +286,25 @@ export default function LocationForm({
)}
/>

{form.watch("form") === "bd" && !isEditMode && (
<FormField
control={form.control}
name="beds_count"
render={({ field }) => (
<FormItem>
<FormLabel>{t("total_number_of_beds")}</FormLabel>
<Input
{...field}
type="number"
min={1}
onChange={(e) => field.onChange(e.target.valueAsNumber)}
/>
<FormMessage />
</FormItem>
)}
/>
)}

<FormField
control={form.control}
name="status"
Expand Down Expand Up @@ -277,9 +358,9 @@ export default function LocationForm({

<Button type="submit" disabled={isPending}>
{isPending ? (
<>{location?.id ? t("updating") : t("creating")}</>
<>{isEditMode ? t("updating") : t("creating")}</>
) : (
<>{location?.id ? t("update") : t("create")}</>
<>{isEditMode ? t("update") : t("create")}</>
)}
</Button>
</form>
Expand Down
Loading