Skip to content

Commit

Permalink
Merge pull request #115 from dev-family/ADM-188
Browse files Browse the repository at this point in the history
ADM-188; (Fix) Double data update on the crud pages
  • Loading branch information
arturvolokhin authored Mar 25, 2024
2 parents 05124f3 + aa1497d commit 670e26e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
6 changes: 6 additions & 0 deletions admiral/dataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { arrayMove } from '@dnd-kit/sortable'
import { useCrudIndex } from '../crud/CrudIndexPageContext'
import { useTopLocation } from '../router'
import styles from './DataTable.module.scss'
import { useUpdateEffect } from '../utils/hooks'

export type DataTableProps<RecordType> = {
resource: string
Expand Down Expand Up @@ -60,6 +61,7 @@ export function DataTable<RecordType extends { id: number | string }>({

const [selectedKeys, setSelectedKeys] = useState<Key[]>([])
const [selectedRows, setSelectedRows] = useState<RecordType[]>([])
const isFirstRender = useRef(true)

const onSelectionChange = useCallback(
(selectedRowKeys: Key[], selectedRows: RecordType[]) => {
Expand Down Expand Up @@ -130,6 +132,10 @@ export function DataTable<RecordType extends { id: number | string }>({
}, [resource, urlState, fetch])

useEffect(() => {
if (autoupdateTime && isAutoupdateTurnOn && isFirstRender.current) {
return
}

fetch(resource, urlState)
}, [resource, urlState])

Expand Down
25 changes: 11 additions & 14 deletions admiral/filters/QuickFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../form'
import debounce from 'lodash.debounce'
import cn from 'classnames'
import useUpdateEffect from '../utils/hooks/useUpdateEffect'

export type QuickFiltersProps = {
filters?: string[]
Expand All @@ -34,21 +35,17 @@ export const QuickFilters: React.FC<QuickFiltersProps> = ({ filters }) => {
setOptions(filterOptions)
}, [filterOptions])

useEffect(() => {
if (shouldUpdateUrlState.current) {
const delayDebounceSetUrlState = debounce((value) => {
setUrlState((prevUrlState) => ({
...prevUrlState,
filter: value,
}))
}, 500)
delayDebounceSetUrlState(values)
useUpdateEffect(() => {
const delayDebounceSetUrlState = debounce((value) => {
setUrlState((prevUrlState) => ({
...prevUrlState,
filter: value,
}))
}, 500)
delayDebounceSetUrlState(values)

return () => {
delayDebounceSetUrlState.cancel()
}
} else {
shouldUpdateUrlState.current = true
return () => {
delayDebounceSetUrlState.cancel()
}
}, [values])

Expand Down
1 change: 1 addition & 0 deletions admiral/utils/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as useMedia } from './useMedia'
export { default as useMergedState } from './useMergedState'
export { default as useSafeSetState } from './useSafeSetState'
export { default as useUrlState } from './useUrlState'
export { default as useUpdateEffect } from './useUpdateEffect'
16 changes: 16 additions & 0 deletions admiral/utils/hooks/useUpdateEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { DependencyList, EffectCallback } from 'react'
import { useEffect, useRef } from 'react'

const useUpdateEffect = (callback: EffectCallback, dependencies: DependencyList) => {
const isFirstRender = useRef(true)

useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false
} else {
return callback()
}
}, dependencies)
}

export default useUpdateEffect

0 comments on commit 670e26e

Please sign in to comment.