cic-staff-client/src/app/auth/auth.component.ts

81 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-05-10 18:15:25 +02:00
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { CustomErrorStateMatcher } from '@app/_helpers';
import { AuthService } from '@app/_services';
import { ErrorDialogService } from '@app/_services/error-dialog.service';
2021-05-10 18:15:25 +02:00
import { Router } from '@angular/router';
2020-12-28 10:11:25 +01:00
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.scss'],
2021-05-10 18:15:25 +02:00
changeDetection: ChangeDetectionStrategy.OnPush,
2020-12-28 10:11:25 +01:00
})
export class AuthComponent implements OnInit {
2021-02-03 11:52:31 +01:00
keyForm: FormGroup;
2020-12-28 10:11:25 +01:00
submitted: boolean = false;
loading: boolean = false;
2021-04-30 14:50:16 +02:00
matcher: CustomErrorStateMatcher = new CustomErrorStateMatcher();
2020-12-28 10:11:25 +01:00
constructor(
2021-02-03 11:52:31 +01:00
private authService: AuthService,
2020-12-28 10:11:25 +01:00
private formBuilder: FormBuilder,
private router: Router,
2021-06-10 16:29:23 +02:00
private errorDialogService: ErrorDialogService
2021-05-10 18:15:25 +02:00
) {}
2020-12-28 10:11:25 +01:00
ngOnInit(): void {
2021-02-03 11:52:31 +01:00
this.keyForm = this.formBuilder.group({
key: ['', Validators.required],
2020-12-28 10:11:25 +01:00
});
2021-06-12 18:18:17 +02:00
if (this.authService.getPrivateKey()) {
2021-06-14 13:08:58 +02:00
this.authService.loginView();
2021-06-12 18:18:17 +02:00
}
2021-02-03 11:52:31 +01:00
}
2021-05-10 18:15:25 +02:00
get keyFormStub(): any {
return this.keyForm.controls;
}
2020-12-28 10:11:25 +01:00
async onSubmit(): Promise<void> {
2020-12-28 10:11:25 +01:00
this.submitted = true;
2021-05-10 18:15:25 +02:00
if (this.keyForm.invalid) {
return;
}
2020-12-28 10:11:25 +01:00
this.loading = true;
await this.authService.setKey(this.keyFormStub.key.value);
2021-02-03 11:52:31 +01:00
this.loading = false;
}
2021-06-09 02:59:01 +02:00
async login(): Promise<void> {
try {
2021-06-10 16:29:23 +02:00
const loginResult = await this.authService.login();
if (loginResult) {
this.router.navigate(['/home']);
2021-06-10 16:29:23 +02:00
}
} catch (HttpError) {
this.errorDialogService.openDialog({
message: 'Failed to login please try again.',
});
}
2021-02-03 11:52:31 +01:00
}
switchWindows(): void {
2021-04-30 14:50:16 +02:00
const divOne: HTMLElement = document.getElementById('one');
const divTwo: HTMLElement = document.getElementById('two');
2021-02-03 11:52:31 +01:00
this.toggleDisplay(divOne);
this.toggleDisplay(divTwo);
}
toggleDisplay(element: any): void {
2021-04-30 14:50:16 +02:00
const style: string = window.getComputedStyle(element).display;
2021-02-03 11:52:31 +01:00
if (style === 'block') {
element.style.display = 'none';
} else {
element.style.display = 'block';
}
2020-12-28 10:11:25 +01:00
}
}