fixed search results not getting cleared when doing another search
This commit is contained in:
parent
17fc623179
commit
9d81b0a223
1 changed files with 19 additions and 4 deletions
|
|
@ -1,20 +1,35 @@
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useReducer } from "react";
|
||||||
import { getServerList, type Server } from "../../Lib/Servers";
|
import { getServerList, type Server } from "../../Lib/Servers";
|
||||||
import ServerSearch from "../../Components/ServerSearch/ServerSearch";
|
import ServerSearch from "../../Components/ServerSearch/ServerSearch";
|
||||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||||
import { Spinner } from "react-bootstrap";
|
import { Spinner } from "react-bootstrap";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
|
type serverListAction = { type: "CLEAR" } | { type: "SET", payload: Server[] };
|
||||||
|
|
||||||
|
const serverReducer = (state: Server[], action: serverListAction): Server[] => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "CLEAR":
|
||||||
|
return [];
|
||||||
|
case "SET":
|
||||||
|
return action.payload;
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const Search = () => {
|
const Search = () => {
|
||||||
const [searchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const [servers, setServers] = useState<Array<Server>>([]);
|
// const [servers, setServers] = useState<Array<Server>>([]);
|
||||||
|
const [servers, serversDispatch] = useReducer(serverReducer, []);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const sessionCookie = Cookies.get("session");
|
const sessionCookie = Cookies.get("session");
|
||||||
|
|
||||||
const searchTerm = searchParams.get("search") || "";
|
const searchTerm = searchParams.get("search") || "";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
serversDispatch({ type: "CLEAR" });
|
||||||
|
|
||||||
if (!sessionCookie) {
|
if (!sessionCookie) {
|
||||||
navigate("/login");
|
navigate("/login");
|
||||||
return;
|
return;
|
||||||
|
|
@ -43,11 +58,11 @@ const Search = () => {
|
||||||
navigate("/");
|
navigate("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
setServers(workingServers);
|
serversDispatch({ type: "SET", payload: workingServers });
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
alert(e);
|
alert(e);
|
||||||
});
|
});
|
||||||
}, [searchTerm, navigate]);
|
}, [searchTerm, navigate, sessionCookie]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue