Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Commit

Permalink
Fix list after delete
Browse files Browse the repository at this point in the history
Refs #133
  • Loading branch information
fzaninotto committed Nov 23, 2016
1 parent 7eedf2c commit cd5a5a9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/actions/dataActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const CRUD_DELETE_SUCCESS = 'CRUD_DELETE_SUCCESS';
export const crudDelete = (resource, id, basePath) => ({
type: CRUD_DELETE,
payload: { id, basePath },
meta: { resource, fetch: DELETE, cancelPrevious: true },
meta: { resource, fetch: DELETE, cancelPrevious: false },
});

export const CRUD_GET_MANY = 'CRUD_GET_MANY';
Expand Down
11 changes: 9 additions & 2 deletions src/reducer/resource/list/ids.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { CRUD_GET_LIST_SUCCESS } from '../../../actions/dataActions';
import { CRUD_GET_LIST_SUCCESS, CRUD_DELETE_SUCCESS } from '../../../actions/dataActions';

export default (resource) => (previousState = [], { type, payload, meta }) => {
export default resource => (previousState = [], { type, payload, requestPayload, meta }) => {
if (!meta || meta.resource !== resource) {
return previousState;
}
switch (type) {
case CRUD_GET_LIST_SUCCESS:
return payload.data.map(record => record.id);
case CRUD_DELETE_SUCCESS: {
const index = previousState.findIndex(el => el == requestPayload.id); // eslint-disable-line eqeqeq
if (index === -1) {
return previousState;
}
return [...previousState.slice(0, index), ...previousState.slice(index + 1)];
}
default:
return previousState;
}
Expand Down
14 changes: 12 additions & 2 deletions src/sideEffect/saga/crudFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ const crudFetch = (restClient, successSideEffects = () => [], failureSideEffects
try {
response = yield call(restClient, restType, meta.resource, payload);
yield [
put({ type: `${type}_SUCCESS`, payload: response, meta }),
put({
type: `${type}_SUCCESS`,
payload: response,
requestPayload: payload,
meta,
}),
...successSideEffects(type, meta.resource, payload, response).map(a => put(a)),
put({ type: FETCH_END }),
];
} catch (error) {
yield [
put({ type: `${type}_FAILURE`, error: error.message ? error.message : error, meta }),
put({
type: `${type}_FAILURE`,
error: error.message ? error.message : error,
requestPayload: payload,
meta,
}),
...failureSideEffects(type, meta.resource, payload, error).map(a => put(a)),
put({ type: FETCH_ERROR }),
];
Expand Down
2 changes: 1 addition & 1 deletion src/sideEffect/saga/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (type, resource, payload, response) => {
case CRUD_DELETE:
return [
showNotification('Element deleted'),
push(`${payload.basePath}`),
push(payload.basePath),
];
default:
return [];
Expand Down

0 comments on commit cd5a5a9

Please sign in to comment.