Skip to content

Commit

Permalink
šŸ› Fix repeated create attempts adding resource item's id to list multā€¦
Browse files Browse the repository at this point in the history
ā€¦iple times
  • Loading branch information
greena13 committed Aug 4, 2020
1 parent 0b51324 commit ebe7b54
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/action-objects/buildActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const STANDARD_ACTIONS = {
newItem: 'NEW_ITEM',
clearNewItem: 'CLEAR_NEW_ITEM',
editNewItem: 'EDIT_NEW_ITEM',
editNewOrExistingItem: 'EDIT_NEW_OR_EXISTING_ITEM',
clearItemEdit: 'CLEAR_ITEM_EDIT',
createItem: 'CREATE_ITEM',
editItem: 'EDIT_ITEM',
Expand Down
3 changes: 2 additions & 1 deletion src/actions/RESTful/createItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import mergeStatus from '../../reducers/helpers/mergeStatus';
import { isRequestInProgress, registerRequestStart } from '../../utils/RequestManager';
import nop from '../../utils/function/nop';
import EmptyKey from '../../constants/EmptyKey';
import { isNew } from '../../index';

const HTTP_REQUEST_TYPE = 'POST';

Expand Down Expand Up @@ -286,7 +287,7 @@ function reducer(resources, action) {

if (status === CREATING) {
assertInDevMode(() => {
if (currentItem.status.type && currentItem.status.type !== NEW) {
if (!isNew(currentItem)) {
warn(
`${type} has the same key '${temporaryKey}' as an existing item. ` +
'Use updateItem() to update an existing item, ' +
Expand Down
5 changes: 3 additions & 2 deletions src/actions/RESTful/editNewItem.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { EDITING, NEW } from '../../constants/Statuses';
import { EDITING } from '../../constants/Statuses';
import processActionCreatorOptions from '../../action-creators/helpers/processActionCreatorOptions';
import getItemKey from '../../action-creators/helpers/getItemKey';
import applyTransforms from '../../reducers/helpers/applyTransforms';
import assertInDevMode from '../../utils/assertInDevMode';
import warn from '../../utils/dev/warn';
import { isNew } from '../../index';

/** ************************************************************************************************************
* Action creators
Expand Down Expand Up @@ -84,7 +85,7 @@ function reducer(resources, action) {

return resources;

} else if (currentItem.status.type === NEW) {
} else if (isNew(currentItem)) {

/**
* We do a shallow merge of the values that already exist in the redux store for the resource item
Expand Down
40 changes: 20 additions & 20 deletions src/reducers/helpers/applyListOperators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ function applyListOperators(lists, listOperations = {}, temporaryKey) {
listOperations.push.forEach((listKey) => {
const existingList = lists[listKey] || LIST;

if (contains(existingList, temporaryKey)) {
return;
if (contains(existingList.positions, temporaryKey)) {
updatedLists[listKey] = existingList;
} else {
updatedLists[listKey] = {
...existingList,
positions: [
...existingList.positions,
temporaryKey
]
};
}

updatedLists[listKey] = {
...existingList,
positions: [
...existingList.positions,
temporaryKey
]
};
});

listOperations.unshift.forEach((listKey) => {
const existingList = lists[listKey] || LIST;

if (contains(existingList, temporaryKey)) {
return;
if (contains(existingList.positions, temporaryKey)) {
updatedLists[listKey] = existingList;
} else {
updatedLists[listKey] = {
...existingList,
positions: [
temporaryKey,
...existingList.positions
]
};
}

updatedLists[listKey] = {
...existingList,
positions: [
temporaryKey,
...existingList.positions
]
};
});

listOperations.invalidate.forEach((listKey) => {
Expand Down

0 comments on commit ebe7b54

Please sign in to comment.