Skip to content

Commit e33e911

Browse files
committed
wip postprocess when relationship changed only
1 parent a1e0006 commit e33e911

4 files changed

Lines changed: 24 additions & 40 deletions

File tree

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

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

11-
for (const [ i, relationship ] of relationships.entries()) {
12-
const oldRelationship = oldRelationships[i];
10+
for (const relationship of relationships) {
1311
if (!(relationship.value && relationship.field.postprocessor)) {
1412
continue;
1513
}
16-
if (checkSameRelationships(relationship.value, oldRelationship)) {
17-
continue;
18-
}
1914
const withType = relationship.field.withType;
2015
const mod = apos.modules[withType];
2116
const response = await apos.http.post(`${mod.action}/${relationship.field.postprocessor}`, {
@@ -31,6 +26,7 @@ export async function _postprocess(schema, data, widgetOptions, oldData) {
3126
},
3227
busy: true
3328
});
29+
console.log('response.relationship', response.relationship);
3430
relationship.context[relationship.field.name] = response.relationship;
3531
}
3632
}
@@ -45,9 +41,6 @@ function findRelationships(schema, object, oldObject) {
4541
field,
4642
value: object[field.name]
4743
});
48-
if (oldObject) {
49-
oldRelationships.push(oldObject[field.name]);
50-
}
5144
} else if (field.type === 'array') {
5245
for (const value of (object[field.name] || [])) {
5346
relationships = [
@@ -64,18 +57,3 @@ function findRelationships(schema, object, oldObject) {
6457
}
6558
return [ relationships, oldRelationships ];
6659
}
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;
81-
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ export default {
224224
await _postprocess(
225225
this.schema,
226226
this.docFields.data,
227-
apos.area.widgetOptions[0],
228-
oldData
227+
apos.area.widgetOptions[0]
229228
);
230229
}
231230
}

modules/@apostrophecms/schema/ui/apos/logic/AposSchema.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ export default {
275275
const newFieldState = { ...this.fieldState };
276276

277277
let changeFound = false;
278+
const changedTypes = new Set();
278279

279280
this.next.hasErrors = false;
280281
this.next.fieldState = { ...this.fieldState };
@@ -296,22 +297,25 @@ export default {
296297
)
297298
) {
298299
changeFound = true;
300+
changedTypes.add(field.type);
301+
299302
this.next.data[field.name] = this.fieldState[field.name].data;
300303
} else {
301304
this.next.data[field.name] = this.modelValue.data[field.name];
302305
}
303306
});
304307
if (
305-
oldHasErrors !== this.next.hasErrors ||
306-
oldFieldState !== newFieldState
308+
oldHasErrors !== this.next.hasErrors
309+
/* oldFieldState !== newFieldState */
307310
) {
311+
console.log('=====> weird change found <=====');
308312
// Otherwise the save button may never unlock
309313
changeFound = true;
310314
}
311315

312316
if (changeFound) {
313317
// ... removes need for deep watch at parent level
314-
this.$emit('update:model-value', { ...this.next });
318+
this.$emit('update:model-value', { ...this.next }, { changedTypes });
315319
}
316320
},
317321
displayComponent({ name, hidden = false }) {

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,22 +229,25 @@ export default {
229229
this.initPreview();
230230
},
231231
methods: {
232-
async updateDocFields(value) {
232+
async updateDocFields(value, { changedTypes = new Set() } = {}) {
233233
this.updateFieldErrors(value.fieldState);
234-
const oldDocFields = klona(this.docFields.data);
235234
this.docFields.data = {
236235
...this.docFields.data,
237236
...value.data
238237
};
238+
console.log('changedTypes', changedTypes);
239+
if (changedTypes.has('relationship')) {
240+
try {
241+
console.log('this.docFields.data', JSON.stringify(this.docFields.data?._image));
242+
await this.postprocess();
243+
} catch (e) {
244+
await this.handleSaveError(e, {
245+
fallback: 'An error occurred updating the widget.'
246+
});
247+
}
248+
}
239249
this.evaluateConditions();
240250
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-
}
248251
},
249252
initPreview() {
250253
if (!this.preview) {

0 commit comments

Comments
 (0)