Skip to content
Snippets Groups Projects
Commit 5ad9c4d2 authored by William Enright's avatar William Enright Committed by Adrien Béraud
Browse files

Improved data loading on user profile page with Promises

Change-Id: I44f0b809a9add9ec5195a58e1dba8961e925bd53
parent 257a7e43
No related branches found
No related tags found
No related merge requests found
......@@ -58,71 +58,77 @@ var api_path_get_user_search = '/api/admin/users';
function ajaxApiCall(api_path, request_type, data, credentials, callBackFunction, async) {
// build AJAX call
var ajax = {
url: url_path + ":" + url_port + api_path,
type: request_type,
crossDomain: true,
dataType: "json",
success: function(data, statusCode, jqXHR) {
callBackFunction(data, statusCode, jqXHR);
},
error: function(data, statusCode, jqXHR) {
callBackFunction(data, statusCode, jqXHR);
}};
// pass credentials in the header
if (credentials) {
ajax['headers'] = {
"Authorization": "Basic " + btoa(credentials["username"] + ':' + credentials["password"]),
}
}
if (window.localStorage.getItem('access_token')) {
return new Promise(function(resolve) {
var ajax = {
url: url_path + ":" + url_port + api_path,
type: request_type,
crossDomain: true,
dataType: "json",
success: function (data, statusCode, jqXHR) {
callBackFunction(data, statusCode, jqXHR);
},
error: function (data, statusCode, jqXHR) {
callBackFunction(data, statusCode, jqXHR);
}
};
var jwt = localStorage.getItem('access_token');
ajax['headers'] = {
"Authorization": "Bearer " + jwt,
// pass credentials in the header
if (credentials) {
ajax['headers'] = {
"Authorization": "Basic " + btoa(credentials["username"] + ':' + credentials["password"]),
}
}
}
// pass data in the header
if (data) {
if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search ||
(api_path == api_path_post_create_user && request_type == 'POST') || api_path == api_path_post_update_user
|| api_path == api_path_get_auth_devices || api_path == api_path_post_configuration_change_password)
isSearch = true;
if (window.localStorage.getItem('access_token')) {
// search dataType
if (isSearch) {
ajax['data'] = data;
var jwt = localStorage.getItem('access_token');
ajax['headers'] = {
"Authorization": "Bearer " + jwt,
}
}
else {
if (window.localStorage.getItem('access_token')) {
var jwt = localStorage.getItem('access_token');
// pass data in the header
if (data) {
if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search || api_path == api_path_get_user_search ||
(api_path == api_path_post_create_user && request_type == 'POST') || api_path == api_path_post_update_user
|| api_path == api_path_get_auth_devices || api_path == api_path_post_configuration_change_password)
isSearch = true;
// search dataType
if (isSearch) {
ajax['data'] = data;
}
else {
if (window.localStorage.getItem('access_token')) {
var jwt = localStorage.getItem('access_token');
ajax['headers'] = {
"Authorization": "Bearer " + jwt,
}
} else {
ajax['headers'] = {
"Content-type":"application/json"
ajax['headers'] = {
"Authorization": "Bearer " + jwt,
}
} else {
ajax['headers'] = {
"Content-type": "application/json"
}
}
ajax['data'] = JSON.stringify(data);
}
}
ajax['data'] = JSON.stringify(data);
// async
if (!async) {
ajax['async'] = async;
}
}
// async
if (!async) {
ajax['async'] = async;
}
$.ajax(ajax);
isSearch = false;
resolve(data);
});
$.ajax(ajax);
isSearch = false;
return data;
}
function setJWT(value) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment