web-base/admin/src/api.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

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)
});
return await response.json();
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
}
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
async fetchUsers(pageNum = 1) {
return this.apiCall("user/fetch", { page: pageNum });
}
2020-06-15 21:14:59 +02:00
async fetchGroups(pageNum = 1) {
return this.apiCall("groups/fetch", { page: pageNum });
}
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-14 12:38:35 +02:00
};