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 @@

Hello {{account.username}}!

Welcome to the admin panel

- -

Change password

-
- - New password - this field must not be empty - - - - Retype new password - this field must not be empty - - -
- please type your new password the same way twice. -
- -
-

Post status

@@ -41,4 +22,34 @@
+ +

Update quote

+
+ + Quote + this field must not be empty + + + +
+
+ +

Change password

+
+ + New password + this field must not be empty + + + + Retype new password + this field must not be empty + + +
+ please type your new password the same way twice. +
+ +
+
\ No newline at end of file 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 @@

Hello {{account.username}}!

Welcome to the admin panel

- -

Change password

-
- - New password - this field must not be empty - - - - Retype new password - this field must not be empty - - -
- please type your new password the same way twice. -
- -
-

Post status

@@ -41,4 +22,34 @@
+ +

Update quote

+
+ + Quote + this field must not be empty + + + +
+
+ +

Change password

+
+ + New password + this field must not be empty + + + + Retype new password + this field must not be empty + + +
+ please type your new password the same way twice. +
+ +
+
\ No newline at end of file diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index c96f14b..4b518fa 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -16,6 +16,7 @@ export class AdminComponent implements OnInit { passwordForm: FormGroup statusForm: FormGroup + quoteForm: FormGroup public passwordError = false public username = '' timeOptions: TimeEntry[] = [ @@ -35,6 +36,9 @@ status: ['', Validators.required], time: [null], }) + this.quoteForm = this.formBuilder.group({ + quote: ['', Validators.required], + }) account.subscribe((username: string) => { if (account.username.length == 0) { router.navigate(['login']) @@ -44,7 +48,7 @@ } ngOnInit(): void { - var form = this.statusForm.get('time')?.setValue(60) + this.statusForm.get('time')?.setValue(60) } onPasswordSubmit() { @@ -67,4 +71,11 @@ } this.account.postStatus(this.statusForm.controls['status'].value, this.statusForm.controls['time'].value) } + + onQuoteSubmit() { + if (this.quoteForm.invalid) { + return + } + this.account.updateQuote(this.quoteForm.controls['quote'].value) + } } 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 @@

Hello {{account.username}}!

Welcome to the admin panel

- -

Change password

-
- - New password - this field must not be empty - - - - Retype new password - this field must not be empty - - -
- please type your new password the same way twice. -
- -
-

Post status

@@ -41,4 +22,34 @@
+ +

Update quote

+
+ + Quote + this field must not be empty + + + +
+
+ +

Change password

+
+ + New password + this field must not be empty + + + + Retype new password + this field must not be empty + + +
+ please type your new password the same way twice. +
+ +
+
\ No newline at end of file diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index c96f14b..4b518fa 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -16,6 +16,7 @@ export class AdminComponent implements OnInit { passwordForm: FormGroup statusForm: FormGroup + quoteForm: FormGroup public passwordError = false public username = '' timeOptions: TimeEntry[] = [ @@ -35,6 +36,9 @@ status: ['', Validators.required], time: [null], }) + this.quoteForm = this.formBuilder.group({ + quote: ['', Validators.required], + }) account.subscribe((username: string) => { if (account.username.length == 0) { router.navigate(['login']) @@ -44,7 +48,7 @@ } ngOnInit(): void { - var form = this.statusForm.get('time')?.setValue(60) + this.statusForm.get('time')?.setValue(60) } onPasswordSubmit() { @@ -67,4 +71,11 @@ } this.account.postStatus(this.statusForm.controls['status'].value, this.statusForm.controls['time'].value) } + + onQuoteSubmit() { + if (this.quoteForm.invalid) { + return + } + this.account.updateQuote(this.quoteForm.controls['quote'].value) + } } diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html index b7052eb..bd51544 100644 --- a/src/app/home/home.component.html +++ b/src/app/home/home.component.html @@ -44,12 +44,23 @@ The backend is made using Ruby on rails.

- -

User statuses:

-
+ +

Quotes:

+
- + +
+
{{quote.quote}}
-{{quote.user}}
+
+
+
+ +

User statuses:

+
+ +
+

{{userStatus.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 @@

Hello {{account.username}}!

Welcome to the admin panel

- -

Change password

-
- - New password - this field must not be empty - - - - Retype new password - this field must not be empty - - -
- please type your new password the same way twice. -
- -
-

Post status

@@ -41,4 +22,34 @@
+ +

Update quote

+
+ + Quote + this field must not be empty + + + +
+
+ +

Change password

+
+ + New password + this field must not be empty + + + + Retype new password + this field must not be empty + + +
+ please type your new password the same way twice. +
+ +
+
\ No newline at end of file diff --git a/src/app/admin/admin.component.ts b/src/app/admin/admin.component.ts index c96f14b..4b518fa 100644 --- a/src/app/admin/admin.component.ts +++ b/src/app/admin/admin.component.ts @@ -16,6 +16,7 @@ export class AdminComponent implements OnInit { passwordForm: FormGroup statusForm: FormGroup + quoteForm: FormGroup public passwordError = false public username = '' timeOptions: TimeEntry[] = [ @@ -35,6 +36,9 @@ status: ['', Validators.required], time: [null], }) + this.quoteForm = this.formBuilder.group({ + quote: ['', Validators.required], + }) account.subscribe((username: string) => { if (account.username.length == 0) { router.navigate(['login']) @@ -44,7 +48,7 @@ } ngOnInit(): void { - var form = this.statusForm.get('time')?.setValue(60) + this.statusForm.get('time')?.setValue(60) } onPasswordSubmit() { @@ -67,4 +71,11 @@ } this.account.postStatus(this.statusForm.controls['status'].value, this.statusForm.controls['time'].value) } + + onQuoteSubmit() { + if (this.quoteForm.invalid) { + return + } + this.account.updateQuote(this.quoteForm.controls['quote'].value) + } } diff --git a/src/app/home/home.component.html b/src/app/home/home.component.html index b7052eb..bd51544 100644 --- a/src/app/home/home.component.html +++ b/src/app/home/home.component.html @@ -44,12 +44,23 @@ The backend is made using Ruby on rails.

- -

User statuses:

-
+ +

Quotes:

+
- + +
+
{{quote.quote}}
-{{quote.user}}
+
+
+
+ +

User statuses:

+
+ +
+

{{userStatus.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 + } + }) } }