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] Rely on meta to handle optimistic actions #2684

Merged
merged 4 commits into from
Jan 2, 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
77 changes: 42 additions & 35 deletions packages/ra-core/src/reducer/admin/resource/data.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { FETCH_END } from '../../../actions/fetchActions';
import {
CREATE,
DELETE,
DELETE_MANY,
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
GET_ONE,
UPDATE,
UPDATE_MANY,
} from '../../../dataFetchActions';
import {
CRUD_DELETE_OPTIMISTIC,
CRUD_DELETE_MANY_OPTIMISTIC,
CRUD_UPDATE_OPTIMISTIC,
CRUD_UPDATE_MANY_OPTIMISTIC,
} from '../../../actions/dataActions';

import getFetchedAt from '../../../util/getFetchedAt';

Expand Down Expand Up @@ -57,43 +54,53 @@ export const addRecordsFactory = getFetchedAt => (

const addRecords = addRecordsFactory(getFetchedAt);

// We track the last time data was fetched by adding a property on the data which is an array
// (Hence using defineProperty)
const updateDataFetchedTime = (data, fetchedAt) => {
Object.defineProperty(data, 'fetchedAt', {
value: fetchedAt,
});
}

const initialState = {};
Object.defineProperty(initialState, 'fetchedAt', { value: {} }); // non enumerable by default
updateDataFetchedTime(initialState, {}); // non enumerable by default

export default (previousState = initialState, { type, payload, meta }) => {
if (type === CRUD_UPDATE_OPTIMISTIC) {
const updatedRecord = { ...previousState[payload.id], ...payload.data };
return addRecords([updatedRecord], previousState);
}
if (type === CRUD_UPDATE_MANY_OPTIMISTIC) {
const updatedRecords = payload.ids
.reduce((records, id) => records.concat(previousState[id]), [])
.map(record => ({ ...record, ...payload.data }));
return addRecords(updatedRecords, previousState);
}
if (type === CRUD_DELETE_OPTIMISTIC) {
const { [payload.id]: removed, ...newState } = previousState;
export default (previousState = initialState, { payload, meta }) => {
if (meta && meta.optimistic) {
if (meta.fetch === UPDATE) {
ThieryMichel marked this conversation as resolved.
Show resolved Hide resolved
ThieryMichel marked this conversation as resolved.
Show resolved Hide resolved
const updatedRecord = { ...previousState[payload.id], ...payload.data };
return addRecords([updatedRecord], previousState);
}
if (meta.fetch === UPDATE_MANY) {
const updatedRecords = payload.ids
.map(id => ({
...previousState[id],
...payload.data,
}));

Object.defineProperty(newState, 'fetchedAt', {
value: previousState.fetchedAt,
});
return addRecords(updatedRecords, previousState);
}
if (meta.fetch === DELETE) {
const { [payload.id]: removed, ...newState } = previousState;

return newState;
}
if (type === CRUD_DELETE_MANY_OPTIMISTIC) {
const newState = Object.entries(previousState)
.filter(([key]) => !payload.ids.includes(key))
.reduce((obj, [key, val]) => ({ ...obj, [key]: val }), {});
updateDataFetchedTime(newState, previousState.fetchedAt);

Object.defineProperty(newState, 'fetchedAt', {
value: previousState.fetchedAt,
});
return newState;
}
if (meta.fetch === DELETE_MANY) {
const newState = Object.entries(previousState)
.filter(([key]) => !payload.ids.includes(key))
ThieryMichel marked this conversation as resolved.
Show resolved Hide resolved
.reduce((obj, [key, val]) => ({ ...obj, [key]: val }), {});

return newState;
updateDataFetchedTime(newState, previousState.fetchedAt);

return newState;
}
}
if (!meta || !meta.fetchResponse || meta.fetchStatus !== FETCH_END) {
return previousState;
}

switch (meta.fetchResponse) {
case GET_LIST:
case GET_MANY:
Expand Down
21 changes: 13 additions & 8 deletions packages/ra-core/src/reducer/admin/resource/data.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import assert from 'assert';

import {
CRUD_DELETE_OPTIMISTIC,
CRUD_DELETE_MANY_OPTIMISTIC,
} from '../../../actions/dataActions';
import { DELETE, DELETE_MANY } from '../../../dataFetchActions';

import dataReducer, { addRecordsFactory } from './data';

Expand Down Expand Up @@ -100,7 +97,7 @@ describe('data addRecordsFactory', () => {
});

describe('Resources data reducer', () => {
describe('CRUD_DELETE_OPTIMISTIC', () => {
describe('optimistic DELETE', () => {
it('removes the deleted record', () => {
const state = {
record1: { id: 'record1', prop: 'value' },
Expand All @@ -110,8 +107,12 @@ describe('Resources data reducer', () => {

assert.deepEqual(
dataReducer(state, {
type: CRUD_DELETE_OPTIMISTIC,
type: 'FOO',
payload: { id: 'record2' },
meta: {
fetch: DELETE,
optimistic: true,
}
}),
{
record1: { id: 'record1', prop: 'value' },
Expand All @@ -120,7 +121,7 @@ describe('Resources data reducer', () => {
);
});
});
describe('CRUD_DELETE_MANY_OPTIMISTIC', () => {
describe('optimistic DELETE_MANY', () => {
it('removes the deleted records', () => {
const state = {
record1: { id: 'record1', prop: 'value' },
Expand All @@ -130,8 +131,12 @@ describe('Resources data reducer', () => {

assert.deepEqual(
dataReducer(state, {
type: CRUD_DELETE_MANY_OPTIMISTIC,
type: 'FOO',
payload: { ids: ['record3', 'record2'] },
meta: {
fetch: DELETE_MANY,
optimistic: true,
}
}),
{
record1: { id: 'record1', prop: 'value' },
Expand Down