Skip to content

Commit

Permalink
Performs a list merging that looks for IDs
Browse files Browse the repository at this point in the history
By looking of ID fields we can handle complex cases such as updating at the same time two nested arrays.
  • Loading branch information
rricard committed Jul 22, 2016
1 parent 37bccb2 commit 38cd192
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/data/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function writeFieldToStore({
fragmentMap,
included: true,
});
value = [].concat(oldValue, value);
value = defaultListMerging(oldValue, value);
}

value.forEach((item, index) => {
Expand Down Expand Up @@ -409,3 +409,9 @@ function writeFieldToStore({
}

}

function defaultListMerging(oldValues: any[], newValues: any[]): any[] {
const newIds = newValues.map(value => value.id ? value.id : null) .filter(id => id !== null);
const filteredOldValues = oldValues.filter(value => !value.id || newIds.indexOf(value.id) < 0);
return [].concat(filteredOldValues, newValues);
}

0 comments on commit 38cd192

Please sign in to comment.