aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/app.component.ts
blob: 2fecc2d5ddb99a85e2759c63ae114570e2534205 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import {Component, OnInit, Inject, ViewContainerRef} from '@angular/core';
import {BehaviorSubject} from 'rxjs';
import { DOCUMENT } from '@angular/common';
import {AuthService} from './services/auth.service';
import {ToastsService} from './services/toasts.service';
import {User, userType} from './models/User';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  standalone: false,
  styleUrl: './app.component.css'
})
export class AppComponent implements OnInit {
    // title = 'ufund-ui';
    currentUser?: BehaviorSubject<User | null>;

    constructor(
        private authService: AuthService,
        private router: Router,
        private route: ActivatedRoute,
        protected toastService: ToastsService,
        private viewContainerRef: ViewContainerRef,
        @Inject(DOCUMENT) private document: Document
    ) {}

    reloadPage() {
        this.document.defaultView?.location.reload();
    }

    ngOnInit() {
        this.toastService.setRootViewContainerRef(this.viewContainerRef)
        this.currentUser = this.authService.getCurrentUserSubject()
        let data = localStorage.getItem("credential");
        if (data) {
            let dataParsed = JSON.parse(data)
            this.authService.restoreLogin(dataParsed.username, dataParsed.key)
            console.log("Key found", dataParsed.key)
        }

        let theme = localStorage.getItem("theme")
        if(!theme) {
            // if no color scheme is set, get the system settings
            let preferredTheme = this.document.defaultView?.matchMedia('(prefers-color-scheme: light)').matches ? "light" : "dark";
            localStorage.setItem("theme",preferredTheme);
            theme = preferredTheme;
        }
        this.document.body.parentElement!.setAttribute("theme",theme);
    }

    login() {
        this.router.navigate(['/login'], {queryParams: {redir: this.router.url}});
    }

    logout() {
        localStorage.removeItem("credential")
        location.reload()
    }

    toggleColorScheme() {
        let theme = localStorage.getItem("theme");
        // fallback
        if (!theme) {
            theme = "light";
        }
        let newTheme = theme == "light" ? "dark" : "light";
        this.document.body.parentElement!.setAttribute("theme",newTheme);
        localStorage.setItem("theme", newTheme);
        console.log(newTheme, this.document.body.parentElement);
    }

    protected readonly userType = userType;
}