More refactorings, more file re-arrangings

This commit is contained in:
aditya.chandel
2025-01-12 13:56:36 -07:00
parent ef3556cb0c
commit d29d59c299
31 changed files with 66 additions and 182 deletions
@@ -0,0 +1,33 @@
<div style="margin-bottom: 1.5rem; display: flex; align-items: center; gap: 1rem;">
<input
id="disabled-input"
type="text"
pInputText
[value]="selectedProductName"
[disabled]="true"
[ngStyle]="{ 'width': '100%', 'padding': '0.5rem', 'font-size': '1rem' }"
/>
<p-button label="Select" severity="success" [ngStyle]="{ 'width': '100px' }" (click)="onSelect()"/>
</div>
<ng-container>
<div style="overflow-x: auto;">
<p-table [value]="paths" [tableStyle]="{ 'min-width': '50rem' }" class="product-table">
<ng-template pTemplate="header">
<tr>
<th [style.width]="'10%'">Type</th>
<th [style.width]="'90%'">Name</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-product>
<tr (click)="onRowClick(product)">
<td>
<i *ngIf="product === '...'" class="pi pi-arrow-up"></i>
<i *ngIf="product !== '...'" class="pi pi-folder-open"></i>
</td>
<td>{{ product }}</td>
</tr>
</ng-template>
</p-table>
</div>
</ng-container>
@@ -0,0 +1,53 @@
import {Component, OnInit} from '@angular/core';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {UtilityService} from './utility.service';
@Component({
selector: 'app-directory-picker-v2',
standalone: false,
templateUrl: './directory-picker.component.html',
styleUrls: ['./directory-picker.component.scss']
})
export class DirectoryPickerComponent implements OnInit {
value: any;
paths: string[] = ['...'];
selectedProductName: string = '';
constructor(private utilityService: UtilityService, private dynamicDialogRef: DynamicDialogRef) {
}
ngOnInit() {
const initialPath = '/';
this.getFolders(initialPath);
}
getFolders(path: string): void {
this.utilityService.getFolders(path).subscribe(
(folders: string[]) => {
this.paths = ['...', ...folders];
},
(error) => {
console.error('Error fetching folders:', error);
}
);
}
onRowClick(path: string): void {
if (path === '...') {
if (this.selectedProductName === '' || this.selectedProductName === '/') {
this.getFolders('/');
} else {
const result = this.selectedProductName.substring(0, this.selectedProductName.lastIndexOf('/')) || '/';
this.selectedProductName = result;
this.getFolders(result);
}
} else {
this.selectedProductName = path;
this.getFolders(path);
}
}
onSelect(): void {
this.dynamicDialogRef.close(this.selectedProductName);
}
}
@@ -0,0 +1,18 @@
import { Injectable } from '@angular/core';
import {Observable} from 'rxjs';
import {HttpClient, HttpParams} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class UtilityService {
private pathUrl = 'http://localhost:8080/v1/path';
constructor(private http: HttpClient) { }
getFolders(path: string): Observable<string[]> {
const params = new HttpParams().set('path', path);
return this.http.get<string[]>(this.pathUrl, { params });
}
}
@@ -0,0 +1,52 @@
<div class="card">
<div class="flex">
<p-dropdown
[options]="(libraryState$ | async)?.libraries || []"
optionLabel="name"
placeholder="Select Library"
[(ngModel)]="selectedLibrary">
</p-dropdown>
</div>
<div class="flex">
<p-dropdown
[options]="selectedLibrary?.paths"
placeholder="Select Upload Location"
[(ngModel)]="selectedPath"
[disabled]="!selectedLibrary">
</p-dropdown>
</div>
<div class="flex">
<div class="button-container">
<p-button
severity="info"
label="Choose"
icon="pi pi-file"
(click)="triggerFileInput(fileInput)"
[disabled]="!selectedPath || isUploading">
</p-button>
<span *ngIf="selectedFileName" class="file-name">{{ selectedFileName }}</span>
</div>
<input
type="file"
#fileInput
style="display: none;"
(change)="onFileSelected($event)"
[disabled]="!selectedPath || isUploading"/>
</div>
<div class="flex">
<p-button
[label]="isUploading ? 'Uploading...' : 'Upload'"
[disabled]="!isFormValid() || isUploading"
icon="pi pi-cloud-upload"
(click)="uploadFile()">
</p-button>
<div class="error-message-container">
<span *ngIf="errorMessage" class="error-message">{{ errorMessage }}</span>
</div>
</div>
</div>
@@ -0,0 +1,28 @@
.flex {
display: flex;
}
.flex + .flex {
margin-top: 15px;
}
.button-container {
display: flex;
align-items: center;
gap: 10px;
}
.file-name {
font-weight: bold;
color: var(--text-color-secondary);
}
.error-message-container {
display: flex;
align-items: center;
}
.error-message {
color: var(--red-600);
margin-left: 10px;
}
@@ -0,0 +1,88 @@
import {Component} from '@angular/core';
import {Library} from '../../../book/model/library.model';
import {FileUploadService} from './file-upload.service';
import {Button} from 'primeng/button';
import {AsyncPipe, NgIf} from '@angular/common';
import {DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
import {ToastService} from '../../../core/service/toast.service';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {LibraryService} from '../../../book/service/library.service';
import {Observable} from 'rxjs';
import {LibraryState} from '../../../book/model/state/library-state.model';
@Component({
selector: 'app-file-upload-v2',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.scss'],
imports: [
Button,
NgIf,
DropdownModule,
FormsModule,
AsyncPipe
],
})
export class FileUploadComponent {
libraryState$: Observable<LibraryState>;
selectedLibrary: Library | null = null;
selectedPath: string = '';
selectedFileName: string = '';
isUploading: boolean = false;
errorMessage: string | null = null;
constructor(
private libraryService: LibraryService,
private fileUploadService: FileUploadService,
private toastService: ToastService,
private dynamicDialogRef: DynamicDialogRef) {
this.libraryState$ = this.libraryService.libraryState$;
}
triggerFileInput(fileInput: HTMLInputElement): void {
fileInput.click();
}
onFileSelected(event: Event): void {
const input = event.target as HTMLInputElement;
if (input && input.files) {
const file = input.files[0];
if (file) {
this.selectedFileName = file.name;
}
}
}
isFormValid(): boolean {
return !!this.selectedLibrary && !!this.selectedPath && !!this.selectedFileName;
}
uploadFile(): void {
this.errorMessage = null;
if (!this.isFormValid()) {
return;
}
const fileInput = document.querySelector('input[type="file"]') as HTMLInputElement;
if (!fileInput || !fileInput.files || fileInput.files.length === 0) {
return;
}
const file = fileInput.files[0];
const libraryId = this.selectedLibrary?.id ? this.selectedLibrary.id.toString() : '';
const path = this.selectedPath || '';
this.isUploading = true;
this.fileUploadService.uploadFile(file, libraryId, path)
.subscribe(
(response) => {
console.log('File uploaded successfully');
this.toastService.showSuccess("File uploaded", file.name);
this.isUploading = false;
this.dynamicDialogRef.close();
},
(error) => {
this.isUploading = false;
this.toastService.showError("Upload failed", error.error.message);
this.errorMessage = "Upload failed. " + error.error.message + ".";
}
);
}
}
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class FileUploadService {
private uploadUrl = 'http://localhost:8080/api/v1/files/upload';
constructor(private http: HttpClient) { }
uploadFile(file: File, libraryId: string, filePath: string): Observable<any> {
const formData: FormData = new FormData();
formData.append('file', file);
formData.append('libraryId', libraryId);
formData.append('filePath', filePath);
return this.http.post(this.uploadUrl, formData).pipe(
catchError((error) => {
throw error;
})
);
}
}
@@ -0,0 +1,17 @@
<p-dialog
[(visible)]="iconDialogVisible"
header="Select an Icon"
[modal]="true"
[draggable]="false"
[style]="{ width: '500px', height: '500px' }">
<input type="text" placeholder="Search icons..." [(ngModel)]="searchText" class="icon-search"/>
<div class="icon-grid">
<div
*ngFor="let icon of filteredIcons()"
class="icon-item"
(click)="selectIcon(icon)"
[class.selected]="icon === selectedIcon">
<i [class]="icon"></i>
</div>
</div>
</p-dialog>
@@ -0,0 +1,32 @@
.icon-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(25px, 1fr));
gap: 30px;
}
.icon-item {
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
.icon-search {
width: 100%;
padding: 8px 12px;
font-size: 16px;
border: 1px solid var(--text-color-secondary);
border-radius: 4px;
margin-bottom: 15px;
box-sizing: border-box;
}
.icon-search:focus {
border-color: var(--primary-color);
outline: none;
}
.icon-search::placeholder {
color: var(--text-color-secondary);
opacity: 1;
}
@@ -0,0 +1,78 @@
import {Component, EventEmitter, Output} from '@angular/core';
import {DialogModule} from 'primeng/dialog';
import {NgForOf} from '@angular/common';
import {FormsModule} from '@angular/forms';
@Component({
selector: 'app-icon-picker',
templateUrl: './icon-picker.component.html',
styleUrls: ['./icon-picker.component.scss'],
imports: [
DialogModule,
NgForOf,
FormsModule
],
})
export class IconPickerComponent {
iconDialogVisible: boolean = false;
selectedIcon: string | null = null;
searchText: string = '';
iconCategories: string[] = [
"address-book", "align-center", "align-justify", "align-left", "align-right", "android",
"angle-double-down", "angle-double-left", "angle-double-right", "angle-double-up", "angle-down", "angle-left",
"angle-right", "angle-up", "apple", "arrow-circle-down", "arrow-circle-left", "arrow-circle-right",
"arrow-circle-up", "arrow-down-left-and-arrow-up-right-to-center", "arrow-down-left", "arrow-down-right", "arrow-down", "arrow-left",
"arrow-right-arrow-left", "arrow-right", "arrow-up-left", "arrow-up-right-and-arrow-down-left-from-center", "arrow-up-right", "arrow-up",
"arrows-alt", "arrows-h", "arrows-v", "asterisks", "at", "backward", "ban", "barcode", "bars", "bell-slash", "bell", "bitcoin",
"bolt", "book", "bookmark-fill", "bookmark", "box", "briefcase", "building-columns", "building", "bullseye", "calculator", "calendar-clock", "calendar-minus",
"calendar-plus", "calendar-times", "calendar", "camera", "car", "caret-down", "caret-left", "caret-right", "caret-up", "cart-arrow-down", "cart-minus", "cart-plus",
"chart-bar", "chart-line", "chart-pie", "chart-scatter", "check-circle", "check-square", "check", "chevron-circle-down", "chevron-circle-left", "chevron-circle-right",
"chevron-circle-up", "chevron-down", "chevron-left", "chevron-right", "chevron-up", "circle-fill", "circle-off", "circle-on", "circle", "clipboard", "clock", "clone", "cloud-download", "cloud-upload",
"cloud", "code", "cog", "comment", "comments", "compass", "copy", "credit-card", "crown", "database", "delete-left", "desktop",
"directions-alt", "directions", "discord", "dollar", "download", "eject", "ellipsis-h", "ellipsis-v", "envelope", "equals", "eraser", "ethereum",
"euro", "exclamation-circle", "exclamation-triangle", "expand", "external-link", "eye-slash", "eye", "face-smile", "facebook", "fast-backward", "fast-forward", "file-arrow-up",
"file-check", "file-edit", "file-excel", "file-export", "file-import", "file-o", "file-pdf", "file-plus", "file-word", "file", "filter-fill", "filter-slash",
"filter", "flag-fill", "flag", "folder-open", "folder-plus", "folder", "forward", "gauge", "gift", "github", "globe", "google",
"graduation-cap", "hammer", "hashtag", "headphones", "heart-fill", "heart", "history", "home", "hourglass", "id-card", "image", "images",
"inbox", "indian-rupee", "info-circle", "info", "instagram", "key", "language", "lightbulb", "link", "linkedin", "list-check", "list",
"lock-open", "lock", "map-marker", "map", "mars", "megaphone", "microchip-ai", "microchip", "microphone", "microsoft", "minus-circle", "minus",
"mobile", "money-bill", "moon", "objects-column", "palette", "paperclip", "pause-circle", "pause", "paypal", "pen-to-square", "pencil", "percentage",
"phone", "pinterest", "play-circle", "play", "plus-circle", "plus", "pound", "power-off", "prime", "print", "qrcode", "question-circle",
"question", "receipt", "reddit", "refresh", "replay", "reply", "save", "search-minus", "search-plus", "search", "send", "server",
"share-alt", "shield", "shop", "shopping-bag", "shopping-cart", "sign-in", "sign-out", "sitemap", "slack", "sliders-h", "sliders-v", "sort-alpha-alt-down",
"sort-alpha-alt-up", "sort-alpha-down", "sort-alpha-up", "sort-alt-slash", "sort-alt", "sort-amount-down-alt", "sort-amount-down", "sort-amount-up-alt",
"sort-amount-up", "sort-down-fill", "sort-down", "sort-numeric-alt-down",
"sort-numeric-alt-up", "sort-numeric-down", "sort-numeric-up", "sort-up-fill", "sort-up", "sort", "sparkles", "spinner-dotted", "spinner", "star-fill", "star-half-fill", "star-half",
"star", "step-backward-alt", "step-backward", "step-forward-alt", "step-forward", "stop-circle", "stop", "stopwatch", "sun", "sync", "table", "tablet",
"tag", "tags", "telegram", "th-large", "thumbs-down-fill", "thumbs-down", "thumbs-up-fill", "thumbs-up", "thumbtack", "ticket", "tiktok", "times-circle",
"times", "trash", "trophy", "truck", "turkish-lira", "twitch", "twitter", "undo", "unlock", "upload", "user-edit", "user-minus",
"user-plus", "user", "users", "venus", "verified", "video", "vimeo", "volume-down", "volume-off", "volume-up", "wallet", "warehouse",
"wave-pulse", "whatsapp", "wifi", "window-maximize", "window-minimize", "wrench", "youtube"
];
icons: string[] = this.createIconList(this.iconCategories);
@Output() iconSelected = new EventEmitter<string>();
createIconList(categories: string[]): string[] {
return categories.map(iconName => `pi pi-${iconName}`);
}
filteredIcons(): string[] {
if (!this.searchText) {
return this.icons;
}
return this.icons.filter(icon => icon.toLowerCase().includes(this.searchText.toLowerCase()));
}
open() {
this.iconDialogVisible = true;
}
selectIcon(icon: string) {
this.selectedIcon = icon;
this.iconDialogVisible = false;
this.iconSelected.emit(icon);
}
}