added state persistance
Some checks failed
Vercel Preview Deployment / Deploy-Preview (push) Failing after 36s

This commit is contained in:
ChrQR 2024-05-26 01:30:26 +02:00
parent d4e7ee8cf8
commit 3faa9fe73b
2 changed files with 110 additions and 84 deletions

View File

@ -1,9 +1,26 @@
"use client";
import { Button } from "~/components/ui/button";
import FormCard from "./_components/FormCard"; import FormCard from "./_components/FormCard";
import useFilterStore from "./store";
const AddRegionButton: React.FC = () => {
const addRegion = useFilterStore((state) => state.addRegion);
const handleClick = () => {
addRegion("sverige");
};
return <Button onClick={handleClick}>Add Region "Sverige"</Button>;
};
export default function App() { export default function App() {
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 />
{JSON.stringify(filters)}
<AddRegionButton />
</div> </div>
); );
} }

View File

@ -1,5 +1,6 @@
import { create } from "zustand"; import { create } from "zustand";
import { produce } from "immer"; import { produce } from "immer";
import { persist, createJSONStorage } from "zustand/middleware";
interface FilterState { interface FilterState {
filters: Filters; filters: Filters;
@ -55,7 +56,9 @@ const initialFilters: Filters = {
}, },
}; };
const useFilterStore = create<FilterState & FilterActions>()((set) => ({ const useFilterStore = create<FilterState & FilterActions>()(
persist(
(set) => ({
filters: { filters: {
regions: [], regions: [],
countries: [], countries: [],
@ -139,6 +142,12 @@ const useFilterStore = create<FilterState & FilterActions>()((set) => ({
state.filters = initialFilters; state.filters = initialFilters;
}), }),
), ),
})); }),
{
name: "filter-storage", // name of the item in the storage (must be unique)
storage: createJSONStorage(() => sessionStorage), // (optional) by default, 'localStorage' is used
},
),
);
export default useFilterStore; export default useFilterStore;