Skip to content
Snippets Groups Projects
Commit 35dde067 authored by Matthias Konitzny's avatar Matthias Konitzny :fire:
Browse files

Unified ajax calls.

parent 7d12f4b6
No related branches found
No related tags found
1 merge request!3Master into new editor
/*global ks_global*/
/**
* Uses the fetch API to create an ajax call for the WordPress API to fetch/post data from/to the server.
* @param data {FormData} Data for the ajax call. Must contain an entry which specifies the ajax `action`
* @returns {Promise<any>} The response from the server
*/
function ajaxCall(data) {
let opts = {
method: "POST",
body: data,
};
return fetch(ks_global.ajax_url, opts).then(function (response) {
return response.json();
});
}
/**
* Returns the json object from the stored graph as promise.
*
......@@ -12,14 +28,7 @@ export function loadGraphJson(spaceId) {
data.append("action", "get_space");
data.append("space", spaceId);
let opts = {
method: "POST",
body: data,
};
return fetch(ks_global.ajax_url, opts).then(function (response) {
return response.json();
});
return ajaxCall(data);
}
/**
......@@ -36,14 +45,7 @@ export function saveGraphJson(spaceId, json) {
data.append("space", spaceId);
data.append("graph", JSON.stringify(json));
let opts = {
method: "POST",
body: data,
};
return fetch(ks_global.ajax_url, opts).then(function (response) {
return response.json();
});
return ajaxCall(data);
}
/**
......@@ -55,14 +57,5 @@ export function listAllSpaces() {
const data = new FormData();
data.append("action", "list_spaces");
let opts = {
method: "POST",
body: data,
};
return fetch(ks_global.ajax_url, opts)
.then(function (response) {
return response.json();
})
.then((data) => data.spaces);
return ajaxCall(data).then((data) => data.spaces);
}
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