Skip to content

Commit a1e0006

Browse files
committed
wip run postprocess on widget data update, tries to compare new and old relationships
1 parent b3cb46f commit a1e0006

3 files changed

Lines changed: 41 additions & 17 deletions

File tree

modules/@apostrophecms/modal/ui/apos/composables/AposEditor.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { isEqual } from 'lodash';
12
// For now just moving postprocess logic here,
23
// if needed we might move more from AposEditorMixin.js
34

45
// Perform any postprocessing required by direct or nested schema fields
56
// before the object can be saved
6-
export async function _postprocess(schema, data, widgetOptions) {
7+
export async function _postprocess(schema, data, widgetOptions, oldData) {
78
// Relationship fields may have postprocessors (e.g. autocropping)
8-
const relationships = findRelationships(schema, data);
9+
const [ relationships, oldRelationships ] = findRelationships(schema, data, oldData);
910

10-
for (const relationship of relationships) {
11+
for (const [ i, relationship ] of relationships.entries()) {
12+
const oldRelationship = oldRelationships[i];
1113
if (!(relationship.value && relationship.field.postprocessor)) {
1214
continue;
1315
}
16+
if (checkSameRelationships(relationship.value, oldRelationship)) {
17+
continue;
18+
}
1419
const withType = relationship.field.withType;
1520
const mod = apos.modules[withType];
1621
const response = await apos.http.post(`${mod.action}/${relationship.field.postprocessor}`, {
@@ -30,15 +35,19 @@ export async function _postprocess(schema, data, widgetOptions) {
3035
}
3136
}
3237

33-
function findRelationships(schema, object) {
38+
function findRelationships(schema, object, oldObject) {
3439
let relationships = [];
40+
const oldRelationships = [];
3541
for (const field of schema) {
3642
if (field.type === 'relationship') {
3743
relationships.push({
3844
context: object,
3945
field,
4046
value: object[field.name]
4147
});
48+
if (oldObject) {
49+
oldRelationships.push(oldObject[field.name]);
50+
}
4251
} else if (field.type === 'array') {
4352
for (const value of (object[field.name] || [])) {
4453
relationships = [
@@ -53,5 +62,20 @@ function findRelationships(schema, object) {
5362
];
5463
}
5564
}
56-
return relationships;
65+
return [ relationships, oldRelationships ];
66+
}
67+
68+
function checkSameRelationships(relationship, oldRelationship) {
69+
for (const piece of relationship) {
70+
const oldPiece = oldRelationship.find((p) => p._id === piece._id);
71+
console.log('piece', piece);
72+
console.log('oldPiece', oldPiece);
73+
if (!oldPiece) {
74+
return false;
75+
}
76+
if (!isEqual(piece._fields, oldPiece._fields)) {
77+
return false;
78+
}
79+
}
80+
return true;
5781
}

modules/@apostrophecms/modal/ui/apos/mixins/AposEditorMixin.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ export default {
220220
this.triggerValidation = false;
221221
});
222222
},
223-
async postprocess() {
223+
async postprocess(oldData) {
224224
await _postprocess(
225225
this.schema,
226226
this.docFields.data,
227-
apos.area.widgetOptions[0]
227+
apos.area.widgetOptions[0],
228+
oldData
228229
);
229230
}
230231
}

modules/@apostrophecms/widget-type/ui/apos/components/AposWidgetEditor.vue

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,22 @@ export default {
229229
this.initPreview();
230230
},
231231
methods: {
232-
updateDocFields(value) {
232+
async updateDocFields(value) {
233233
this.updateFieldErrors(value.fieldState);
234+
const oldDocFields = klona(this.docFields.data);
234235
this.docFields.data = {
235236
...this.docFields.data,
236237
...value.data
237238
};
238239
this.evaluateConditions();
239240
this.updatePreview();
241+
try {
242+
await this.postprocess(oldDocFields);
243+
} catch (e) {
244+
await this.handleSaveError(e, {
245+
fallback: 'An error occurred updating the widget.'
246+
});
247+
}
240248
},
241249
initPreview() {
242250
if (!this.preview) {
@@ -302,15 +310,6 @@ export default {
302310
return;
303311
}
304312
}
305-
try {
306-
await this.postprocess();
307-
} catch (e) {
308-
await this.handleSaveError(e, {
309-
fallback: 'An error occurred saving the widget.'
310-
});
311-
return;
312-
}
313-
314313
const widget = this.getWidgetObject();
315314
this.saving = true;
316315
this.$emit('modal-result', widget);

0 commit comments

Comments
 (0)