Improve Angular project structure and code organization (#1328)

This commit is contained in:
Aditya Chandel
2025-10-12 02:01:59 -06:00
committed by GitHub
parent 54a8bacccd
commit 7c9bd56f63
405 changed files with 463 additions and 727 deletions
@@ -0,0 +1,120 @@
<form [formGroup]="userForm" (ngSubmit)="createUser()" class="space-y-6 p-6">
<div class="flex items-center gap-4">
<label class="w-40 text-right font-medium">Full Name:</label>
<input type="text" pInputText formControlName="name" placeholder="Enter full name..." class="w-full md:w-80"/>
</div>
@if (userForm.get('name')?.invalid && (userForm.get('name')?.dirty || userForm.get('name')?.touched)) {
<small class="text-red-500 pl-44">
Name is required and must be at least 3 characters.
</small>
}
<div class="flex items-center gap-4">
<label class="w-40 text-right font-medium">Email:</label>
<input type="email" pInputText formControlName="email" placeholder="Enter email..." class="w-full md:w-80"/>
</div>
@if (userForm.get('email')?.invalid && (userForm.get('email')?.dirty || userForm.get('email')?.touched)) {
<small class="text-red-500 pl-44">
Enter a valid email address.
</small>
}
<div class="flex items-center gap-4">
<label class="w-40 text-right font-medium">Username:</label>
<input type="text" pInputText formControlName="username" placeholder="Enter username..." class="w-full md:w-80"/>
</div>
@if (userForm.get('username')?.invalid && (userForm.get('username')?.dirty || userForm.get('username')?.touched)) {
<small class="text-red-500 pl-44">
Username is required.
</small>
}
<div class="flex items-center gap-4">
<label class="w-40 text-right font-medium">Password:</label>
<input type="password" pInputText formControlName="password" placeholder="Enter password..." class="w-full md:w-80"/>
</div>
@if (userForm.get('password')?.invalid && (userForm.get('password')?.dirty || userForm.get('password')?.touched)) {
<small class="text-red-500 pl-44">
Password must be at least 6 characters long.
</small>
}
<div class="flex items-center gap-4">
<label class="w-40 text-right font-medium">Accessible Libraries:</label>
<p-multiselect
[options]="libraries"
formControlName="selectedLibraries"
placeholder="Select Libraries"
optionLabel="name"
display="chip"
class="w-full md:w-80"
appendTo="body">
</p-multiselect>
</div>
@if (userForm.get('selectedLibraries')?.invalid && (userForm.get('selectedLibraries')?.dirty || userForm.get('selectedLibraries')?.touched)) {
<small class="text-red-500 pl-44">
At least one library must be selected.
</small>
}
<div>
<p class="text-right w-40 font-medium">Permissions:</p>
<div class="flex flex-col gap-2 pl-44">
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionUpload" [binary]="true"></p-checkbox>
<label>Upload Books</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionDownload" [binary]="true"></p-checkbox>
<label>Download Books</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionEditMetadata" [binary]="true"></p-checkbox>
<label>Edit Metadata</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionManipulateLibrary" [binary]="true"></p-checkbox>
<label>Manage Library</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionEmailBook" [binary]="true"></p-checkbox>
<label>Email Book</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionDeleteBook" [binary]="true"></p-checkbox>
<label>Delete Book</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionAccessOpds" [binary]="true"></p-checkbox>
<label>Access OPDS</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionSyncKoreader" [binary]="true"></p-checkbox>
<label>KOReader Sync</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionSyncKobo" [binary]="true"></p-checkbox>
<label>Kobo Sync</label>
</div>
<div class="flex items-center gap-2">
<p-checkbox formControlName="permissionAdmin" [binary]="true"></p-checkbox>
<label class="text-orange-500 font-bold">Admin</label>
</div>
</div>
</div>
<div class="flex justify-end">
<p-button label="Create User" [disabled]="userForm.invalid" type="submit">
</p-button>
</div>
</form>
@@ -0,0 +1,95 @@
import {Component, inject, OnInit} from '@angular/core';
import {InputText} from 'primeng/inputtext';
import {FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators} from '@angular/forms';
import {Checkbox} from 'primeng/checkbox';
import {MultiSelectModule} from 'primeng/multiselect';
import {Library} from '../../../book/model/library.model';
import {Button} from 'primeng/button';
import {LibraryService} from '../../../book/service/library.service';
import {UserService} from '../user.service';
import {MessageService} from 'primeng/api';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
@Component({
selector: 'app-create-user-dialog',
standalone: true,
imports: [
InputText,
ReactiveFormsModule,
FormsModule,
Checkbox,
MultiSelectModule,
Button
],
templateUrl: './create-user-dialog.component.html',
styleUrl: './create-user-dialog.component.scss'
})
export class CreateUserDialogComponent implements OnInit {
userForm!: FormGroup;
libraries: Library[] = [];
private fb = inject(FormBuilder);
private libraryService = inject(LibraryService);
private userService = inject(UserService);
private messageService = inject(MessageService);
private ref = inject(DynamicDialogRef);
ngOnInit() {
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],
permissionUpload: [false],
permissionDownload: [false],
permissionEditMetadata: [false],
permissionManipulateLibrary: [false],
permissionEmailBook: [false],
permissionDeleteBook: [false],
permissionAccessOpds: [false],
permissionSyncKoreader: [false],
permissionSyncKobo: [false],
permissionAdmin: [false],
});
}
createUser() {
if (this.userForm.invalid) {
this.messageService.add({
severity: 'warn',
summary: 'Validation Error',
detail: 'Please correct errors before submitting.'
});
return;
}
const userData = {
...this.userForm.value,
selectedLibraries: this.userForm.value.selectedLibraries.map((lib: Library) => lib.id)
};
this.userService.createUser(userData).subscribe({
next: () => {
this.messageService.add({
severity: 'success',
summary: 'User Created',
detail: 'The user has been successfully created.'
});
this.ref.close(true);
},
error: (err) => {
this.messageService.add({
severity: 'error',
summary: 'User Creation Failed',
detail: err?.error?.message
? `Unable to create user: ${err.error.message}`
: 'An unexpected error occurred while creating the user. Please try again later.'
});
}
});
}
}