Files
nobat724_front/components/doctors/modal/ButtonApply.js
T

95 lines
2.3 KiB
JavaScript

import { Button } from "@mui/material";
import specialties from "@/data/specialties.json";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { request } from "@/services/response";
function ButtonApply({
data,
setConfirmedData,
matchedCity,
matchedState,
setOpen,
setDoctors,
}) {
const router = useRouter();
const [loading, setLoading] = useState(false);
const sendReq = () => {
setLoading(true);
const params = new URLSearchParams(window.location.search);
const specialty = params.get("specialty");
const gender = params.get("gender");
const degree = params.get("degree");
const turn = params.get("turn");
const requestData = {
specialty: specialty === "null" ? null : specialty,
gender,
degree,
turn,
city: matchedCity?.id || null,
state: matchedState?.id || null,
};
request
.getDoctors(requestData)
.then((res) => {
if (res && res.data) setDoctors(res.data);
})
.catch(() => {})
.finally(() => {
setOpen(false);
setLoading(false);
});
};
const applyFilters = () => {
const current = new URLSearchParams();
const mappings = [
{
key: "specialty",
getValue: () => {
if (data.category) {
const children = specialties.filter(
(item) => item.parent === data.category.id
);
return children.length > 0 ? children[0].id : data.category.id;
}
return null;
},
},
{ key: "gender", getValue: () => data.gender || null },
{ key: "degree", getValue: () => data.degree || null },
{ key: "turn", getValue: () => data.turn },
];
mappings.forEach(({ key, getValue }) => {
const value = getValue();
current.set(key, value);
});
const queryString = current.toString();
setConfirmedData(data);
sendReq();
router.push(`/doctors?${queryString}`);
};
return (
<div className="flex justify-end w-full">
<Button
loading={loading}
variant="contained"
onClick={applyFilters}
className="!px-3 !py-2 !text-[16px] !font-medium"
>
اعمال تغییرات
</Button>
</div>
);
}
export default ButtonApply;