price slider now updates store filter
All checks were successful
Vercel Preview Deployment / Deploy-Preview (push) Successful in 1m22s

This commit is contained in:
ChrQR 2024-05-26 01:41:28 +02:00
parent 3faa9fe73b
commit be98abb776
2 changed files with 16 additions and 0 deletions

View File

@ -13,14 +13,26 @@ const AddRegionButton: React.FC = () => {
return <Button onClick={handleClick}>Add Region "Sverige"</Button>; return <Button onClick={handleClick}>Add Region "Sverige"</Button>;
}; };
const ResetFilters: React.FC = () => {
const resetFilters = useFilterStore((state) => state.resetFilters);
const handleClick = () => {
resetFilters();
};
return <Button onClick={handleClick}>Reset filters</Button>;
};
export default function App() { export default function App() {
const filters = useFilterStore((state) => state.filters); const filters = useFilterStore((state) => state.filters);
return ( return (
<div className="container flex w-full flex-col justify-center"> <div className="container flex w-full flex-col justify-center">
<FormCard /> <FormCard />
<h1 className="text-2xl">Filter state:</h1>
{JSON.stringify(filters)} {JSON.stringify(filters)}
<AddRegionButton /> <AddRegionButton />
<ResetFilters />
</div> </div>
); );
} }

View File

@ -10,16 +10,19 @@ import {
import { Input } from "~/components/ui/input"; import { Input } from "~/components/ui/input";
import { Slider } from "~/components/ui/slider"; import { Slider } from "~/components/ui/slider";
import { ChangeEvent, useState } from "react"; import { ChangeEvent, useState } from "react";
import useFilterStore from "../store";
export default function Filtermenu() { export default function Filtermenu() {
const [minPrice, setMinPrice] = useState<number>(0); const [minPrice, setMinPrice] = useState<number>(0);
const [maxPrice, setMaxPrice] = useState<number>(9999); const [maxPrice, setMaxPrice] = useState<number>(9999);
const [sliderValues, setSliderValues] = useState<[number, number]>([0, 9999]); const [sliderValues, setSliderValues] = useState<[number, number]>([0, 9999]);
const setStorePrice = useFilterStore((state) => state.setPrice);
const handleMinPriceChange = (e: ChangeEvent<HTMLInputElement>): void => { const handleMinPriceChange = (e: ChangeEvent<HTMLInputElement>): void => {
const value = Math.min(Number(e.target.value), maxPrice - 1); const value = Math.min(Number(e.target.value), maxPrice - 1);
setMinPrice(value); setMinPrice(value);
setSliderValues([value, sliderValues[1]]); setSliderValues([value, sliderValues[1]]);
setStorePrice(minPrice, maxPrice);
}; };
const handleMaxPriceChange = (e: ChangeEvent<HTMLInputElement>) => { const handleMaxPriceChange = (e: ChangeEvent<HTMLInputElement>) => {
@ -32,6 +35,7 @@ export default function Filtermenu() {
setSliderValues(values); setSliderValues(values);
setMinPrice(values[0]); setMinPrice(values[0]);
setMaxPrice(values[1]); setMaxPrice(values[1]);
setStorePrice(minPrice, maxPrice);
}; };
return ( return (