From e11f4ed3c573e1f15f62960dc7acf31a2c71c5ba Mon Sep 17 00:00:00 2001 From: ehsan Date: Sun, 21 Sep 2025 23:12:47 +0330 Subject: [PATCH] add loading for redirect button from home to doctors page && change filter for field search in name or specialty and set in url && fix bug select city in home page --- app/component/comment/ItemUser.js | 3 +-- app/doctors/page.js | 17 +++++++++--- components/doctors/head/index.js | 10 ++++++- components/doctors/search/AutoComplete.js | 1 - components/doctors/search/SearchField.js | 33 +++++++++++++++++++---- components/doctors/search/SendReq.js | 4 +-- components/home/search/RedirectLink.js | 25 ++++++++++++----- components/home/search/modal/index.js | 4 +-- 8 files changed, 75 insertions(+), 22 deletions(-) diff --git a/app/component/comment/ItemUser.js b/app/component/comment/ItemUser.js index 31001d5..10617c4 100644 --- a/app/component/comment/ItemUser.js +++ b/app/component/comment/ItemUser.js @@ -28,9 +28,8 @@ function ItemUser({ update, setUpdate, data }) { try { const res = await request.postCommentsLike(body); - console.log(res); } catch (err) { - console.log(err); + // err } finally { if (type === "like") setLoadingLike(false); else setLoadingDislike(false); diff --git a/app/doctors/page.js b/app/doctors/page.js index f3650b9..86737d8 100644 --- a/app/doctors/page.js +++ b/app/doctors/page.js @@ -9,12 +9,23 @@ async function Doctors({ searchParams }) { const API_URL = process.env.NEXT_PUBLIC_API_URL; let doctors = null; + const stateParams = searchParams.state; + const cityParams = searchParams.city; + try { let newSearchParams = searchParams; - if (matchedCity && matchedCity.id !== "63") + + // Conditional State + if (stateParams) newSearchParams.state = stateParams; + else if (matchedState) newSearchParams.state = matchedState.name; + + // Conditional City + if (cityParams) newSearchParams.city = cityParams; + else if (matchedCity && matchedCity.id !== "63") newSearchParams.city = matchedCity.name; - if (matchedState) newSearchParams.state = matchedState.name; - const params = buildDoctorParams(searchParams); + + const params = buildDoctorParams(newSearchParams); + const response = await axios.get(`${API_URL}/api/v1/doctors`, { params, diff --git a/components/doctors/head/index.js b/components/doctors/head/index.js index ff8702a..ce0a8ca 100644 --- a/components/doctors/head/index.js +++ b/components/doctors/head/index.js @@ -56,9 +56,17 @@ function Head({ matchedCity, matchedState, setDoctors }) { }; const sendReq = (newFilter) => { const params = QueryForDoctorsReq(newFilter || filter); + let filteredName = params; + if ( + newFilter.name === newFilter.specialty?.name || + newFilter.name === newFilter.category?.name + ) { + filteredName.name = ""; + delete filteredName.name; + } return request - .getDoctors(params) + .getDoctors(filteredName) .then((res) => { setDoctors(res.data); return res; diff --git a/components/doctors/search/AutoComplete.js b/components/doctors/search/AutoComplete.js index a65f843..aa44af6 100644 --- a/components/doctors/search/AutoComplete.js +++ b/components/doctors/search/AutoComplete.js @@ -19,7 +19,6 @@ function AutoComplete({ }, } : {}; - console.log(inputValue); return ( { const findName = specialties.find((item) => item.name === e); - console.log(findName); + const searchParams = new URLSearchParams(window.location.search); + let dataUrl = {}; if (findName) { let newFilter = { ...filter }; @@ -32,19 +31,43 @@ function SearchField({ filter, setFilter, updateData, setDataInURL }) { const itemParent = specialties.find( (item) => item.id === findName.parent ); + dataUrl.specialty = findName.name; newFilter.category = itemParent; newFilter.specialty = findName; // updateData("name", e); } else { + dataUrl.specialty = findName.name; newFilter.category = findName; newFilter.specialty = ""; } - newFilter.name = e + + searchParams.delete("name"); + Object.entries(dataUrl).forEach(([key, value]) => { + if (value) { + searchParams.set(key, value); + } + }); + router.push(`?${searchParams.toString()}`); + newFilter.name = e; setFilter(newFilter); // const newFilter = { ...filter, [name]: e }; // setFilter(newFilter); } else { - updateData("name", e); + 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; + // updateData("name", e); + setFilter(newFilter); + setDataInURL(newFilter); } }; diff --git a/components/doctors/search/SendReq.js b/components/doctors/search/SendReq.js index c4ebe84..a2254a4 100644 --- a/components/doctors/search/SendReq.js +++ b/components/doctors/search/SendReq.js @@ -28,8 +28,8 @@ function SendReq({ filter, setFilter, setDataInURL, sendReq }) { const filterData = () => { setLoading(true); // const newFilter = checkName(); - setFilter(filter); - setDataInURL(filter); + // setFilter(filter); + // setDataInURL(filter); sendReq(filter).finally(() => { setLoading(false); diff --git a/components/home/search/RedirectLink.js b/components/home/search/RedirectLink.js index 6a10ab3..da70af9 100644 --- a/components/home/search/RedirectLink.js +++ b/components/home/search/RedirectLink.js @@ -2,14 +2,17 @@ import SearchD from "@/components/icons/SearchD"; import { Search } from "@mui/icons-material"; -import { Button } from "@mui/material"; +import { Button, CircularProgress } from "@mui/material"; import { useRouter } from "next/navigation"; import specialties from "@/data/specialties.json"; +import { useState } from "react"; function RedirectLink({ data }) { const router = useRouter(); + const [loading, setLoading] = useState(false); - const handleRedirect = () => { + const handleRedirect = (e) => { + loading && e.preventDefault() const filters = {}; if (data.city && data.city !== "نوبت 724") filters.city = data.city; if (data.province) filters.state = data.province; @@ -25,18 +28,28 @@ function RedirectLink({ data }) { } const queryParams = new URLSearchParams(filters).toString(); - + setLoading(true); router.push(`/doctors?${queryParams}`); }; return ( <> + {/*
+ +
*/} diff --git a/components/home/search/modal/index.js b/components/home/search/modal/index.js index f09af74..80a62b8 100644 --- a/components/home/search/modal/index.js +++ b/components/home/search/modal/index.js @@ -45,8 +45,8 @@ function ModalSearchCity({ }, [provinceSelected]); const updateData = (event, name) => { - if (name === "province" && event !== provinceSelected) { - setSelected({ province: event, city: "" }); + if (name === "province") { + setSelected({ ...selected, province: event, city: "" }); } else { setSelected({ ...selected, [name]: event }); }