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