158 lines
3.3 KiB
JavaScript
158 lines
3.3 KiB
JavaScript
import { Account } from "./account.js";
|
|
import { MatchMaking } from "./matchmaking.js";
|
|
import { Profiles } from "./profiles.js";
|
|
import { Channels } from './chat/channels.js';
|
|
import { MyProfile } from "./MyProfile.js";
|
|
import { navigateTo } from "../index.js"
|
|
import { Tourmanents } from "./tournament/tournaments.js";
|
|
import {Notice} from "./chat/notice.js"
|
|
|
|
function getCookie(name)
|
|
{
|
|
let cookie = {};
|
|
document.cookie.split(';').forEach(function(el) {
|
|
let split = el.split('=');
|
|
cookie[split[0].trim()] = split.slice(1).join("=");
|
|
})
|
|
return cookie[name];
|
|
}
|
|
|
|
class Client
|
|
{
|
|
constructor(url)
|
|
{
|
|
this._url = url;
|
|
this.account = new Account(this);
|
|
this.profiles = new Profiles(this);
|
|
this.matchmaking = new MatchMaking(this);
|
|
this.tournaments = new Tourmanents(this);
|
|
this._logged = undefined;
|
|
|
|
this.channels = new Channels(this);
|
|
this.channel = undefined;
|
|
|
|
this.notice = new Notice(this);
|
|
}
|
|
|
|
async isAuthentificate()
|
|
{
|
|
if (this._logged == undefined)
|
|
this._logged = await this._test_logged();
|
|
return this._logged;
|
|
}
|
|
|
|
async _get(uri, data)
|
|
{
|
|
let response = await fetch(this._url + uri, {
|
|
method: "GET",
|
|
body: JSON.stringify(data),
|
|
});
|
|
return response;
|
|
}
|
|
|
|
async _post(uri, data)
|
|
{
|
|
let response = await fetch(this._url + uri, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-CSRFToken": getCookie("csrftoken"),
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
return response;
|
|
}
|
|
|
|
async _delete(uri, data)
|
|
{
|
|
let response = await fetch(this._url + uri, {
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-CSRFToken": getCookie("csrftoken"),
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
return response;
|
|
}
|
|
|
|
async _patch_json(uri, data)
|
|
{
|
|
let response = await fetch(this._url + uri, {
|
|
ethod: "PATCH",
|
|
headers: {
|
|
"X-CSRFToken": getCookie("csrftoken"),
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
return response;
|
|
}
|
|
|
|
async _patch_file(uri, file)
|
|
{
|
|
let response = await fetch(this._url + uri, {
|
|
method: "PATCH",
|
|
headers: {
|
|
"X-CSRFToken": getCookie("csrftoken"),
|
|
},
|
|
body: file,
|
|
});
|
|
return response;
|
|
}
|
|
|
|
async _update_logged(state)
|
|
{
|
|
if (this._logged == state)
|
|
return;
|
|
|
|
if (state)
|
|
{
|
|
this.me = new MyProfile(this);
|
|
await this.me.init();
|
|
[...document.getElementById('nav-account-links').children].forEach(el => {
|
|
if (el.matches('[logged-in]'))
|
|
el.classList.remove('d-none');
|
|
else
|
|
el.classList.add('d-none');
|
|
});
|
|
}
|
|
else
|
|
{
|
|
this.me = undefined;
|
|
[...document.getElementById('nav-account-links').children].forEach(el => {
|
|
if (el.matches('[logged-in]'))
|
|
el.classList.add('d-none');
|
|
else
|
|
el.classList.remove('d-none');
|
|
});
|
|
}
|
|
this._logged = state;
|
|
}
|
|
|
|
async login(username, password)
|
|
{
|
|
let response = await this._post("/api/accounts/login", {username: username, password: password})
|
|
if (response.status == 200)
|
|
await this._update_logged(true);
|
|
|
|
return response;
|
|
}
|
|
|
|
async logout()
|
|
{
|
|
await this._get("/api/accounts/logout");
|
|
await this._update_logged(false);
|
|
}
|
|
|
|
async _test_logged()
|
|
{
|
|
let response = await this._get("/api/accounts/logged");
|
|
|
|
await this._update_logged(response.status === 200);
|
|
return response.status === 200
|
|
}
|
|
}
|
|
|
|
export {Client}
|