Skip to content

Commit

Permalink
chore: moved toggle filter drawer to redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
ariansobczak-rst committed Apr 18, 2023
1 parent 902cf1e commit 3e6be00
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 41 deletions.
43 changes: 12 additions & 31 deletions src/frontend/components/routes/resource-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useState } from 'react'
import { connect } from 'react-redux'
import { useParams } from 'react-router'

import { Box } from '@adminjs/design-system'
import BaseActionComponent from '../app/base-action-component.js'
import { ResourceJSON } from '../../interfaces/index.js'
import { ReduxState } from '../../store/store.js'
Expand All @@ -12,9 +13,10 @@ import Wrapper from './utils/wrapper.js'
import DrawerPortal from '../app/drawer-portal.js'
import FilterDrawer from '../app/filter-drawer.js'
import allowOverride from '../../hoc/allow-override.js'
import { useFilterDrawer } from '../../hooks/use-filter-drawer.js'

type PropsFromState = {
resources: Array<ResourceJSON>;
resources: Array<ResourceJSON>
}

type Props = PropsFromState
Expand All @@ -23,54 +25,33 @@ const ResourceAction: React.FC<Props> = (props) => {
const params = useParams<ResourceActionParams>()
const { resources } = props
const { resourceId, actionName } = params
const [filterVisible, setFilterVisible] = useState(false)
const { toggleFilter } = useFilterDrawer()
const [tag, setTag] = useState('')

const resource = resources.find((r) => r.id === resourceId)
if (!resource) {
return (<NoResourceError resourceId={resourceId!} />)
return <NoResourceError resourceId={resourceId!} />
}
const action = resource.resourceActions.find((r) => r.name === actionName)
if (!action) {
return (<NoActionError resourceId={resourceId!} actionName={actionName!} />)
return <NoActionError resourceId={resourceId!} actionName={actionName!} />
}

const toggleFilter = action.showFilter
? ((): void => setFilterVisible(!filterVisible))
: undefined

if (action.showInDrawer) {
return (
<DrawerPortal width={action.containerWidth}>
<BaseActionComponent
action={action}
resource={resource}
/>
<BaseActionComponent action={action} resource={resource} />
</DrawerPortal>
)
}

return (
<Wrapper width={action.containerWidth} showFilter={action.showFilter}>
<ActionHeader
resource={resource}
action={action}
toggleFilter={toggleFilter}
tag={tag}
/>
<BaseActionComponent
action={action}
resource={resource}
setTag={setTag}
/>
{action.showFilter ? (
<FilterDrawer
key={filterVisible.toString()}
resource={resource}
isVisible={filterVisible}
toggleFilter={toggleFilter!}
/>
) : ''}
<Box flex flexDirection="column">
<ActionHeader resource={resource} action={action} toggleFilter={toggleFilter} tag={tag} />
<BaseActionComponent action={action} resource={resource} setTag={setTag} />
</Box>
{action.showFilter ? <FilterDrawer resource={resource} /> : ''}
</Wrapper>
)
}
Expand Down
9 changes: 0 additions & 9 deletions src/frontend/components/routes/utils/query-has-filter.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/frontend/hooks/use-filter-drawer.tsx
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 }
}
16 changes: 16 additions & 0 deletions src/frontend/store/actions/filter-drawer.ts
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,
})
25 changes: 25 additions & 0 deletions src/frontend/store/reducers/filterDrawerReducer.ts
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
}
}
}
1 change: 1 addition & 0 deletions src/frontend/store/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './assetsReducer.js'
export * from './brandingReducer.js'
export * from './dashboardReducer.js'
export * from './drawerReducer.js'
export * from './filterDrawerReducer.js'
export * from './localesReducer.js'
export * from './modalReducer.js'
export * from './noticesReducer.js'
Expand Down
6 changes: 5 additions & 1 deletion src/frontend/store/store.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Note: We are using legacy "createStore"
import { combineReducers, legacy_createStore as createStore } from 'redux'
import { composeWithDevToolsDevelopmentOnly } from '@redux-devtools/extension'
import { combineReducers, legacy_createStore as createStore } from 'redux'

import type { Assets, BrandingOptions, VersionProps } from '../../adminjs-options.interface.js'
import {
assetsReducer,
brandingReducer,
dashboardReducer,
drawerReducer,
filterDrawerReducer,
localesReducer,
modalReducer,
noticesReducer,
Expand All @@ -20,6 +21,7 @@ import {
versionsReducer,
type DashboardInState,
type DrawerInState,
type FilterDrawerInState,
type LolcaleInState,
type ModalInState,
type NoticesInState,
Expand All @@ -36,6 +38,7 @@ export type ReduxState = {
branding: BrandingOptions
dashboard: DashboardInState
drawer: DrawerInState
filterDrawer: FilterDrawerInState
locale: LolcaleInState
modal: ModalInState
notices: NoticesInState
Expand All @@ -53,6 +56,7 @@ const reducer = combineReducers<ReduxState>({
branding: brandingReducer,
dashboard: dashboardReducer,
drawer: drawerReducer,
filterDrawer: filterDrawerReducer,
locale: localesReducer,
modal: modalReducer,
notices: noticesReducer,
Expand Down

0 comments on commit 3e6be00

Please sign in to comment.