Switch to reactive form
This commit is contained in:
+27
-15
@@ -1,29 +1,42 @@
|
|||||||
<div class="space-y-6 p-6">
|
<form [formGroup]="userForm" (ngSubmit)="createUser()" class="space-y-6 p-6">
|
||||||
|
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex items-center gap-4">
|
||||||
<label class="w-40 text-right font-medium">Full Name:</label>
|
<label class="w-40 text-right font-medium">Full Name:</label>
|
||||||
<input type="text" pInputText [(ngModel)]="name" placeholder="Enter full name..." class="w-full md:w-80"/>
|
<input type="text" pInputText formControlName="name" placeholder="Enter full name..." class="w-full md:w-80"/>
|
||||||
</div>
|
</div>
|
||||||
|
<small class="text-red-500 pl-44" *ngIf="userForm.get('name')?.invalid && (userForm.get('name')?.dirty || userForm.get('name')?.touched)">
|
||||||
|
Name is required and must be at least 3 characters.
|
||||||
|
</small>
|
||||||
|
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex items-center gap-4">
|
||||||
<label class="w-40 text-right font-medium">Email:</label>
|
<label class="w-40 text-right font-medium">Email:</label>
|
||||||
<input type="email" pInputText [(ngModel)]="email" placeholder="Enter email..." class="w-full md:w-80"/>
|
<input type="email" pInputText formControlName="email" placeholder="Enter email..." class="w-full md:w-80"/>
|
||||||
</div>
|
</div>
|
||||||
|
<small class="text-red-500 pl-44" *ngIf="userForm.get('email')?.invalid && (userForm.get('email')?.dirty || userForm.get('email')?.touched)">
|
||||||
|
Enter a valid email address.
|
||||||
|
</small>
|
||||||
|
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex items-center gap-4">
|
||||||
<label class="w-40 text-right font-medium">Username:</label>
|
<label class="w-40 text-right font-medium">Username:</label>
|
||||||
<input type="text" pInputText [(ngModel)]="username" placeholder="Enter username..." class="w-full md:w-80"/>
|
<input type="text" pInputText formControlName="username" placeholder="Enter username..." class="w-full md:w-80"/>
|
||||||
</div>
|
</div>
|
||||||
|
<small class="text-red-500 pl-44" *ngIf="userForm.get('username')?.invalid && (userForm.get('username')?.dirty || userForm.get('username')?.touched)">
|
||||||
|
Username is required.
|
||||||
|
</small>
|
||||||
|
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex items-center gap-4">
|
||||||
<label class="w-40 text-right font-medium">Password:</label>
|
<label class="w-40 text-right font-medium">Password:</label>
|
||||||
<input type="password" pInputText [(ngModel)]="password" placeholder="Enter password..." class="w-full md:w-80"/>
|
<input type="password" pInputText formControlName="password" placeholder="Enter password..." class="w-full md:w-80"/>
|
||||||
</div>
|
</div>
|
||||||
|
<small class="text-red-500 pl-44" *ngIf="userForm.get('password')?.invalid && (userForm.get('password')?.dirty || userForm.get('password')?.touched)">
|
||||||
|
Password must be at least 6 characters long.
|
||||||
|
</small>
|
||||||
|
|
||||||
<div class="flex items-center gap-4">
|
<div class="flex items-center gap-4">
|
||||||
<label class="w-40 text-right font-medium">Accessible Libraries:</label>
|
<label class="w-40 text-right font-medium">Accessible Libraries:</label>
|
||||||
<p-multiselect
|
<p-multiselect
|
||||||
[options]="libraries"
|
[options]="libraries"
|
||||||
[(ngModel)]="selectedLibraries"
|
formControlName="selectedLibraries"
|
||||||
placeholder="Select Libraries"
|
placeholder="Select Libraries"
|
||||||
optionLabel="name"
|
optionLabel="name"
|
||||||
display="chip"
|
display="chip"
|
||||||
@@ -31,33 +44,32 @@
|
|||||||
appendTo="body">
|
appendTo="body">
|
||||||
</p-multiselect>
|
</p-multiselect>
|
||||||
</div>
|
</div>
|
||||||
|
<small class="text-red-500 pl-44" *ngIf="userForm.get('selectedLibraries')?.invalid && (userForm.get('selectedLibraries')?.dirty || userForm.get('selectedLibraries')?.touched)">
|
||||||
|
At least one library must be selected.
|
||||||
|
</small>
|
||||||
|
|
||||||
<!-- Grouped Permissions -->
|
|
||||||
<div>
|
<div>
|
||||||
<p class="text-right w-40 font-medium">Permissions:</p>
|
<p class="text-right w-40 font-medium">Permissions:</p>
|
||||||
<div class="flex flex-col gap-2 pl-44">
|
<div class="flex flex-col gap-2 pl-44">
|
||||||
<div class="flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<p-checkbox [(ngModel)]="canUpload" [binary]="true"></p-checkbox>
|
<p-checkbox formControlName="canUpload" [binary]="true"></p-checkbox>
|
||||||
<label>Upload Books</label>
|
<label>Upload Books</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<p-checkbox [(ngModel)]="canDownload" [binary]="true"></p-checkbox>
|
<p-checkbox formControlName="canDownload" [binary]="true"></p-checkbox>
|
||||||
<label>Download Books</label>
|
<label>Download Books</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<p-checkbox [(ngModel)]="canEditMetadata" [binary]="true"></p-checkbox>
|
<p-checkbox formControlName="canEditMetadata" [binary]="true"></p-checkbox>
|
||||||
<label>Edit Metadata</label>
|
<label>Edit Metadata</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="flex justify-end">
|
<div class="flex justify-end">
|
||||||
<p-button
|
<p-button label="Create User" [disabled]="userForm.invalid" type="submit">
|
||||||
label="Create User"
|
|
||||||
[disabled]="!username || password.length < 6 || !name || !email"
|
|
||||||
(onClick)="createUser()">
|
|
||||||
</p-button>
|
</p-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</form>
|
||||||
|
|||||||
+20
-22
@@ -1,6 +1,6 @@
|
|||||||
import {Component, inject, OnInit} from '@angular/core';
|
import {Component, inject, OnInit} from '@angular/core';
|
||||||
import {InputText} from 'primeng/inputtext';
|
import {InputText} from 'primeng/inputtext';
|
||||||
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
|
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms';
|
||||||
import {Checkbox} from 'primeng/checkbox';
|
import {Checkbox} from 'primeng/checkbox';
|
||||||
import {MultiSelectModule} from 'primeng/multiselect';
|
import {MultiSelectModule} from 'primeng/multiselect';
|
||||||
import {Library} from '../../../../../book/model/library.model';
|
import {Library} from '../../../../../book/model/library.model';
|
||||||
@@ -9,6 +9,7 @@ import {LibraryService} from '../../../../../book/service/library.service';
|
|||||||
import {UserService} from '../../../../../user.service';
|
import {UserService} from '../../../../../user.service';
|
||||||
import {MessageService} from 'primeng/api';
|
import {MessageService} from 'primeng/api';
|
||||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||||
|
import {NgIf} from '@angular/common';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-create-user-dialog',
|
selector: 'app-create-user-dialog',
|
||||||
@@ -19,23 +20,17 @@ import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
|||||||
FormsModule,
|
FormsModule,
|
||||||
Checkbox,
|
Checkbox,
|
||||||
MultiSelectModule,
|
MultiSelectModule,
|
||||||
Button
|
Button,
|
||||||
|
NgIf
|
||||||
],
|
],
|
||||||
templateUrl: './create-user-dialog.component.html',
|
templateUrl: './create-user-dialog.component.html',
|
||||||
styleUrl: './create-user-dialog.component.scss'
|
styleUrl: './create-user-dialog.component.scss'
|
||||||
})
|
})
|
||||||
export class CreateUserDialogComponent implements OnInit {
|
export class CreateUserDialogComponent implements OnInit {
|
||||||
|
userForm!: FormGroup;
|
||||||
libraries: Library[] = [];
|
libraries: Library[] = [];
|
||||||
selectedLibraries: Library[] = [];
|
|
||||||
|
|
||||||
username: string = '';
|
|
||||||
password: string = '';
|
|
||||||
name: string = '';
|
|
||||||
email: string = '';
|
|
||||||
canUpload: boolean = false;
|
|
||||||
canDownload: boolean = false;
|
|
||||||
canEditMetadata: boolean = false;
|
|
||||||
|
|
||||||
|
private fb = inject(FormBuilder);
|
||||||
private libraryService = inject(LibraryService);
|
private libraryService = inject(LibraryService);
|
||||||
private userService = inject(UserService);
|
private userService = inject(UserService);
|
||||||
private messageService = inject(MessageService);
|
private messageService = inject(MessageService);
|
||||||
@@ -43,27 +38,30 @@ export class CreateUserDialogComponent implements OnInit {
|
|||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.libraries = this.libraryService.getLibrariesFromState();
|
this.libraries = this.libraryService.getLibrariesFromState();
|
||||||
|
|
||||||
|
this.userForm = this.fb.group({
|
||||||
|
name: ['', [Validators.required, Validators.minLength(3)]],
|
||||||
|
email: ['', [Validators.required, Validators.email]],
|
||||||
|
username: ['', Validators.required],
|
||||||
|
password: ['', [Validators.required, Validators.minLength(6)]],
|
||||||
|
selectedLibraries: [[], Validators.required],
|
||||||
|
canUpload: [false],
|
||||||
|
canDownload: [false],
|
||||||
|
canEditMetadata: [false]
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
createUser() {
|
createUser() {
|
||||||
if (!this.username || this.password.length < 6 || !this.name || !this.email || this.selectedLibraries.length === 0) {
|
if (this.userForm.invalid) {
|
||||||
this.messageService.add({
|
this.messageService.add({
|
||||||
severity: 'warn',
|
severity: 'warn',
|
||||||
summary: 'Validation Error',
|
summary: 'Validation Error',
|
||||||
detail: 'Please fill all required fields correctly.'
|
detail: 'Please correct errors before submitting.'
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userData = {
|
const userData = this.userForm.value;
|
||||||
username: this.username,
|
|
||||||
password: this.password,
|
|
||||||
name: this.name,
|
|
||||||
email: this.email,
|
|
||||||
permissionUpload: this.canUpload,
|
|
||||||
permissionDownload: this.canDownload,
|
|
||||||
permissionEditMetadata: this.canEditMetadata
|
|
||||||
};
|
|
||||||
|
|
||||||
this.userService.createUser(userData).subscribe({
|
this.userService.createUser(userData).subscribe({
|
||||||
next: response => {
|
next: response => {
|
||||||
|
|||||||
Reference in New Issue
Block a user