-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: moved toggle filter drawer to redux store
- Loading branch information
1 parent
902cf1e
commit 3e6be00
Showing
7 changed files
with
88 additions
and
41 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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { useEffect, useState } from 'react' | ||
import { hideFilterDrawer, showFilterDrawer } from '../store/actions/filter-drawer.js' | ||
import { ReduxState } from '../store/index.js' | ||
import { useQueryListParams } from './use-query-list-params.js' | ||
|
||
export const useFilterDrawer = () => { | ||
const [filtersCount, setFiltersCount] = useState(0) | ||
const dispach = useDispatch() | ||
const isVisible = useSelector((state: ReduxState) => state.filterDrawer.isVisible) | ||
|
||
const { showFilters, filters = {} } = useQueryListParams() | ||
|
||
useEffect(() => { | ||
setFiltersCount(Object.keys(filters).length) | ||
}, [filters]) | ||
|
||
useEffect(() => { | ||
if (showFilters) { | ||
dispach(showFilterDrawer()) | ||
} | ||
}, [showFilters]) | ||
|
||
const toggleFilter = () => { | ||
dispach(isVisible ? hideFilterDrawer() : showFilterDrawer()) | ||
} | ||
|
||
return { filtersCount, isVisible, toggleFilter } | ||
} |
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,16 @@ | ||
export const OPEN_FILTER_DRAWER = 'OPEN_FILTER_DRAWER' | ||
export const CLOSE_FILTER_DRAWER = 'CLOSE_FILTER_DRAWER' | ||
|
||
export type FilterDrawerAction = | ||
| { type: typeof OPEN_FILTER_DRAWER; isVisible: true } | ||
| { type: typeof CLOSE_FILTER_DRAWER; isVisible: false } | ||
|
||
export const showFilterDrawer = (): FilterDrawerAction => ({ | ||
type: OPEN_FILTER_DRAWER, | ||
isVisible: true, | ||
}) | ||
|
||
export const hideFilterDrawer = (): FilterDrawerAction => ({ | ||
type: CLOSE_FILTER_DRAWER, | ||
isVisible: false, | ||
}) |
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,25 @@ | ||
import { | ||
CLOSE_FILTER_DRAWER, | ||
OPEN_FILTER_DRAWER, | ||
type FilterDrawerAction, | ||
} from '../actions/filter-drawer.js' | ||
|
||
export type FilterDrawerInState = ReturnType<typeof filterDrawerReducer> | ||
|
||
const initialState = { | ||
isVisible: false, | ||
} | ||
|
||
export const filterDrawerReducer = (state = initialState, action: FilterDrawerAction) => { | ||
switch (action.type) { | ||
case OPEN_FILTER_DRAWER: { | ||
return { ...state, isVisible: action.isVisible } | ||
} | ||
case CLOSE_FILTER_DRAWER: { | ||
return { ...state, isVisible: action.isVisible } | ||
} | ||
default: { | ||
return state | ||
} | ||
} | ||
} |
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