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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion packages/ra-core/src/reducer/admin/references/oneToMany.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import assert from 'assert';

import { nameRelatedTo } from './oneToMany';
import oneToManyReducer, { nameRelatedTo } from './oneToMany';
import { DELETE, DELETE_MANY } from '../../../dataFetchActions';
import { UNDOABLE } from '../../../actions';

describe('oneToMany', () => {
describe('oneToMany', () => {
Expand Down Expand Up @@ -30,5 +32,93 @@ describe('oneToMany', () => {
'posts_comments@id_6?active=true'
);
});

it('should remove reference deleted optimistically', () => {
const previousState = {
'posts_comments@id_1': {
ids: [1, 2, 3],
total: 3,
},
'reviews_comments@id_1': {
ids: [1, 3, 4],
total: 3,
},
'posts_reviews@id_1': {
ids: [1, 2, 3],
total: 3,
},
};

const state = oneToManyReducer(previousState, {
type: UNDOABLE,
payload: {
id: 2,
},
meta: {
resource: 'comments',
optimistic: true,
fetch: DELETE,
},
});

expect(state).toEqual({
'posts_comments@id_1': {
ids: [1, 3],
total: 2,
},
'reviews_comments@id_1': {
ids: [1, 3, 4],
total: 3,
},
'posts_reviews@id_1': {
ids: [1, 2, 3],
total: 3,
},
});
});

it('should remove references deleted optimistically', () => {
const previousState = {
'posts_comments@id_1': {
ids: [1, 2, 3],
total: 3,
},
'reviews_comments@id_1': {
ids: [1, 3, 4],
total: 3,
},
'posts_reviews@id_1': {
ids: [1, 2, 3],
total: 3,
},
};

const state = oneToManyReducer(previousState, {
type: UNDOABLE,
payload: {
ids: [2, 3],
},
meta: {
resource: 'comments',
optimistic: true,
fetch: DELETE_MANY,
},
});

expect(state).toEqual({
'posts_comments@id_1': {
ids: [1],
total: 1,
},
'reviews_comments@id_1': {
ids: [1, 4],
total: 2,
},
'posts_reviews@id_1': {
ids: [1, 2, 3],
total: 3,
},
});
});
});
});
60 changes: 57 additions & 3 deletions packages/ra-core/src/reducer/admin/references/oneToMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Reducer } from 'redux';
import {
CRUD_GET_MANY_REFERENCE_SUCCESS,
CrudGetManyReferenceSuccessAction,
CrudDeleteSuccessAction,
} from '../../../actions/dataActions';
import { Identifier, ReduxState } from '../../../types';
import { DELETE, DELETE_MANY } from '../../../dataFetchActions';

const initialState = {};

Expand All @@ -13,12 +15,33 @@ interface State {

type ActionTypes =
| CrudGetManyReferenceSuccessAction
| CrudDeleteSuccessAction
| { type: 'OTHER_ACTION'; payload: any; meta?: any };

const oneToManyReducer: Reducer<State> = (
previousState = initialState,
action: ActionTypes
) => {
if (action.meta && action.meta.optimistic) {
const relatedTo = getRelatedReferences(
previousState,
action.meta.resource
);

if (action.meta.fetch === DELETE) {
return relatedTo.reduce(
removeDeletedReferences([action.payload.id]),
previousState
);
}

if (action.meta.fetch === DELETE_MANY) {
return relatedTo.reduce(
removeDeletedReferences(action.payload.ids),
previousState
);
}
}
switch (action.type) {
case CRUD_GET_MANY_REFERENCE_SUCCESS:
return {
Expand All @@ -28,16 +51,17 @@ const oneToManyReducer: Reducer<State> = (
total: action.payload.total,
},
};

default:
return previousState;
}
};

export const getIds = (state: ReduxState, relatedTo) =>
export const getIds = (state: ReduxState, relatedTo: string) =>
state.admin.references.oneToMany[relatedTo] &&
state.admin.references.oneToMany[relatedTo].ids;

export const getTotal = (state: ReduxState, relatedTo) =>
export const getTotal = (state: ReduxState, relatedTo: string) =>
state.admin.references.oneToMany[relatedTo] &&
state.admin.references.oneToMany[relatedTo].total;

Expand Down Expand Up @@ -114,7 +138,37 @@ export const getReferencesByIds = (
return Object.keys(references).length > 0 ? references : null;
};

export const nameRelatedTo = (reference, id, resource, target, filter = {}) => {
const getRelatedReferences = (previousState: State, resource: string) =>
Object.keys(previousState).filter(key => key.includes(resource));

const removeDeletedReferences = (removedIds: Identifier[]) => (
previousState: State,
key: string
) => {
const idsToKeep = previousState[key].ids.filter(
id => !removedIds.includes(id)
);

djhi marked this conversation as resolved.
Show resolved Hide resolved
if (idsToKeep.length === previousState[key].ids.length) {
return previousState;
}

return {
...previousState,
[key]: {
ids: idsToKeep,
total: idsToKeep.length,
},
};
};

export const nameRelatedTo = (
reference: string,
id: Identifier,
resource: string,
target: string,
filter: object = {}
) => {
const keys = Object.keys(filter);
if (!keys.length) {
return `${resource}_${reference}@${target}_${id}`;
Expand Down