clear search change to doctor name
This commit is contained in:
@@ -56,7 +56,7 @@ function AutoComplete({
|
||||
return (
|
||||
<Autocomplete
|
||||
freeSolo
|
||||
options={data}
|
||||
options={Array.isArray(data) ? data : []} // ✅ همیشه آرایه
|
||||
getOptionLabel={(option) =>
|
||||
typeof option === "string" ? option : option.name
|
||||
}
|
||||
@@ -65,11 +65,6 @@ function AutoComplete({
|
||||
className="!w-full"
|
||||
onInputChange={onInputChange}
|
||||
onChange={onChange}
|
||||
renderOption={(props, option) => (
|
||||
<li {...props} key={option.id}>
|
||||
{option.name}
|
||||
</li>
|
||||
)}
|
||||
sx={{
|
||||
"& .MuiAutocomplete-input": {
|
||||
background: noneBg ? "" : "#ffffff !important",
|
||||
|
||||
@@ -1,79 +1,61 @@
|
||||
"use client";
|
||||
|
||||
import { clearFilterParams } from "@/helper";
|
||||
import { useRouter } from "next/navigation";
|
||||
import specialties from "@/data/specialties.json";
|
||||
import { useRef } from "react";
|
||||
import AutoComplete from "./AutoComplete";
|
||||
|
||||
function SearchField({ filter, setFilter, setDataInURL }) {
|
||||
function SearchField({ filter, setFilter, setDataInURL, sendReq }) {
|
||||
const router = useRouter();
|
||||
const typingTimeoutRef = useRef(null);
|
||||
|
||||
// پاک کردن ورودی
|
||||
const clearText = () => {
|
||||
clearFilterParams(router, ["name", "category", "specialty"]);
|
||||
const newFilter = {
|
||||
...filter,
|
||||
...{
|
||||
name: "",
|
||||
category: undefined,
|
||||
specialty: undefined,
|
||||
},
|
||||
};
|
||||
clearFilterParams(router, ["name"]);
|
||||
const newFilter = { ...filter, name: "" };
|
||||
setFilter(newFilter);
|
||||
setDataInURL(newFilter);
|
||||
sendReq(newFilter); // درخواست مجدد بدون فیلتر
|
||||
};
|
||||
|
||||
const handleSearch = (e) => {
|
||||
const findName = specialties.find((item) => item.name === e);
|
||||
// جستجو فقط با نام پزشک
|
||||
const handleSearch = (value) => {
|
||||
const searchParams = new URLSearchParams(window.location.search);
|
||||
let dataUrl = {};
|
||||
|
||||
if (findName) {
|
||||
let newFilter = { ...filter };
|
||||
if (findName.parent) {
|
||||
const itemParent = specialties.find(
|
||||
(item) => item.id === findName.parent
|
||||
);
|
||||
dataUrl.specialty = findName.name;
|
||||
newFilter.category = itemParent;
|
||||
newFilter.specialty = findName;
|
||||
} else {
|
||||
dataUrl.specialty = findName.name;
|
||||
newFilter.category = findName;
|
||||
newFilter.specialty = "";
|
||||
}
|
||||
const newFilter = { ...filter, name: value };
|
||||
|
||||
// اگر ورودی خالی شد → URL و لیست ریست بشن
|
||||
if (!value.trim()) {
|
||||
searchParams.delete("name");
|
||||
Object.entries(dataUrl).forEach(([key, value]) => {
|
||||
if (value) {
|
||||
searchParams.set(key, value);
|
||||
}
|
||||
});
|
||||
router.push(`?${searchParams.toString()}`);
|
||||
newFilter.name = e;
|
||||
setFilter(newFilter);
|
||||
} else {
|
||||
let newFilter = filter;
|
||||
|
||||
if (
|
||||
filter.name === filter.specialty?.name ||
|
||||
filter.name === filter.category?.name
|
||||
) {
|
||||
searchParams.delete("specialty");
|
||||
newFilter.specialty = "";
|
||||
newFilter.category = "";
|
||||
router.push(`?${searchParams.toString()}`);
|
||||
}
|
||||
newFilter.name = e;
|
||||
setFilter(newFilter);
|
||||
setDataInURL(newFilter);
|
||||
sendReq(newFilter);
|
||||
return;
|
||||
}
|
||||
|
||||
// مقدار name در URL تنظیم میشود
|
||||
searchParams.set("name", value);
|
||||
router.push(`?${searchParams.toString()}`);
|
||||
|
||||
// بهروزرسانی stateها
|
||||
setFilter(newFilter);
|
||||
setDataInURL(newFilter);
|
||||
|
||||
// ✅ debounce برای جلوگیری از اسپم درخواستها
|
||||
if (typingTimeoutRef.current) clearTimeout(typingTimeoutRef.current);
|
||||
typingTimeoutRef.current = setTimeout(() => {
|
||||
sendReq(newFilter); // فقط با نام پزشک به بکاند
|
||||
}, 400);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="!min-w-[50%] !w-full">
|
||||
<AutoComplete
|
||||
data={specialties}
|
||||
data={[]} // ✅ autocomplete بدون داده محلی، چون از API سرچ میکنی
|
||||
clearText={clearText}
|
||||
inputValue={filter.name}
|
||||
handleSearch={handleSearch}
|
||||
placeholder="جستجوی نام پزشک، تخصص، بیماری ..."
|
||||
placeholder="جستجوی نام پزشک ..."
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -40,6 +40,7 @@ function SearchBar({ filter, sendReq, setFilter, updateData, setDataInURL }) {
|
||||
filter={filter}
|
||||
setFilter={setFilter}
|
||||
setDataInURL={setDataInURL}
|
||||
sendReq={sendReq}
|
||||
/>
|
||||
<SendReq
|
||||
filter={filter}
|
||||
|
||||
Reference in New Issue
Block a user