@@ -44,6 +44,61 @@ AutoForm.hooks({
4444 context . result ( false ) ;
4545 }
4646 } ) ;
47+ } ,
48+ update : function ( apiBackendForm ) {
49+ // Keep the context to use inside the callback function
50+ var context = this ;
51+
52+ // Get current API Backend document for modification
53+ var apiBackend = context . currentDoc ;
54+
55+ // Get the set of updated properties
56+ var setApiBackendProperties = context . updateDoc . $set ;
57+
58+ // Get the set of properties to remove
59+ var unsetApiBackendProperties = context . updateDoc . $unset ;
60+
61+ // Update properties on API Backend document
62+ for ( let property in setApiBackendProperties ) {
63+ apiBackend [ property ] = setApiBackendProperties [ property ] ;
64+ }
65+
66+ // Delete unused properties from API Backend object
67+ for ( let property in unsetApiBackendProperties ) {
68+ delete apiBackend [ property ] ;
69+ }
70+
71+ // Get ID of API Umbrella backend (not the Apinf document ID)
72+ var apiUmbrellaBackendId = apiBackend . id ;
73+
74+ // Send the API Backend to API Umbrella
75+ response = Meteor . call (
76+ 'updateApiBackendOnApiUmbrella' ,
77+ apiUmbrellaBackendId ,
78+ apiBackend ,
79+ function ( error , apiUmbrellaWebResponse ) {
80+ // Check for API Umbrella error
81+ if ( apiUmbrellaWebResponse . http_status === 204 ) {
82+ // If status is OK, submit form
83+ context . result ( apiBackendForm ) ;
84+ } else {
85+ // If there are errors
86+ var errors = _ . values ( apiUmbrellaWebResponse . errors ) ;
87+
88+ // Flatten all error descriptions to show using sAlert
89+ errors = _ . flatten ( errors ) ;
90+ _ . each ( errors , function ( error ) {
91+ //Display error to the user, keep the sAlert box visible.
92+ sAlert . error ( error , { timeout : 'none' } ) ;
93+ // TODO: Figure out a way to send the errors back to the autoform fields, as if it were client validation,
94+ // and get rid of sAlert here.
95+ } ) ;
96+
97+ // Cancel form submission on error,
98+ // so user see the error message and edit the incorrect fields
99+ context . result ( false ) ;
100+ }
101+ } ) ;
47102 }
48103 } ,
49104 onSuccess : function ( ) {
@@ -81,11 +136,13 @@ AutoForm.hooks({
81136
82137 // Publish the API Backend on API Umbrella
83138 Meteor . call ( 'publishApiBackendOnApiUmbrella' , apiUmbrellaApiId , function ( error , apiUmbrellaWebResponse ) {
84- console . log ( apiUmbrellaWebResponse ) ;
85139
140+ // Check for a successful response
86141 if ( apiUmbrellaWebResponse . http_status === 201 ) {
142+ // Alert the user of the success
87143 sAlert . success ( "API Backend successfully published." ) ;
88144 } else {
145+ // If there are errors, inform the user
89146 var errors = _ . values ( apiUmbrellaWebResponse . errors ) ;
90147
91148 // Flatten all error descriptions to show using sAlert
0 commit comments