-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix List view error after delete when using a field with no record test #5900
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f0e60a1
Fix List error when deleting record with non-robut field
fzaninotto 9f9a7b3
Add more comments
fzaninotto b1c4134
Fix unit test
fzaninotto 4625dbe
avoid rerender
fzaninotto 240209f
Fix logic error
fzaninotto 1bb2675
Fix request leak
fzaninotto 0ef90e9
Add unit test
fzaninotto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import { isValidElement, ReactElement, useEffect, useMemo } from 'react'; | ||
import inflection from 'inflection'; | ||
import { Location } from 'history'; | ||
import { useSelector } from 'react-redux'; | ||
import get from 'lodash/get'; | ||
|
||
import { useCheckMinimumRequiredProps } from './checkMinimumRequiredProps'; | ||
import useListParams from './useListParams'; | ||
import useRecordSelection from './useRecordSelection'; | ||
import useTranslate from '../i18n/useTranslate'; | ||
import useNotify from '../sideEffect/useNotify'; | ||
import useGetList from '../dataProvider/useGetList'; | ||
import { useGetMainList } from '../dataProvider/useGetMainList'; | ||
import { SORT_ASC } from '../reducer/admin/resource/list/queryReducer'; | ||
import { CRUD_GET_LIST } from '../actions'; | ||
import defaultExporter from '../export/defaultExporter'; | ||
|
@@ -18,7 +16,6 @@ import { | |
SortPayload, | ||
RecordMap, | ||
Identifier, | ||
ReduxState, | ||
Record, | ||
Exporter, | ||
} from '../types'; | ||
|
@@ -53,8 +50,6 @@ const defaultSort = { | |
order: SORT_ASC, | ||
}; | ||
|
||
const defaultData = {}; | ||
|
||
export interface ListControllerProps<RecordType extends Record = Record> { | ||
basePath: string; | ||
currentSort: SortPayload; | ||
|
@@ -153,7 +148,9 @@ const useListController = <RecordType extends Record = Record>( | |
* We want the list of ids to be always available for optimistic rendering, | ||
* and therefore we need a custom action (CRUD_GET_LIST) that will be used. | ||
*/ | ||
const { ids, total, error, loading, loaded } = useGetList<RecordType>( | ||
const { ids, data, total, error, loading, loaded } = useGetMainList< | ||
RecordType | ||
>( | ||
resource, | ||
{ | ||
page: query.page, | ||
|
@@ -181,41 +178,12 @@ const useListController = <RecordType extends Record = Record>( | |
} | ||
); | ||
|
||
const data = useSelector( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logic moved to a special hook, useGetMainList |
||
(state: ReduxState): RecordMap<RecordType> => | ||
get( | ||
state.admin.resources, | ||
[resource, 'data'], | ||
defaultData | ||
) as RecordMap<RecordType> | ||
); | ||
|
||
// When the user changes the page/sort/filter, this controller runs the | ||
// useGetList hook again. While the result of this new call is loading, | ||
// the ids and total are empty. To avoid rendering an empty list at that | ||
// moment, we override the ids and total with the latest loaded ones. | ||
const defaultIds = useSelector((state: ReduxState): Identifier[] => | ||
get(state.admin.resources, [resource, 'list', 'ids'], []) | ||
); | ||
const defaultTotal = useSelector((state: ReduxState): number => | ||
get(state.admin.resources, [resource, 'list', 'total']) | ||
); | ||
|
||
// Since the total can be empty during the loading phase | ||
// We need to override that total with the latest loaded one | ||
// This way, the useEffect bellow won't reset the page to 1 | ||
const finalTotal = typeof total === 'undefined' ? defaultTotal : total; | ||
|
||
const finalIds = typeof total === 'undefined' ? defaultIds : ids; | ||
|
||
const totalPages = useMemo(() => { | ||
return Math.ceil(finalTotal / query.perPage) || 1; | ||
}, [query.perPage, finalTotal]); | ||
const totalPages = Math.ceil(total / query.perPage) || 1; | ||
|
||
useEffect(() => { | ||
if ( | ||
query.page <= 0 || | ||
(!loading && query.page > 1 && (finalIds || []).length === 0) | ||
(!loading && query.page > 1 && ids.length === 0) | ||
) { | ||
// Query for a page that doesn't exist, set page to 1 | ||
queryModifiers.setPage(1); | ||
|
@@ -224,15 +192,7 @@ const useListController = <RecordType extends Record = Record>( | |
// It occurs when deleting the last element of the last page | ||
queryModifiers.setPage(totalPages); | ||
} | ||
}, [ | ||
loading, | ||
query.page, | ||
finalIds, | ||
queryModifiers, | ||
total, | ||
totalPages, | ||
defaultIds, | ||
]); | ||
}, [loading, query.page, ids, queryModifiers, total, totalPages]); | ||
|
||
const currentSort = useMemo( | ||
() => ({ | ||
|
@@ -262,8 +222,8 @@ const useListController = <RecordType extends Record = Record>( | |
filterValues: query.filterValues, | ||
hasCreate, | ||
hideFilter: queryModifiers.hideFilter, | ||
ids: finalIds, | ||
loaded: loaded || defaultIds.length > 0, | ||
ids, | ||
loaded: loaded || ids.length > 0, | ||
loading, | ||
onSelect: selectionModifiers.select, | ||
onToggleItem: selectionModifiers.toggle, | ||
|
@@ -277,7 +237,7 @@ const useListController = <RecordType extends Record = Record>( | |
setPerPage: queryModifiers.setPerPage, | ||
setSort: queryModifiers.setSort, | ||
showFilter: queryModifiers.showFilter, | ||
total: finalTotal, | ||
total: total, | ||
}; | ||
}; | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test fails on master