2020-06-14 12:38:35 +02:00
|
|
|
import 'babel-polyfill';
|
|
|
|
|
|
|
|
export default class API {
|
|
|
|
constructor() {
|
2020-06-14 22:35:01 +02:00
|
|
|
this.loggedIn = false;
|
|
|
|
this.user = { };
|
|
|
|
}
|
|
|
|
|
|
|
|
csrfToken() {
|
|
|
|
return this.loggedIn ? this.user.session.csrf_token : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async apiCall(method, params) {
|
|
|
|
params = params || { };
|
|
|
|
params.csrf_token = this.csrfToken();
|
|
|
|
let response = await fetch("/api/" + method, {
|
|
|
|
method: 'post',
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: JSON.stringify(params)
|
|
|
|
});
|
|
|
|
|
2020-06-23 01:03:35 +02:00
|
|
|
let res = await response.json();
|
|
|
|
if (!res.success && res.msg === "You are not logged in.") {
|
|
|
|
document.location.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetchUser() {
|
2020-06-14 22:35:01 +02:00
|
|
|
let response = await fetch("/api/user/info");
|
|
|
|
let data = await response.json();
|
|
|
|
this.user = data["user"];
|
|
|
|
this.loggedIn = data["loggedIn"];
|
|
|
|
return data && data.success && data.loggedIn;
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:57:54 +02:00
|
|
|
async editUser(id, username, email, password, groups) {
|
|
|
|
return this.apiCall("user/edit", { "id": id, "username": username, "email": email, "password": password, "groups": groups });
|
|
|
|
}
|
|
|
|
|
2020-06-14 12:38:35 +02:00
|
|
|
async logout() {
|
2020-06-14 22:35:01 +02:00
|
|
|
return this.apiCall("user/logout");
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
2020-06-15 00:00:15 +02:00
|
|
|
|
|
|
|
async getNotifications() {
|
|
|
|
return this.apiCall("notifications/fetch");
|
|
|
|
}
|
2020-06-15 20:07:43 +02:00
|
|
|
|
2020-06-23 16:26:04 +02:00
|
|
|
async getUser(id) {
|
|
|
|
return this.apiCall("user/get", { id: id });
|
|
|
|
}
|
|
|
|
|
2020-06-23 22:24:56 +02:00
|
|
|
async deleteUser(id) {
|
|
|
|
return this.apiCall("user/delete", { id: id });
|
|
|
|
}
|
|
|
|
|
2020-06-23 15:31:09 +02:00
|
|
|
async fetchUsers(pageNum = 1, count = 20) {
|
|
|
|
return this.apiCall("user/fetch", { page: pageNum, count: count });
|
2020-06-15 20:07:43 +02:00
|
|
|
}
|
2020-06-15 21:14:59 +02:00
|
|
|
|
2020-06-23 15:31:09 +02:00
|
|
|
async fetchGroups(pageNum = 1, count = 20) {
|
|
|
|
return this.apiCall("groups/fetch", { page: pageNum, count: count });
|
2020-06-15 21:14:59 +02:00
|
|
|
}
|
2020-06-17 14:30:37 +02:00
|
|
|
|
|
|
|
async inviteUser(username, email) {
|
|
|
|
return this.apiCall("user/invite", { username: username, email: email });
|
|
|
|
}
|
|
|
|
|
|
|
|
async createUser(username, email, password, confirmPassword) {
|
|
|
|
return this.apiCall("user/create", { username: username, email: email, password: password, confirmPassword: confirmPassword });
|
|
|
|
}
|
2020-06-17 23:50:08 +02:00
|
|
|
|
|
|
|
async getStats() {
|
|
|
|
return this.apiCall("stats");
|
|
|
|
}
|
2020-06-19 13:13:13 +02:00
|
|
|
|
|
|
|
async getRoutes() {
|
|
|
|
return this.apiCall("routes/fetch");
|
|
|
|
}
|
2020-06-19 16:37:44 +02:00
|
|
|
|
|
|
|
async saveRoutes(routes) {
|
2020-06-24 01:09:08 +02:00
|
|
|
return this.apiCall("routes/save", { routes: routes });
|
|
|
|
}
|
|
|
|
|
|
|
|
async createGroup(name, color) {
|
|
|
|
return this.apiCall("groups/create", { name: name, color: color });
|
2020-06-19 16:37:44 +02:00
|
|
|
}
|
2020-06-24 01:23:37 +02:00
|
|
|
|
|
|
|
async deleteGroup(id) {
|
|
|
|
return this.apiCall("groups/delete", { uid: id });
|
|
|
|
}
|
2020-06-26 01:47:43 +02:00
|
|
|
|
|
|
|
async getSettings(key = "") {
|
|
|
|
return this.apiCall("settings/get", { key: key });
|
|
|
|
}
|
2020-06-14 12:38:35 +02:00
|
|
|
};
|