save changed marks

This commit is contained in:
walamana
2023-09-18 11:51:41 +02:00
parent 00cca5ca9a
commit 24f14da9b2
16 changed files with 300 additions and 119 deletions

View File

@@ -0,0 +1,84 @@
import type {Gottesdienst, Mark} from "@/models/models";
import {Auth} from "@/services/auth";
const API_ENDPOINT = "http://0.0.0.0:8080/api"
export async function api(endpoint: string, method: string = "GET", body?: any ) {
let isJson = (typeof body == "object")
return fetch(API_ENDPOINT + endpoint, {
method: method,
body: isJson ? JSON.stringify(body) : body,
headers: {
Accept: "application/json",
"Content-Type": (isJson ? "application/json" : "text/plain"),
"Access-Control-Allow-Origin": "*",
"Authorization": "Bearer " + Auth.getToken()
}
})
}
function setToken(token: string) {
let expires = new Date((new Date()).getTime() + (1000*60*60*24*5)).toUTCString()
document.cookie = `token=${token};expires=${expires};HTTPOnly`
}
function getToken(): string | null {
return ""
}
export namespace API {
export async function getPlan(id: number) {
return api(`/plan?id=${id}`).then(res => res.json())
.then(data => {
return {
gottesdienste: data.gottesdienste,
ministranten: data.ministranten,
marks: data.marks
}
})
}
function formatGottesdienste(data: any): Array<Gottesdienst>{
return data.map(json => {
json["date"] = new Date(json["date"])
json["attendance"] = new Date(json["attendance"])
return json as Gottesdienst
}) as Array<Gottesdienst>
}
export async function addGottesdienst(
name: string = "",
date: Date,
attendance: Date = new Date(date.getTime() - 1000*60*30),
planId: number
){
return api("/gottesdienste", "PUT", {
id: 0, name, date: date.getTime(), attendance: attendance.getTime(), planId
}).then(res => res.json())
.then(json => {
return {
id: json.id, name, date, attendance, planId
}
})
}
export async function deleteGottesdienst(id) {
return api("/gottesdienste?id=" + id, "DELETE")
.then(data => data.status == 200)
}
export async function setMarks(marks: Mark[]): Promise<boolean> {
return api("/marks", "PATCH", marks)
.then(res => Promise.resolve(res.status == 200))
}
export async function resetPassword(username: String): Promise<any> {
return api("/auth/reset", "POST", { username })
.then(res => res.json())
}
}

View File

@@ -0,0 +1,85 @@
import {api} from "@/services/api";
import {Subject} from "rxjs";
export namespace Auth {
let loggedIn = false
let user = ""
let privileges: string[] = []
export const loggedInSubject: Subject<boolean> = new Subject<boolean>()
export function isLoggedIn() {
return loggedIn
}
export function getUser() {
return user;
}
export function getPrivileges() {
return privileges
}
function setToken(token: string) {
window.localStorage.setItem("token", token)
}
export function getToken(): string {
return window.localStorage.getItem("token")
}
export async function login(username: string, password: string): Promise<{
success: boolean,
token?: string
}> {
return api("/auth", "POST", {
username, password
}).then(res => res.json() as Promise<{
success: boolean,
token?: string
privileges?: string[]
}>).then(res => {
if(res.success) {
loggedIn = true
user = username;
privileges = res.privileges ?? []
privileges.push(username)
setToken(res.token ?? "")
loggedInSubject.next(true)
}
return Promise.resolve(res)
})
}
export function logout(){
setToken("")
loggedIn = false;
user = "";
privileges = []
loggedInSubject.next(false)
}
export function checkForToken() {
const token = getToken()
if(token && token != ""){
const payload = parseJwt(token)
loggedIn = true
user = payload.username
privileges = payload.privileges
privileges.push(user)
loggedInSubject.next(true)
}
}
function parseJwt (token) {
var base64Url = token.split('.')[1];
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
return JSON.parse(jsonPayload);
}
}