site-proximo/assets/js/ajaxManager.js

102 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

class AjaxManager {
2020-02-12 11:44:29 +01:00
static async getAll() {
let categories = await AjaxManager.get('category');
let articles = await AjaxManager.get('article');
let article_categories = await AjaxManager.get('article_categories');
return {articles: articles, categories: categories, article_categories: article_categories};
}
2020-02-11 15:12:31 +01:00
static async get(type) {
let data = {
2020-02-11 15:12:31 +01:00
type: type,
action: 'get',
};
let response = await $.ajax({
type: "POST",
2020-02-14 12:05:21 +01:00
url: "json_ajax.php",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
console.log(response);
return response['data'];
}
2020-02-11 15:12:31 +01:00
static async delete(id, type) {
let data = {
2020-02-11 15:12:31 +01:00
type: type,
action: 'remove',
data: id,
};
let response = await $.ajax({
type: "POST",
2020-02-14 12:05:21 +01:00
url: "json_ajax.php",
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
console.log(response);
return response['status'];
}
2020-02-11 15:12:31 +01:00
static async save(data, isNew, type) {
let formattedData = {
type: type,
action: isNew ? 'create' : 'update',
2020-02-11 15:12:31 +01:00
data: data,
};
let response = await $.ajax({
type: "POST",
2020-02-14 12:05:21 +01:00
url: "json_ajax.php",
2020-02-11 15:12:31 +01:00
data: JSON.stringify(formattedData),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
console.log(response);
return response['data'];
}
2020-02-14 12:05:21 +01:00
static async saveArticleImage(image, id) {
if (image !== undefined) {
let formData = new FormData();
formData.append('type', 'image');
formData.append('data', id);
formData.append('image', image);
let response = await $.ajax({
type: "POST",
url: "form_ajax.php",
data: formData,
cache: false,
contentType: false,
processData: false,
});
response = JSON.parse(response);
console.log(response);
return response['status'];
} else
return 1;
}
2020-02-15 19:01:27 +01:00
static async saveStockChange(data, isSell) {
let formattedData = {
type: 'stock',
action: isSell ? 'sell' : 'buy',
data: data,
};
let response = await $.ajax({
type: "POST",
url: "json_ajax.php",
data: JSON.stringify(formattedData),
dataType: "json",
contentType: "application/json; charset=utf-8",
});
console.log(response);
return response['data'];
}
}