This commit is contained in:
2022-11-29 14:17:11 +01:00
parent c9a7da688f
commit 25ef07b0b7
32 changed files with 1275 additions and 507 deletions

View File

@@ -1,5 +1,3 @@
// import 'babel-polyfill';
export default class API {
constructor() {
this.loggedIn = false;
@@ -27,16 +25,31 @@ export default class API {
return res;
}
/** Info **/
async info() {
return this.apiCall("info");
}
/** UserAPI **/
async login(username, password, rememberMe=false) {
return this.apiCall("user/login", { username: username, password: password, stayLoggedIn: rememberMe })
}
async fetchUser() {
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;
if (data) {
this.user = data["user"];
this.loggedIn = data["loggedIn"];
}
return data;
}
async editUser(id, username, email, password, groups, confirmed) {
return this.apiCall("user/edit", { id: id, username: username, email: email, password: password, groups: groups, confirmed: confirmed });
return this.apiCall("user/edit", {
id: id, username: username, email: email,
password: password, groups: groups, confirmed: confirmed
});
}
async logout() {
@@ -67,10 +80,12 @@ export default class API {
return this.apiCall("user/create", { username: username, email: email, password: password, confirmPassword: confirmPassword });
}
/** Stats **/
async getStats() {
return this.apiCall("stats");
}
/** RoutesAPI **/
async getRoutes() {
return this.apiCall("routes/fetch");
}
@@ -79,6 +94,7 @@ export default class API {
return this.apiCall("routes/save", { routes: routes });
}
/** GroupAPI **/
async createGroup(name, color) {
return this.apiCall("groups/create", { name: name, color: color });
}
@@ -87,6 +103,7 @@ export default class API {
return this.apiCall("groups/delete", { id: id });
}
/** SettingsAPI **/
async getSettings(key = "") {
return this.apiCall("settings/get", { key: key });
}
@@ -95,10 +112,12 @@ export default class API {
return this.apiCall("settings/set", { settings: settings });
}
/** MailAPI **/
async sendTestMail(receiver) {
return this.apiCall("mail/test", { receiver: receiver });
}
/** PermissionAPI **/
async fetchPermissions() {
return this.apiCall("permission/fetch");
}
@@ -107,7 +126,21 @@ export default class API {
return this.apiCall("permission/save", { permissions: permissions });
}
/** VisitorsAPI **/
async getVisitors(type, date) {
return this.apiCall("visitors/stats", { type: type, date: date });
}
/** LanguageAPI **/
async getLanguages() {
return this.apiCall("language/get");
}
async setLanguageByCode(code) {
return this.apiCall("language/set", { code: code });
}
async setLanguageByName(name) {
return this.apiCall("language/set", { name: name });
}
};

View File

@@ -1,26 +0,0 @@
import moment from 'moment';
function getPeriodString(date) {
return moment(date).fromNow();
}
export default function humanReadableSize(bytes, dp = 1) {
const thresh = 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
}
export { getPeriodString };

View File

@@ -0,0 +1,14 @@
import React from "react";
import { enUS as dateFnsEN } from "date-fns/locale/index.js";
export default class LocaleEnglish {
constructor() {
this.code = "en_US";
this.name = "American English";
this.entries = {};
}
toDateFns() {
return dateFnsEN;
}
};

View File

@@ -0,0 +1,14 @@
import React from "react";
import { de as dateFnsDE } from "date-fns/locale/index.js";
export default class LocaleGerman {
constructor() {
this.code = "de_DE";
this.name = "Deutsch Standard";
this.entries = {};
}
toDateFns() {
return dateFnsDE;
}
}

View File

@@ -0,0 +1,35 @@
import LocaleGerman from "./german";
import LocaleEnglish from "./english";
let INSTANCE = new LocaleEnglish();
function initLocale(code) {
if (!INSTANCE || INSTANCE.code !== code) {
const constructors = {
"de_DE": LocaleGerman,
"en_US": LocaleEnglish,
}
if (constructors.hasOwnProperty(code)) {
INSTANCE = new (constructors[code])();
} else {
INSTANCE = { code: code, entries: { } };
}
}
return INSTANCE;
}
function translate(key) {
return (INSTANCE.entries[key] || key);
}
function useLanguageModule(module) {
if (module[INSTANCE.code]) {
for (const [key, value] of Object.entries(module[INSTANCE.code])) {
INSTANCE.entries[key] = value;
}
}
}
export { translate as L, initLocale, useLanguageModule, INSTANCE as currentLocale };

47
react/shared/util.js Normal file
View File

@@ -0,0 +1,47 @@
function humanReadableSize(bytes, dp = 1) {
const thresh = 1024;
if (Math.abs(bytes) < thresh) {
return bytes + ' B';
}
const units = ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
let u = -1;
const r = 10**dp;
do {
bytes /= thresh;
++u;
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
return bytes.toFixed(dp) + ' ' + units[u];
}
const removeParameter = (key) => {
const url = new URL(window.location);
url.searchParams.delete(key);
window.history.replaceState(null, '', url);
}
const getParameter = (key) => {
const url = new URL(window.location);
if (url.searchParams.has(key)) {
return decodeURIComponent(url.searchParams.get(key));
} else {
return null;
}
}
const encodeText = (str) => {
return Uint8Array.from(str, c => c.charCodeAt(0));
}
const decodeText = (buffer) => {
return String.fromCharCode(...new Uint8Array(buffer));
}
const getBaseUrl = () => {
return window.location.protocol + "//" + window.location.host;
}
export { humanReadableSize, removeParameter, getParameter, encodeText, decodeText, getBaseUrl };