-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement purpose filter and wire to map
- Loading branch information
Showing
10 changed files
with
500 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createContext, Dispatch, useContext } from 'react'; | ||
import { useImmerReducer } from 'use-immer'; | ||
|
||
export type QueryInfo = { | ||
where: string; | ||
table: string; | ||
}; | ||
type FilterState = Record<string, QueryInfo>; | ||
type FilterKeys = 'purpose'; | ||
type Action = | ||
| { | ||
type: 'UPDATE_TABLE'; | ||
filterKey: FilterKeys; | ||
value: QueryInfo; | ||
} | ||
| { | ||
type: 'CLEAR_TABLE'; | ||
filterKey: FilterKeys; | ||
} | ||
| { | ||
type: 'CLEAR_FILTER'; | ||
}; | ||
|
||
const FilterContext = createContext<{ filter: FilterState; filterDispatch: Dispatch<Action> } | null>(null); | ||
|
||
const initialState: FilterState = {}; | ||
|
||
function reducer(draft: FilterState, action: Action): FilterState { | ||
switch (action.type) { | ||
case 'UPDATE_TABLE': | ||
draft[action.filterKey] = action.value; | ||
|
||
console.log('updated filter:', JSON.stringify(draft, null, 2)); | ||
|
||
return draft; | ||
|
||
case 'CLEAR_TABLE': | ||
delete draft[action.filterKey]; | ||
|
||
console.log('updated filter:', JSON.stringify(draft, null, 2)); | ||
|
||
return draft; | ||
|
||
case 'CLEAR_FILTER': | ||
return initialState; | ||
} | ||
} | ||
|
||
export function FilterProvider({ children }: { children: React.ReactNode }) { | ||
const [filter, filterDispatch] = useImmerReducer(reducer, initialState); | ||
|
||
return <FilterContext.Provider value={{ filter, filterDispatch }}>{children}</FilterContext.Provider>; | ||
} | ||
|
||
export function useFilter() { | ||
const context = useContext(FilterContext); | ||
|
||
if (!context) { | ||
throw new Error('useFilter must be used within a FilterProvider'); | ||
} | ||
|
||
return context; | ||
} |
Oops, something went wrong.