| No Comments | No TrackBacks
//TODO INITIATE THE SAVING
//TODO obtain the token from AEM for POSTING (code is below),then call saveContraForm_Details
//First, get the AEM token
let token = null;
if (window.StockControl.Constants.isAEM) {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
let msg = JSON.parse(xhr.responseText);
token = msg.token;
dispatch(saveContraForm_Details(token, isRebillSelected));
}
};
// Open the request to the token endpoint and send the GET
xhr.open("GET", "/libs/granite/csrf/token.json", true);
xhr.send();
} else {
dispatch(saveContraForm_Details(token, isRebillSelected));
}


const saveContraForm_Details = (token, isRebill) => {
return (dispatch, getState) => {
let contraEndpointUrl = ''; //TODO replace this with the new API url for contra/rebill

const doError = (data, disableButtons) => {
return (dispatch, getState) => {
//TODO you can reuse this error handing in case there is a server error with the response/API down
/*console.log(data.err);
const updateStateAndResolve = async () => {
let promise = new Promise((resolve, reject) => {
let alerts = {
msg: getState().transactionDetailReducer.generalFormLabels.internal_server_error,
type: "alert-danger",
};
const newalerts = Object.assign({}, alerts);
dispatch({
type: SET_STATE_TRANSACTION,
payload: {
alerts: newalerts
}
});
resolve('done');
});

let result = await promise;
if (result === 'done') {
if (disableButtons) {
let targetSave = document.getElementsByName("save")[0];
targetSave.disabled = false;
}
}
};
updateStateAndResolve();
return data;*/
}
};

if (contraEndpointUrl === '') { //TODO DELETE this...just a demo...
//TODO this is just a test to demo the functionality so remove this whole block
setTimeout( () => {
let linkUrl = `${window.StockControl.Constants.routeTransactionDetail}?id=8kpazktoo8na1578549690896`;
if (isRebill) { //This is a rebill, so when successfully saved, open in new window.
linkUrl = window.StockControl.Constants.routeTransaction + "?Id=08uiikixbxyd1580277608412&action=rebill";
}
window.location.href = linkUrl;
}, 4000);
}


if (contraEndpointUrl !== '') { //TODO you can delete this check once you have the endpoint
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {

if (xhr.readyState === 4) {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
//console.log("PostData ", body);
//console.log("data ", data);

if (!data["error_info"]) {
//TODO add logic here to call the endpoint to post the contraReason and contraComments. This API will trigger the back end changes to the completed record and will create the contra.
//if the stock movemnt ID does not contain rebill or contra (see constants defined at top), then the first one will be called 'Contra'
// if the stock movement id does contain 'contra' (see constants at top), then it will be appended _Contra1

/*
The format for the contra is as such:
a4gbqqzixh131570518702487 - first ticket, marked as completed but will be cancelled.
a4gbqqzixh131570518702487_Contra - the first contra ticket will be appended with _Contra
a4gbqqzixh131570518702487_Rebill - the first rebill will be appended with _Rebill
a4gbqqzixh131570518702487_Contra2 - the second contra (only able to be made from the a4gbqqzixh131570518702487_Rebill ticket) is _Contra2
a4gbqqzixh131570518702487_Rebill2
a4gbqqzixh131570518702487_Contra3
*/
//TODO add success here
//TODO obtain the response. The response should contain the contra stock movement ID (eg, a4gbqqzixh131570518702487_Contra, a4gbqqzixh131570518702487_Contra1).
//Todo the stock movement id should be used to create the URL to redirect to ONLY if the isRebillSelected flag is true. Otherwise, simply refresh the page (with closed modal)

let stock_movement_id = data.id; //TODO this should be the response stock movement ID

let linkUrl = `${window.StockControl.Constants.routeTransactionDetail}?id=${stock_movement_id}`;
if (isRebill) { //This is a rebill, so when successfully saved, open in new window.
//TODO obtain the saved ID (contra one)
//Pass the contra ID to the form page Id=1234_contra&action=rebill
//Todo update isRebill in state...this should open the cancelled transaction in the draft page on finish
linkUrl = window.StockControl.Constants.routeTransaction + "?Id=" + stock_movement_id + "&action=rebill";
}

//redirects to detail page unless it is a rebill
window.location.href = linkUrl;

} else {
//dispatch(doError(data, true)); TODO uncomment
}
return data;
} else { //status is not 200
// dispatch(doError(data, false)); TODO uncomment
}
}
};
xhr.open("POST", contraEndpointUrl, true);
if (token !== null) {
xhr.setRequestHeader("CSRF-Token", token);
}
//xhr.send(JSON.stringify(body)); TODO uncomment this out when you send the body json
}



}
};

export default saveContraForm_Details;






Related Entries

No TrackBacks

TrackBack URL: http://almostafternoon.com/cgi-bin/mt5/mt-tb.cgi/2735

Leave a comment

Archives

OpenID accepted here Learn more about OpenID