I am calling a single API many times in one of my React page depending on some data. The problem I am facing is to add a waiting loader until all APIs respond. I wrote the below code.
return function (dispatch) {
document.body.classList.add('loading');
let appName = payload.appName;
let functionName = payload.functionName;
let tokenKey = payload.tokenKey;
let url = host + "/focusRestApi/webapi/fetchModelList";
let config = { headers: { 'Authorization': "token " + tokenKey, 'Token': localStorage.getItem('token') } };
let data = {};
data.applicationName = appName;
url = url + "?portfolioId=" + portfolioName;
if(functionName.length!=0){
for(let i=0;i<functionName.length;i++){
data.functionality = functionName[i].applicationFunctionalityName;
axios.post(url, data, config)
.then((response) => {
dispatch({
type: "FUNCTIONVIEW_CTD_DATA", payload:
{ functionName: functionName[i].applicationFunctionalityName, data: response.data }
});
if(i === functionName.length-1) {
document.body.classList.remove('loading');
}
});
}
}
}
Here, I have removed the loader inside an If condition. But it's not working fine. The loader is getting removed before all APis are responding. I am not sure what is the issue here. Can someone help me with this. Thanks in advance..
Updated: The above code has been written in getGithubModals
function. And here is the code from a different JS file where I am calling the above function:
if (props.functionality_data_list.length === 0) {
props.dispatch(getGithubModals({
appName: appId,
functionName: "0", githubUser: git_user, repo: git_repo, tokenKey: git_token
}));
} else {
props.functionality_data_list.forEach(function (id) {
functionName.push(id);
props.dispatch(getFunctionalityModals({
applicationName: appId,
functionality: id.applicationFunctionalityName
}));
});
props.dispatch(getGithubModals({
appName: appId,
functionName: functionName, githubUser: git_user, repo: git_repo, tokenKey: git_token
}));
}