Skip to content

Commit

Permalink
Create agent-filter.context.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
KyngKai909 authored Jun 15, 2024
1 parent f8b3d59 commit c606ded
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/nextjs/contexts/agent-filter.context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React, { ReactNode, createContext, useContext } from "react";
import { useLocalStorage } from "usehooks-ts";
import { AgentFilterModel } from "~~/models/agent-filter.model";

export const defaultPropertyFilter: PropertiesFilterModel = {
search: "",
agentType: "realEstate",
validated: "true",
};

interface FilterContextModel {
filter: PropertiesFilterModel;
applyFilter: React.Dispatch<React.SetStateAction<PropertiesFilterModel>>;
reset: () => void;
}

// Create a context for the filter
const FilterContext = createContext<FilterContextModel | null>(null);

// Create a provider component for the filter context
export const PropertiesFilterProvider = ({ children }: { children: ReactNode }) => {
const [filter, setFilter] = useLocalStorage<PropertiesFilterModel>(
"PropertyFilter.Filter",
defaultPropertyFilter,
);

const applyFilter = (x: any) => {
setFilter(prev => ({ ...prev, ...x }));
};

const reset = () => {
setFilter(defaultPropertyFilter);
};

return (
<FilterContext.Provider value={{ filter, applyFilter, reset }}>
{children}
</FilterContext.Provider>
);
};

// Create a hook to use the filter context
export const usePropertiesFilter = () => {
const context = useContext(FilterContext);
if (!context) {
throw new Error("useFilter must be used within a FilterProvider");
}
return context;
};

0 comments on commit c606ded

Please sign in to comment.