diff --git a/src/components/Cars/List/index.jsx b/src/components/Cars/List/index.jsx index 12bcb17..57c36eb 100644 --- a/src/components/Cars/List/index.jsx +++ b/src/components/Cars/List/index.jsx @@ -18,10 +18,10 @@ import useStyles from "../../useStyles"; const MainForm = () => { const classes = useStyles(); - const [online, setOnline] = useState(false); - const [onlineHMI, setOnlineHMI] = useState(false); + const [online, setOnline] = useState(null); + const [onlineHMI, setOnlineHMI] = useState(null); const [selectedVins, setSelectedVins] = useState([]); - const { vins, search, query, setQuery, loading } = useQuery(); + const { payload, query, setQuery, loading } = useQuery(); const { setTitle, setSitePath } = useStatusContext(); const { token: { @@ -36,7 +36,7 @@ const MainForm = () => { }; const handleOnline = (event) => { - setOnline(event.target.checked); + setOnline(event.target.checked || null); }; const handleSelectAll = (cars) => { @@ -53,7 +53,7 @@ const MainForm = () => { }; const handleOnlineHMI = (event) => { - setOnlineHMI(event.target.checked); + setOnlineHMI(event.target.checked || null); }; useEffect(() => { @@ -105,10 +105,9 @@ const MainForm = () => { token={token} multiSelect search={{ - search, - vins, - online: online ? true : null, - online_hmi: onlineHMI ? true : null, + ...payload, + online, + online_hmi: onlineHMI, }} selected={selectedVins} onSelect={handleSelect} diff --git a/src/components/Cars/List/useQuery.js b/src/components/Cars/List/useQuery.js index ba1ca86..ebd43c7 100644 --- a/src/components/Cars/List/useQuery.js +++ b/src/components/Cars/List/useQuery.js @@ -35,42 +35,40 @@ function parseQueryPart(part) { export default function useQuery() { const [query, setQuery] = useLocalStorage("VEHICLE_SEARCH", ""); const [parts, setParts] = useState([]); - const [search, setSearch] = useState(null); - const [vins, setVins] = useState(""); + const [payload, setPayload] = useState({}); const [loading, setLoading] = useState(true); - function reset() { - setSearch(""); - setVins([]); - } - useEffect(() => { - reset(); + setLoading(true); + let vins = []; + let search = ""; const parts = query.replaceAll(" ", ",").split(",").map(parseQueryPart); setParts(parts); parts.forEach(([type, value]) => { if (type === "vin") { - setVins(vins => { - if (vins.length) { - return `${vins},${value}`; - } - return value; - }); + vins.push(value); } if (type === "search") { - setSearch(search => `${search} ${value}`.trim()); + search = `${search} ${value}`.trim(); } }); + + setPayload({ + search, + vins: vins.join(","), + }) + }, [query, setPayload]); + + useEffect(() => { setLoading(false); - }, [query]); + }, [payload, setLoading]) return { parts, - search, - vins, + payload, query, setQuery, loading, diff --git a/src/components/Cars/List/useQuery.test.jsx b/src/components/Cars/List/useQuery.test.jsx index 14f7483..ae2e511 100644 --- a/src/components/Cars/List/useQuery.test.jsx +++ b/src/components/Cars/List/useQuery.test.jsx @@ -3,8 +3,7 @@ import useQuery from "./useQuery"; const MyComponent = () => { const { - search, - vins, + payload, query, setQuery, } = useQuery(); @@ -17,8 +16,8 @@ const MyComponent = () => { onChange={(e) => setQuery(e.target.value)} />
    -
  1. {search}
  2. -
  3. {vins}
  4. +
  5. {payload.search}
  6. +
  7. {payload.vins}
  8. {query}
diff --git a/src/components/Controls/CarSelectionTable/index.jsx b/src/components/Controls/CarSelectionTable/index.jsx index da12505..f90e7c6 100644 --- a/src/components/Controls/CarSelectionTable/index.jsx +++ b/src/components/Controls/CarSelectionTable/index.jsx @@ -67,7 +67,6 @@ const CarSelectionTable = (props) => { const [order, setOrder] = useState("asc"); const { getVehicles, vehicles, totalVehicles } = useVehicleContext(); const { setMessage } = useStatusContext(); - const { search: searchTerm } = search; const handleSort = (_event, property) => { if (property === orderBy) { @@ -111,21 +110,22 @@ const CarSelectionTable = (props) => { }; useEffect(() => { - const options = { + const queryParams = { + ...search, limit: pageSize, offset: pageSize * pageIndex, order: `${orderBy} ${order}`, }; (async () => { try { - await getVehicles(Object.assign(options, search), token); + await getVehicles(queryParams, token); } catch (e) { setMessage(e.message); logger.warn(e.stack); } })(); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [pageIndex, pageSize, orderBy, order, search, token]); + }, [pageIndex, pageSize, orderBy, order, token]); useEffect(() => { setPageIndex(0); @@ -172,7 +172,7 @@ const CarSelectionTable = (props) => {