diff --git a/src/app/account.service.ts b/src/app/account.service.ts index aa39dec..e0690a2 100644 --- a/src/app/account.service.ts +++ b/src/app/account.service.ts @@ -26,13 +26,15 @@ dat: string = '' typ: string = '' usr: string = '' + admin: number = 0 } @Injectable({ providedIn: 'root' }) export class AccountService { - public username = '?'; + public username = '?' + public isAdmin = false private callbacks: Function[] = [] subscribe(f: Function) { @@ -87,6 +89,7 @@ var data = JSON.parse(atob(token?.split('.')[1] as string)) var tokenData = Object.assign(new TokenData(), data) this.setUser(tokenData.usr) + this.isAdmin = tokenData.admin == 1 this.http.post(`${environment.apiURL}/users/checkToken`, {token, username: this.username}) .subscribe((answer: Object) => { var checkTokenAnswer = Object.assign(new CheckTokenAnswer(), answer) @@ -145,4 +148,16 @@ } }) } + + updateQuote(quote: string) { + this.http.post(`${environment.apiURL}/users/updateQuote`, + {username: this.username, quote, token: localStorage.getItem('userToken')}).subscribe((answer) => { + var status = Object.assign(new Answer(), answer) + if (status.success) { + Swal.fire('You updated your quote', 'Everyone can see it', 'success') + } else { + Swal.fire('Error!', 'Something went wrong, bad user token?', 'error') + } + }) + } } diff --git a/src/app/account.service.ts b/src/app/account.service.ts index aa39dec..e0690a2 100644 --- a/src/app/account.service.ts +++ b/src/app/account.service.ts @@ -26,13 +26,15 @@ dat: string = '' typ: string = '' usr: string = '' + admin: number = 0 } @Injectable({ providedIn: 'root' }) export class AccountService { - public username = '?'; + public username = '?' + public isAdmin = false private callbacks: Function[] = [] subscribe(f: Function) { @@ -87,6 +89,7 @@ var data = JSON.parse(atob(token?.split('.')[1] as string)) var tokenData = Object.assign(new TokenData(), data) this.setUser(tokenData.usr) + this.isAdmin = tokenData.admin == 1 this.http.post(`${environment.apiURL}/users/checkToken`, {token, username: this.username}) .subscribe((answer: Object) => { var checkTokenAnswer = Object.assign(new CheckTokenAnswer(), answer) @@ -145,4 +148,16 @@ } }) } + + updateQuote(quote: string) { + this.http.post(`${environment.apiURL}/users/updateQuote`, + {username: this.username, quote, token: localStorage.getItem('userToken')}).subscribe((answer) => { + var status = Object.assign(new Answer(), answer) + if (status.success) { + Swal.fire('You updated your quote', 'Everyone can see it', 'success') + } else { + Swal.fire('Error!', 'Something went wrong, bad user token?', 'error') + } + }) + } } diff --git a/src/app/admin/admin.component.html b/src/app/admin/admin.component.html index 9052a81..fa0cbf0 100644 --- a/src/app/admin/admin.component.html +++ b/src/app/admin/admin.component.html @@ -3,25 +3,6 @@
{{quote.quote}}+
-{{quote.user}}
{{userStatus.message}}
diff --git a/src/app/account.service.ts b/src/app/account.service.ts index aa39dec..e0690a2 100644 --- a/src/app/account.service.ts +++ b/src/app/account.service.ts @@ -26,13 +26,15 @@ dat: string = '' typ: string = '' usr: string = '' + admin: number = 0 } @Injectable({ providedIn: 'root' }) export class AccountService { - public username = '?'; + public username = '?' + public isAdmin = false private callbacks: Function[] = [] subscribe(f: Function) { @@ -87,6 +89,7 @@ var data = JSON.parse(atob(token?.split('.')[1] as string)) var tokenData = Object.assign(new TokenData(), data) this.setUser(tokenData.usr) + this.isAdmin = tokenData.admin == 1 this.http.post(`${environment.apiURL}/users/checkToken`, {token, username: this.username}) .subscribe((answer: Object) => { var checkTokenAnswer = Object.assign(new CheckTokenAnswer(), answer) @@ -145,4 +148,16 @@ } }) } + + updateQuote(quote: string) { + this.http.post(`${environment.apiURL}/users/updateQuote`, + {username: this.username, quote, token: localStorage.getItem('userToken')}).subscribe((answer) => { + var status = Object.assign(new Answer(), answer) + if (status.success) { + Swal.fire('You updated your quote', 'Everyone can see it', 'success') + } else { + Swal.fire('Error!', 'Something went wrong, bad user token?', 'error') + } + }) + } } diff --git a/src/app/admin/admin.component.html b/src/app/admin/admin.component.html index 9052a81..fa0cbf0 100644 --- a/src/app/admin/admin.component.html +++ b/src/app/admin/admin.component.html @@ -3,25 +3,6 @@{{quote.quote}}+
-{{quote.user}}
{{userStatus.message}}
diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 47e5069..28dfbaa 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -1,13 +1,21 @@ +import { FormStyle } from '@angular/common'; import { HttpClient } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; +import { interval } from 'rxjs'; import { environment } from 'src/environments/environment'; -class UserStatus { +class Status { constructor(public user: string, public message: string) {} } +class StatusAnswer { + constructor(public success: boolean = false, public userStatuses: Status[] = []) {} +} -class UserStatusAnswer { - constructor(public success: boolean = false, public userStatuses: UserStatus[] = []) {} +class Quote { + constructor(public user: string, public quote: string) {} +} +class QuotesAnswer { + constructor(public success: boolean = false, public quotes: Quote[] = []) {} } @Component({ @@ -16,19 +24,34 @@ styleUrls: ['./home.component.scss'] }) export class HomeComponent implements OnInit { - userStatuses: UserStatus[] = [] - loading = true + userStatuses: Status[] = [] + quotes: Quote[] = [] + loadingStatuses = true + loadingQuotes = true constructor(private http: HttpClient) { } ngOnInit(): void { + this.updateElements() + interval(20000).subscribe((() => { + this.updateElements() + })) + } + + updateElements() { this.http.get(`${environment.apiURL}/users/status`).subscribe((answer) => { - var userStatusAnswer = Object.assign(new UserStatusAnswer(), answer) - this.loading = !userStatusAnswer.success - if (!this.loading) { + var userStatusAnswer = Object.assign(new StatusAnswer(), answer) + this.loadingStatuses &&= !userStatusAnswer.success + if (userStatusAnswer.success) { this.userStatuses = userStatusAnswer.userStatuses } - } - ) + }) + this.http.get(`${environment.apiURL}/users/quotes`).subscribe((answer) => { + var quotesAnswer = Object.assign(new QuotesAnswer(), answer) + this.loadingQuotes &&= !quotesAnswer.success + if (quotesAnswer.success) { + this.quotes = quotesAnswer.quotes + } + }) } }