update api request and response

This commit is contained in:
Samandar Turgunboyev
2025-12-05 17:49:55 +05:00
parent f7dbb665a0
commit 17b833dd88
23 changed files with 1155 additions and 36 deletions

View File

@@ -1,3 +1,5 @@
import { doctor_api } from "@/features/doctors/lib/api";
import { pharmacies_api } from "@/features/pharmacies/lib/api";
import { plans_api } from "@/features/plans/lib/api";
import type {
PlanCreateReq,
@@ -53,8 +55,16 @@ const AddedPlan = ({ initialValues, setDialogOpen }: Props) => {
date: initialValues ? initialValues?.date : "",
},
});
const [type, setType] = useState<"DOCTOR" | "PHARM">("DOCTOR");
const [long, setLong] = useState<number>(41.233);
const [lat, setLat] = useState<number>(63.233);
const [searchUser, setSearchUser] = useState<string>("");
const [openUser, setOpenUser] = useState<boolean>(false);
const [searchDoctor, setSearchDoctor] = useState<string>("");
const [openDoctor, setOpenDoctor] = useState<boolean>(false);
const [searchPharm, setSearchPharm] = useState<string>("");
const [openPharm, setOpenPharm] = useState<boolean>(false);
const queryClient = useQueryClient();
const [open, setOpen] = useState(false);
@@ -79,6 +89,50 @@ const AddedPlan = ({ initialValues, setDialogOpen }: Props) => {
},
});
const user_id = form.watch("user");
const { data: doctor, isLoading: isDoctorLoading } = useQuery({
queryKey: ["doctor_list", searchDoctor, user_id],
queryFn: () => {
const params: {
limit?: number;
offset?: number;
user_id?: number;
full_name?: string;
} = {
limit: 8,
full_name: searchDoctor,
user_id: Number(user_id),
};
return doctor_api.list(params);
},
select(data) {
return data.data.data;
},
});
const { data: pharm, isLoading: isPharmLoading } = useQuery({
queryKey: ["pharm_list", searchPharm, user_id],
queryFn: () => {
const params: {
limit?: number;
offset?: number;
user_id?: number;
name?: string;
} = {
limit: 8,
name: searchPharm,
user_id: Number(user_id),
};
return pharmacies_api.list(params);
},
select(data) {
return data.data.data;
},
});
const { mutate, isPending } = useMutation({
mutationFn: (body: PlanCreateReq) => plans_api.create(body),
onSuccess: () => {
@@ -117,18 +171,32 @@ const AddedPlan = ({ initialValues, setDialogOpen }: Props) => {
function onSubmit(data: z.infer<typeof createPlanFormData>) {
if (initialValues) {
edit({
id: initialValues.id,
body: {
date: formatDate.format(data.date, "YYYY-MM-DD"),
description: data.description,
extra_location: {
latitude: initialValues.latitude,
longitude: initialValues.longitude,
},
latitude: initialValues.latitude,
longitude: initialValues.longitude,
title: data.name,
},
id: initialValues.id,
});
} else {
mutate({
date: formatDate.format(data.date, "YYYY-MM-DD"),
description: data.description,
extra_location: {
latitude: lat,
longitude: long,
},
latitude: lat,
longitude: long,
title: data.name,
doctor_id: data.doctor_id ? Number(data.doctor_id) : null,
pharmacy_id: data.pharmacy_id ? Number(data.pharmacy_id) : null,
user_id: Number(data.user),
});
}
@@ -223,6 +291,213 @@ const AddedPlan = ({ initialValues, setDialogOpen }: Props) => {
);
}}
/>
<div className="flex gap-2">
<Button
type="button"
onClick={() => setType("DOCTOR")}
className={cn(
"cursor-pointer h-10 text-sm",
type === "DOCTOR"
? "bg-blue-700 hover:bg-blue-700 text-white"
: "bg-gray-300 hover:bg-gray-300 text-black/80",
)}
>
Shifokorga birlashtirish
</Button>
<Button
onClick={() => setType("PHARM")}
type="button"
className={cn(
"cursor-pointer h-10 text-sm",
type === "PHARM"
? "bg-blue-700 hover:bg-blue-700 text-white"
: "bg-gray-300 hover:bg-gray-300 text-black/80",
)}
>
Dorixonaga birlashtirish
</Button>
</div>
{type === "DOCTOR" && (
<FormField
name="doctor_id"
control={form.control}
render={({ field }) => {
const selectedUser = doctor?.results.find(
(u) => String(u.id) === field.value,
);
return (
<FormItem className="flex flex-col">
<Label className="text-md">Shifokorlar</Label>
<Popover open={openDoctor} onOpenChange={setOpenDoctor}>
<PopoverTrigger asChild disabled={initialValues !== null}>
<FormControl>
<Button
type="button"
variant="outline"
role="combobox"
aria-expanded={openDoctor}
className={cn(
"w-full h-12 justify-between",
!field.value && "text-muted-foreground",
)}
>
{selectedUser
? `${selectedUser.first_name} ${selectedUser.last_name}`
: "Shifokorni tanlang"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent
className="w-[--radix-popover-trigger-width] p-0"
align="start"
>
<Command shouldFilter={false}>
<CommandInput
placeholder="Qidirish..."
className="h-9"
value={searchDoctor}
onValueChange={setSearchDoctor}
/>
<CommandList>
{isDoctorLoading ? (
<div className="py-6 text-center text-sm">
<Loader2 className="mx-auto h-4 w-4 animate-spin" />
</div>
) : doctor && doctor.results.length > 0 ? (
<CommandGroup>
{doctor.results.map((u) => (
<CommandItem
key={u.id}
value={`${u.id}`}
onSelect={() => {
field.onChange(String(u.id));
setOpenDoctor(false);
setLat(u.latitude);
setLong(u.longitude);
form.setValue("pharmacy_id", undefined);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
field.value === String(u.id)
? "opacity-100"
: "opacity-0",
)}
/>
{u.first_name} {u.last_name}
</CommandItem>
))}
</CommandGroup>
) : (
<CommandEmpty>Shifokor topilmadi</CommandEmpty>
)}
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</FormItem>
);
}}
/>
)}
{type === "PHARM" && (
<FormField
name="pharmacy_id"
control={form.control}
render={({ field }) => {
const selectedUser = pharm?.results.find(
(u) => String(u.id) === field.value,
);
return (
<FormItem className="flex flex-col">
<Label className="text-md">Dorixonalar</Label>
<Popover open={openPharm} onOpenChange={setOpenPharm}>
<PopoverTrigger asChild disabled={initialValues !== null}>
<FormControl>
<Button
type="button"
variant="outline"
role="combobox"
aria-expanded={openPharm}
className={cn(
"w-full h-12 justify-between",
!field.value && "text-muted-foreground",
)}
>
{selectedUser
? `${selectedUser.name}`
: "Dorixonani tanlang"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent
className="w-[--radix-popover-trigger-width] p-0"
align="start"
>
<Command shouldFilter={false}>
<CommandInput
placeholder="Qidirish..."
className="h-9"
value={searchPharm}
onValueChange={setSearchPharm}
/>
<CommandList>
{isPharmLoading ? (
<div className="py-6 text-center text-sm">
<Loader2 className="mx-auto h-4 w-4 animate-spin" />
</div>
) : pharm && pharm.results.length > 0 ? (
<CommandGroup>
{pharm.results.map((u) => (
<CommandItem
key={u.id}
value={`${u.id}`}
onSelect={() => {
field.onChange(String(u.id));
setOpenPharm(false);
setLat(u.latitude);
setLong(u.longitude);
form.setValue("doctor_id", undefined);
}}
>
<Check
className={cn(
"mr-2 h-4 w-4",
field.value === String(u.id)
? "opacity-100"
: "opacity-0",
)}
/>
{u.name}
</CommandItem>
))}
</CommandGroup>
) : (
<CommandEmpty>Dorixona topilmadi</CommandEmpty>
)}
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</FormItem>
);
}}
/>
)}
<FormField
control={form.control}
@@ -307,7 +582,9 @@ const AddedPlan = ({ initialValues, setDialogOpen }: Props) => {
<Button
className="w-full h-12 text-lg rounded-lg bg-blue-600 hover:bg-blue-600 cursor-pointer"
disabled={isPending || editPending || initialValues?.is_done}
disabled={
isPending || editPending || initialValues?.comment ? true : false
}
>
{isPending || editPending ? (
<Loader2 className="animate-spin" />