Skip to content
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

[RFR] Fix handling of deleted references #3216

Merged
merged 9 commits into from
May 14, 2019
Merged

Conversation

djhi
Copy link
Contributor

@djhi djhi commented May 13, 2019

Problem

When a <ReferenceManyField> displays a <DataGrid> with a DeleteButton on each row, optimistically deleting a reference will set its row into loading state but won't actually remove it until the action is resolved.

ra-references-many-delete-bug

Solution

Handle the DELETE effects in the oneToMany reducer by checking whether we have references related to the deleted item resource and removing the deleted item from them.

ra-references-many-delete-bug-fix

@djhi djhi added this to the 2.9.1 milestone May 13, 2019
@djhi djhi requested a review from fzaninotto May 13, 2019 08:43
previousState,
key
) => {
const idsToRemove = previousState[key].ids.filter(id =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ain't the total equal to the number of ids ?
If so, you could filter to get the kept id, and returns

{
    ids: keptIds,
    total: keptIds.length
}

If not the test should reflect that fact.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also refactor removeDeletedReference to use removeDeletedReferences.

previousState: State,
key: string
) => {
const idsToRemove = previousState[key].ids.filter(id =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of filtering twice just to count the number of elements to filter out, why don't you just count once and then compare the length to the length of the original key?

    const idsToKeep = previousState[key].ids.filter(
        id => !removedIds.includes(id)
    );
    if (idsToKeep.length === previousState[key].ids.length) {
        return previousState;
    }
    return {
        ...previousState,
        [key]: {
            ids: idsToKeep,
            total: idsToKeep.length,
        },
    };

@fzaninotto fzaninotto merged commit 196f6cd into master May 14, 2019
@fzaninotto fzaninotto deleted the fix-references-delete branch May 14, 2019 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants