174 lines
5.0 KiB
TypeScript
174 lines
5.0 KiB
TypeScript
import {
|
|
Component,
|
|
inject,
|
|
OnDestroy,
|
|
signal,
|
|
DestroyRef,
|
|
OnInit,
|
|
} from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { Router, RouterLink } from '@angular/router';
|
|
import {
|
|
FormBuilder,
|
|
FormGroup,
|
|
ReactiveFormsModule,
|
|
Validators,
|
|
} from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatCheckboxModule } from '@angular/material/checkbox';
|
|
import { CredentialService } from '../../shared/services/credential.service';
|
|
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
|
import {
|
|
Subscription,
|
|
map,
|
|
switchMap,
|
|
debounceTime,
|
|
distinctUntilChanged,
|
|
catchError,
|
|
of,
|
|
} from 'rxjs';
|
|
import {
|
|
MatAutocompleteModule,
|
|
MatAutocompleteSelectedEvent,
|
|
} from '@angular/material/autocomplete';
|
|
import { InstitutionsService } from '../../api';
|
|
@Component({
|
|
selector: 'app-register',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
ReactiveFormsModule,
|
|
MatButtonModule,
|
|
MatFormFieldModule,
|
|
MatInputModule,
|
|
MatIconModule,
|
|
MatCheckboxModule,
|
|
RouterLink,
|
|
MatSnackBarModule,
|
|
MatAutocompleteModule,
|
|
],
|
|
templateUrl: './app-register.component.html',
|
|
styleUrl: './app-register.component.scss',
|
|
})
|
|
export class AppRegisterComponent implements OnDestroy, OnInit {
|
|
private readonly fb = inject(FormBuilder);
|
|
protected readonly credentialService = inject(CredentialService);
|
|
private readonly router = inject(Router);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
private subscription?: Subscription;
|
|
private institutionsService = inject(InstitutionsService);
|
|
registerForm: FormGroup = this.fb.group({
|
|
username: ['', [Validators.required, Validators.minLength(4)]],
|
|
email: ['', [Validators.required, Validators.email]],
|
|
password: ['', [Validators.required, Validators.minLength(8)]],
|
|
institution: ['', [Validators.required]],
|
|
superadmin: [false],
|
|
});
|
|
readonly institutionOptions = signal<any[]>([]);
|
|
showPassword = false;
|
|
|
|
constructor() {
|
|
this.setupInstitutionAutocomplete();
|
|
}
|
|
ngOnInit(): void {
|
|
this.subscription = this.credentialService.validating.subscribe(
|
|
(validating) => {
|
|
if (validating) {
|
|
this.registerForm.disable();
|
|
} else {
|
|
this.registerForm.enable();
|
|
this.checkEnter();
|
|
}
|
|
},
|
|
);
|
|
if (!this.credentialService.validating) {
|
|
this.checkEnter();
|
|
}
|
|
}
|
|
ngOnDestroy(): void {
|
|
this.subscription?.unsubscribe();
|
|
}
|
|
|
|
checkEnter(): void {
|
|
if (this.credentialService.isAuthenticated.value) {
|
|
this.router.navigate(['/dashboard']);
|
|
}
|
|
}
|
|
submit() {
|
|
if (this.registerForm.valid) {
|
|
this.credentialService.validating.next(true);
|
|
this.registerForm.disable();
|
|
|
|
this.subscription = this.credentialService
|
|
.registerAccount(this.registerForm.value)
|
|
.subscribe({
|
|
next: () => {
|
|
this.credentialService.validating.next(false);
|
|
this.snackBar.open(
|
|
'Konto erfolgreich erstellt! Du kannst Dich jetzt anmelden.',
|
|
'OK',
|
|
{ duration: 5000 },
|
|
);
|
|
this.router.navigate(['/login']);
|
|
},
|
|
error: (err) => {
|
|
this.credentialService.validating.next(false);
|
|
this.registerForm.enable();
|
|
const message =
|
|
err.error?.message ||
|
|
'Fehler bei der Registrierung. Bitte versuche es später erneut.';
|
|
this.snackBar.open(message, 'Schließen', {
|
|
panelClass: ['error-snackbar'],
|
|
duration: 5000,
|
|
});
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
private setupInstitutionAutocomplete(): void {
|
|
this.registerForm.controls['institution'].valueChanges
|
|
.pipe(
|
|
debounceTime(250),
|
|
map((value) => (typeof value === 'string' ? value.trim() : '')),
|
|
distinctUntilChanged(),
|
|
switchMap((query) => {
|
|
if (query.length < 2) {
|
|
this.institutionOptions.set([]);
|
|
return of([]);
|
|
}
|
|
|
|
return this.searchInstitutions(query).pipe(
|
|
catchError((ev) => {
|
|
console.log(ev);
|
|
return of([]);
|
|
}),
|
|
);
|
|
}),
|
|
takeUntilDestroyed(this.destroyRef),
|
|
)
|
|
.subscribe((options) => {
|
|
this.institutionOptions.set(options);
|
|
});
|
|
}
|
|
|
|
private searchInstitutions(query: string) {
|
|
return this.institutionsService
|
|
.autocompleteInstitutions(query, undefined, undefined, 10)
|
|
.pipe(map((response) => response.results ?? []));
|
|
}
|
|
|
|
onInstitutionSelected(event: MatAutocompleteSelectedEvent): void {
|
|
const val = event.option.value;
|
|
this.registerForm.controls['institution'].setValue(val, {
|
|
emitEvent: false,
|
|
});
|
|
this.institutionOptions.set([]);
|
|
}
|
|
}
|