Skip to content

Commit 18f47c6

Browse files
authored
Update Store.revertRecords and Store.revert to handle summary records (#4060)
1 parent bd91a25 commit 18f47c6

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
state. Additionally enhanced `Store.modifyRecords` to return a `StoreChangeLog` of updates.
2828

2929
### 🐞 Bug Fixes
30-
* Fixed bug where `Store.modifyRecords` was not properly handling changes to `SummaryRecords`.
30+
* Fixed bugs where `Store.modifyRecords`, `Store.revertRecords` and `Store.revert` were not properly
31+
handling changes to `SummaryRecords`.
3132

3233
### 🐞 Bug Fixes
3334

data/Store.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
remove as lodashRemove,
2525
uniq,
2626
first,
27-
some
27+
some,
28+
partition
2829
} from 'lodash';
2930
import {Field, FieldSpec} from './Field';
3031
import {parseFilter} from './filter/Utils';
@@ -590,7 +591,8 @@ export class Store extends HoistBase {
590591
data: updatedData,
591592
parent: currentRec.parent,
592593
store: currentRec.store,
593-
committedData: currentRec.committedData
594+
committedData: currentRec.committedData,
595+
isSummary: currentRec.isSummary
594596
});
595597

596598
if (!equal(currentRec.data, updatedRec.data)) {
@@ -643,13 +645,20 @@ export class Store extends HoistBase {
643645
records = castArray(records);
644646
if (isEmpty(records)) return;
645647

646-
const recs = records.map(it => (it instanceof StoreRecord ? it : this.getOrThrow(it)));
648+
const recs = records.map(it => (it instanceof StoreRecord ? it : this.getOrThrow(it))),
649+
[summaryRecsToRevert, recsToRevert] = partition(recs, 'isSummary');
647650

648-
this._current = this._current
649-
.withTransaction({update: recs.map(r => this.getCommittedOrThrow(r.id))})
650-
.normalize(this._committed);
651+
if (!isEmpty(summaryRecsToRevert)) {
652+
this.revertSummaryRecords(summaryRecsToRevert);
653+
}
651654

652-
this.rebuildFiltered();
655+
if (!isEmpty(recsToRevert)) {
656+
this._current = this._current
657+
.withTransaction({update: recsToRevert.map(r => this.getCommittedOrThrow(r.id))})
658+
.normalize(this._committed);
659+
660+
this.rebuildFiltered();
661+
}
653662
}
654663

655664
/**
@@ -662,6 +671,7 @@ export class Store extends HoistBase {
662671
@action
663672
revert() {
664673
this._current = this._committed;
674+
if (this.summaryRecords) this.revertSummaryRecords(this.summaryRecords);
665675
this.rebuildFiltered();
666676
}
667677

@@ -1119,6 +1129,24 @@ export class Store extends HoistBase {
11191129
'idSpec should be either a name of a field, or a function to generate an id.'
11201130
);
11211131
}
1132+
1133+
@action
1134+
private revertSummaryRecords(records: StoreRecord[]) {
1135+
this.summaryRecords = this.summaryRecords.map(summaryRec => {
1136+
const recToRevert = records.find(it => it.id === summaryRec.id);
1137+
if (!recToRevert) return summaryRec;
1138+
1139+
const ret = new StoreRecord({
1140+
id: recToRevert.id,
1141+
raw: recToRevert.raw,
1142+
data: {...recToRevert.committedData},
1143+
store: this,
1144+
isSummary: true
1145+
});
1146+
ret.finalize();
1147+
return ret;
1148+
});
1149+
}
11221150
}
11231151

11241152
//---------------------------------------------------------------------

0 commit comments

Comments
 (0)