This commit is contained in:
@@ -93,6 +93,14 @@ export default {
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
},
|
||||
fetchPutOptions(data) {
|
||||
return {
|
||||
method: "put",
|
||||
mode: "cors",
|
||||
headers: this.postAuthorizedHeaders(),
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
},
|
||||
fetchGetOptions() {
|
||||
/* GET WITH AUTH */
|
||||
return {
|
||||
@@ -134,6 +142,9 @@ export default {
|
||||
}
|
||||
return store.state.apiUrl + apiPath;
|
||||
},
|
||||
/////////////////////////////
|
||||
// ENCODE QUERY STRING
|
||||
//
|
||||
buildQuery(obj, sep, eq, name) {
|
||||
sep = sep || "&";
|
||||
eq = eq || "=";
|
||||
@@ -166,6 +177,9 @@ export default {
|
||||
encodeURIComponent(stringifyPrimitive(obj))
|
||||
);
|
||||
},
|
||||
///////////////////////////////////
|
||||
// GET DATA FROM API SERVER
|
||||
//
|
||||
get(route) {
|
||||
var that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
@@ -195,6 +209,49 @@ export default {
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
///////////////////////////////////
|
||||
// POST / PUT DATA TO API SERVER
|
||||
//
|
||||
upsert(route, data) {
|
||||
var that = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
//determine if this is a new or existing record
|
||||
var fetchOptions = undefined;
|
||||
if (data.concurrencyToken) {
|
||||
fetchOptions = that.fetchPutOptions(data);
|
||||
} else {
|
||||
fetchOptions = that.fetchPostOptions(data);
|
||||
}
|
||||
fetch(that.APIUrl(route), fetchOptions)
|
||||
.then(that.status)
|
||||
.then(that.json)
|
||||
.then(response => {
|
||||
//Note: response.error indicates there is an error, however this is not an unusual condition
|
||||
//it could be validation errors or other general error so we need to treat it here like it's normal
|
||||
//and let the caller deal with it appropriately
|
||||
resolve(response);
|
||||
})
|
||||
.catch(function(error) {
|
||||
//fundamental error, can't proceed with this call
|
||||
|
||||
var errorMessage =
|
||||
"API error: UPSERT route =" + route + ", message =" + error.message;
|
||||
store.commit("logItem", errorMessage);
|
||||
|
||||
if (error.message && error.message.includes("401")) {
|
||||
store.commit(
|
||||
"logItem",
|
||||
"User is not authorized, redirecting to login"
|
||||
);
|
||||
auth.logout();
|
||||
router.push("/login");
|
||||
} else {
|
||||
//alert("Error: " + errorMessage);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//new functions above here
|
||||
|
||||
@@ -75,6 +75,46 @@ function getControlLabel(ctrl) {
|
||||
}
|
||||
|
||||
export default {
|
||||
///////////////////////////////
|
||||
// SERVER ERRORS
|
||||
//
|
||||
Server(v, ref) {
|
||||
|
||||
|
||||
//check if any errors and short circuit if none
|
||||
|
||||
//check if this control is mentioned at all and short circuit return false if not
|
||||
|
||||
var ctrl = getControl(v, ref);
|
||||
if(typeof ctrl == 'undefined'){
|
||||
return false;
|
||||
}
|
||||
|
||||
//It IS in the list of error controls:
|
||||
|
||||
//check if this control has been changed at all, I guess being here might mean it has?
|
||||
//If this control is dirty / was changed from the value that was in it when the server returned the error then remove the error for this control from the server error list and return false
|
||||
//ELSE if this control is unchanged / not dirty
|
||||
//return the error(s) properly localized
|
||||
|
||||
|
||||
//IF user modifies field in any way then this rule must be cleared
|
||||
|
||||
//Some rules are for the entire object so...???
|
||||
//maybe need a form control that is just about rules or something
|
||||
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
// // "ErrorRequiredFieldEmpty": "{0} is a required field. Please enter a value for {0}",
|
||||
// var err = locale.get("ErrorRequiredFieldEmpty");
|
||||
// var fieldName = getControlLabel(ctrl);
|
||||
// err = _.replace(err, "{0}", fieldName);
|
||||
// //lodash replace only replaces first instance so need to do it twice
|
||||
// err = _.replace(err, "{0}", fieldName);
|
||||
// return err;
|
||||
},
|
||||
///////////////////////////////
|
||||
// REQUIRED
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user