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}
0 commit comments