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,149 @@
<div class="w-full flex justify-center">
<p-stepper [value]="1">
<p-step-list>
<p-step [value]="1">Library Details</p-step>
<p-step [value]="2">Select Directories</p-step>
</p-step-list>
<p-step-panels>
<p-step-panel [value]="1">
<ng-template #content let-activateCallback="activateCallback">
<div class="flex flex-col justify-between w-full md:w-[45rem] max-w-[50rem] mx-auto">
<div class="flex-grow flex items-center justify-center">
<div class="grid grid-cols-[auto,1fr] gap-x-6 gap-y-10 border border-[var(--border-color)] p-6 rounded-xl w-full">
<label class="self-center">Library Name</label>
<input
pInputText
class="w-full"
[(ngModel)]="chosenLibraryName"
placeholder="Enter library name..."/>
<label class="self-center">Library Icon</label>
@if (!selectedIcon) {
<p-button label="Select Icon" icon="pi pi-search" (onClick)="openIconPicker()"/>
} @else {
<div class="flex items-center gap-2">
<i [class]="selectedIcon" class="text-3xl"></i>
<p-button
icon="pi pi-times"
(onClick)="clearSelectedIcon()"
[rounded]="true"
[text]="true"
[outlined]="true"
severity="danger"/>
</div>
}
<label class="self-center">Import Mode</label>
<div class="flex items-center gap-2">
<p-select
[(ngModel)]="scanMode"
[options]="scanModeOptions"
optionLabel="label"
optionValue="value"
placeholder="Choose how to scan books"
class="w-full"/>
<i
class="pi pi-info-circle text-sky-600 cursor-pointer"
pTooltip="Select how BookLore interprets your files during import:
• File as Book: Each file becomes a separate book.
• Folder as Book: Each folder is imported as a single book, including multiple formats or extra files.
If you are unsure, choose 'File as Book'."
tooltipPosition="right"></i>
</div>
@if (scanMode == 'FOLDER_AS_BOOK') {
<label class="self-center">Preferred Format</label>
<div class="flex items-center gap-2">
<p-select
[(ngModel)]="defaultBookFormat"
[options]="bookFormatOptions"
optionLabel="label"
optionValue="value"
placeholder="Select preferred format"
class="w-full"
appendTo="body"/>
<i
class="pi pi-info-circle text-sky-600 cursor-pointer"
pTooltip="If a folder contains books in multiple formats, this setting determines which format BookLore will use as the primary file."
tooltipPosition="right"></i>
</div>
}
<label class="self-center">Monitor Folders</label>
<div class="flex items-center gap-2">
<p-toggleswitch [(ngModel)]="watch"/>
<i
class="pi pi-info-circle text-sky-600 cursor-pointer"
pTooltip="Watches for file changes and auto-adds/removes books."
tooltipPosition="right"></i>
</div>
</div>
</div>
<div class="flex justify-end pt-6">
<p-button
label="Next"
icon="pi pi-arrow-right"
iconPos="right"
(onClick)="validateLibraryNameAndProceed(activateCallback)"
[disabled]="!isLibraryDetailsValid()"/>
</div>
</div>
</ng-template>
</p-step-panel>
<p-step-panel [value]="2">
<ng-template #content let-activateCallback="activateCallback">
<div class="flex flex-col justify-between w-[24rem] md:w-[45rem] max-w-[50rem] mx-auto">
<div class="flex-grow flex items-center justify-center">
<div class="flex flex-col gap-6 border border-[var(--border-color)] px-2 py-4 md:p-6 rounded-xl w-full">
<div class="flex justify-center">
<p-button
label="Add Book Folders"
icon="pi pi-folder-open"
[outlined]="true"
severity="info"
(onClick)="openDirectoryPicker()"/>
</div>
<div class="flex flex-col gap-2 max-h-70 overflow-y-auto">
@for (folder of folders; track folder) {
<div class="flex items-start justify-between p-1 gap-4">
<div class="flex items-center gap-4 overflow-x-auto whitespace-nowrap flex-1 pr-2" [pTooltip]="folder" tooltipPosition="top">
<i class="pi pi-folder text-yellow-600 text-sm"></i>
<span class="truncate">{{ folder }}</span>
</div>
<div class="flex-shrink-0">
<p-button
icon="pi pi-times"
[rounded]="true"
outlined
size="small"
severity="danger"
(onClick)="removeFolder(folders.indexOf(folder))"/>
</div>
</div>
}
</div>
</div>
</div>
<div class="flex justify-between pt-6">
<p-button label="Back" icon="pi pi-arrow-left" iconPos="left" (onClick)="activateCallback(1)"/>
<p-button label="Save" icon="pi pi-save" severity="success" [disabled]="!isDirectorySelectionValid()" (onClick)="createOrUpdateLibrary()"/>
</div>
</div>
</ng-template>
</p-step-panel>
</p-step-panels>
</p-stepper>
</div>
@@ -0,0 +1,179 @@
import {Component, inject, OnInit} from '@angular/core';
import {DialogService, DynamicDialogConfig, DynamicDialogRef} from 'primeng/dynamicdialog';
import {DirectoryPickerComponent} from '../../shared/components/directory-picker/directory-picker.component';
import {MessageService} from 'primeng/api';
import {Router} from '@angular/router';
import {LibraryService} from '../book/service/library.service';
import {Button} from 'primeng/button';
import {TableModule} from 'primeng/table';
import {Step, StepList, StepPanel, StepPanels, Stepper} from 'primeng/stepper';
import {FormsModule} from '@angular/forms';
import {InputText} from 'primeng/inputtext';
import {Library, LibraryScanMode, BookFileType} from '../book/model/library.model';
import {ToggleSwitch} from 'primeng/toggleswitch';
import {Tooltip} from 'primeng/tooltip';
import {IconPickerService} from '../../shared/service/icon-picker.service';
import {Select} from 'primeng/select';
@Component({
selector: 'app-library-creator',
standalone: true,
templateUrl: './library-creator.component.html',
imports: [Button, TableModule, StepPanel, FormsModule, InputText, Stepper, StepList, Step, StepPanels, ToggleSwitch, Tooltip, Select],
styleUrl: './library-creator.component.scss'
})
export class LibraryCreatorComponent implements OnInit {
chosenLibraryName: string = '';
folders: string[] = [];
selectedIcon: string | null = null;
mode!: string;
library!: Library | undefined;
editModeLibraryName: string = '';
directoryPickerDialogRef!: DynamicDialogRef<DirectoryPickerComponent>;
watch: boolean = false;
scanMode: LibraryScanMode = 'FILE_AS_BOOK';
defaultBookFormat: BookFileType | undefined = undefined;
readonly scanModeOptions = [
{label: 'Each file is a book (Recommended)', value: 'FILE_AS_BOOK'},
{label: 'Each folder is a book with extras', value: 'FOLDER_AS_BOOK'}
];
readonly bookFormatOptions = [
{label: 'None', value: undefined},
{label: 'EPUB', value: 'EPUB'},
{label: 'PDF', value: 'PDF'},
{label: 'CBX/CBZ/CBR', value: 'CBX'}
];
private dialogService = inject(DialogService);
private dynamicDialogRef = inject(DynamicDialogRef);
private dynamicDialogConfig = inject(DynamicDialogConfig);
private libraryService = inject(LibraryService);
private messageService = inject(MessageService);
private router = inject(Router);
private iconPicker = inject(IconPickerService);
ngOnInit(): void {
const data = this.dynamicDialogConfig?.data;
if (data?.mode === 'edit') {
this.mode = data.mode;
this.library = this.libraryService.findLibraryById(data.libraryId);
if (this.library) {
const {name, icon, paths, watch, scanMode, defaultBookFormat} = this.library;
this.chosenLibraryName = name;
this.editModeLibraryName = name;
this.selectedIcon = `pi pi-${icon}`;
this.watch = watch;
this.scanMode = scanMode || 'FILE_AS_BOOK';
this.defaultBookFormat = defaultBookFormat || undefined;
this.folders = paths.map(path => path.path);
}
}
}
openDirectoryPicker(): void {
this.directoryPickerDialogRef = this.dialogService.open(DirectoryPickerComponent, {
header: 'Select Media Directory',
modal: true,
closable: true,
contentStyle: {overflow: 'hidden'},
baseZIndex: 10
});
this.directoryPickerDialogRef.onClose.subscribe((selectedFolder: string) => {
if (selectedFolder) {
this.addFolder(selectedFolder);
}
});
}
openIconPicker(): void {
this.iconPicker.open().subscribe(icon => {
if (icon) {
this.selectedIcon = icon;
}
});
}
addFolder(folder: string): void {
this.folders.push(folder);
}
removeFolder(index: number): void {
this.folders.splice(index, 1);
}
clearSelectedIcon(): void {
this.selectedIcon = null;
}
isLibraryDetailsValid(): boolean {
return !!this.chosenLibraryName.trim() && !!this.selectedIcon;
}
isDirectorySelectionValid(): boolean {
return this.folders.length > 0;
}
validateLibraryNameAndProceed(activateCallback: Function): void {
let trimmedLibraryName = this.chosenLibraryName.trim();
if (trimmedLibraryName && trimmedLibraryName != this.editModeLibraryName) {
let exists = this.libraryService.doesLibraryExistByName(trimmedLibraryName);
if (exists) {
this.messageService.add({
severity: 'error',
summary: 'Library Name Exists',
detail: 'This library name is already taken.',
});
} else {
activateCallback(2);
}
} else {
activateCallback(2);
}
}
createOrUpdateLibrary(): void {
if (this.mode === 'edit') {
const library: Library = {
name: this.chosenLibraryName,
icon: this.selectedIcon?.replace('pi pi-', '') || 'heart',
paths: this.folders.map(folder => ({path: folder})),
watch: this.watch,
scanMode: this.scanMode,
defaultBookFormat: this.defaultBookFormat
};
this.libraryService.updateLibrary(library, this.library?.id).subscribe({
next: () => {
this.messageService.add({severity: 'success', summary: 'Library Updated', detail: 'The library was updated successfully.'});
this.dynamicDialogRef.close();
},
error: (e) => {
this.messageService.add({severity: 'error', summary: 'Update Failed', detail: 'An error occurred while updating the library. Please try again.'});
console.error(e);
}
});
} else {
const library: Library = {
name: this.chosenLibraryName,
icon: this.selectedIcon?.replace('pi pi-', '') || 'heart',
paths: this.folders.map(folder => ({path: folder})),
watch: this.watch,
scanMode: this.scanMode,
defaultBookFormat: this.defaultBookFormat
};
this.libraryService.createLibrary(library).subscribe({
next: (createdLibrary) => {
this.router.navigate(['/library', createdLibrary.id, 'books']);
this.messageService.add({severity: 'success', summary: 'Library Created', detail: 'The library was created successfully.'});
this.dynamicDialogRef.close();
},
error: (e) => {
this.messageService.add({severity: 'error', summary: 'Creation Failed', detail: 'An error occurred while creating the library. Please try again.'});
console.error(e);
}
});
}
}
}