<div class="container card-body form-inline">
<div class="row form-group">
<div class="col input-group input-group-primary">
- <button type="submit" class="btn btn-primary" (click)="createUserId()">Create a new user-ID</button>
+ <button type="submit" class="btn btn-primary" (click)="createUserId()" [disabled]="loading">Create a new user-ID</button>
</div>
<div class="col input-group input-group-primary">
<label for="id" class="input-group-text">User-ID</label>
<input id="id" type="text" class="form-control" placeholder="Enter your user-ID" [formControl]="useridForm">
- <button type="submit" class="btn btn-primary" (click)="updateUserId()" [disabled]="useridForm.invalid">Send</button>
+ <button type="submit" class="btn btn-primary" (click)="updateUserId()" [disabled]="useridForm.invalid || loading">Send</button>
</div>
</div>
</div>
<div class="container card-footer">
+ @if (error) {
+ <div class="row">
+ <div class="col alert alert-primary" role="alert">{{ error }}</div>
+ </div>
+ }
<div class="row">
<div class="col">Value: {{ useridForm.value }}</div>
<div class="col">Form Status: {{ useridForm.status }}</div>
-import { Component } from '@angular/core';
-import {
- FormControl,
- ReactiveFormsModule,
- Validators,
- ValidationErrors,
-} from '@angular/forms';
+import { Component, inject } from '@angular/core';
+import { FormControl, ReactiveFormsModule, ValidationErrors, Validators, } from '@angular/forms';
import { Router } from "@angular/router";
import { UserService } from "./index";
-import { UUID_V4_FORMAT } from '@pact-foundation/pact/src/dsl/matchers';
const UUID_FORMAT = "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$";
})
export class UserComponent {
+ private userService = inject(UserService);
+ private router = inject(Router);
useridForm = new FormControl(
'',
Validators.pattern(UUID_FORMAT)
]);
- updateUserId(): void {
+ error?: string;
+ loading = false;
+
+
+ async updateUserId() {
+ this.error = undefined;
+ this.loading = true;
+
let input = this.useridForm.getRawValue();
+
if (input !== null) {
- this.userService.setUser(input.trim());
- this.router.navigate(['chatrooms'])
+ try {
+ await this.userService.setUser(input.trim());
+ this.router.navigate(['chatrooms'])
+ }
+ catch (error) {
+ const message = (error as any)?.message ?? (error as any)?.error?.message ?? String(error);
+ this.error = 'Fehler beim laden des Benutzers: ' + message;
+ }
+ finally {
+ this.loading = false;
+ }
}
}
- createUserId(): void {
- this.userService.createUser();
+ async createUserId() {
+ this.error = undefined;
+ this.loading = true;
+
+ try {
+ await this.userService.createUser();
+ }
+ catch (error) {
+ const message = (error as any)?.message ?? (error as any)?.error?.message ?? String(error);
+ this.error = 'Fehler beim erzeugen des Benutzers: ' + message;
+ }
+ finally {
+ this.loading = false;
+ }
}
- noWhitespaceValidator(control: FormControl) : ValidationErrors {
+ private noWhitespaceValidator(control: FormControl) : ValidationErrors {
const isWhitespace = (control.value || '').trim().length === 0;
const isValid = !isWhitespace;
return isValid ? {} : { 'whitespace': true };
}
-
- constructor(
- private userService: UserService,
- private router: Router) {}
}