Made several improvements to book upload component
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
import {Component, inject, Input} from '@angular/core';
|
||||
import {FileUpload, FileUploadEvent} from 'primeng/fileupload';
|
||||
import {Button} from 'primeng/button';
|
||||
import {LibraryService} from '../book/service/library.service';
|
||||
import {Observable} from 'rxjs';
|
||||
import {LibraryState} from '../book/model/state/library-state.model';
|
||||
import {AsyncPipe, NgIf} from '@angular/common';
|
||||
import {DropdownModule} from 'primeng/dropdown';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {Library, LibraryPath} from '../book/model/library.model';
|
||||
import {MessageService} from 'primeng/api';
|
||||
import {DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-uploader',
|
||||
imports: [
|
||||
FileUpload,
|
||||
Button,
|
||||
AsyncPipe,
|
||||
DropdownModule,
|
||||
FormsModule,
|
||||
NgIf
|
||||
],
|
||||
templateUrl: './book-uploader.component.html',
|
||||
styleUrl: './book-uploader.component.scss'
|
||||
})
|
||||
export class BookUploaderComponent {
|
||||
|
||||
private uploadUrl = 'http://localhost:8080/api/v1/files/upload';
|
||||
|
||||
private libraryService = inject(LibraryService);
|
||||
private messageService = inject(MessageService);
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
|
||||
libraryState$: Observable<LibraryState> = this.libraryService.libraryState$;
|
||||
|
||||
selectedLibrary: Library | null = null;
|
||||
selectedPath: LibraryPath | null = null;
|
||||
|
||||
getUploadUrl() {
|
||||
return `${this.uploadUrl}?libraryId=${this.selectedLibrary?.id}&pathId=${this.selectedPath?.id}`;
|
||||
}
|
||||
|
||||
onUpload($event: FileUploadEvent) {
|
||||
this.dynamicDialogRef.close();
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Book successfully uploaded'});
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ import {TooltipModule} from 'primeng/tooltip';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {InputTextModule} from 'primeng/inputtext';
|
||||
import {BookSearcherComponent} from '../../../book/components/book-searcher/book-searcher.component';
|
||||
import {FileUploadComponent} from '../../../utilities/component/file-upload/file-upload.component';
|
||||
import {NgClass} from '@angular/common';
|
||||
import {EventService} from '../../../shared/websocket/event.service';
|
||||
import {Button} from 'primeng/button';
|
||||
@@ -16,7 +15,7 @@ import {StyleClass} from 'primeng/styleclass';
|
||||
import {Divider} from 'primeng/divider';
|
||||
import {ThemeConfiguratorComponent} from '../theme-configurator/theme-configurator.component';
|
||||
import {LiveNotificationBoxComponent} from '../../../core/component/live-notification-box/live-notification-box.component';
|
||||
import {BookUploaderComponent} from '../../../book-uploader/book-uploader.component';
|
||||
import {BookUploaderComponent} from '../../../utilities/component/book-uploader/book-uploader.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-topbar',
|
||||
|
||||
+26
-4
@@ -21,18 +21,40 @@
|
||||
#fu
|
||||
mode="basic"
|
||||
chooseLabel="Choose"
|
||||
chooseIcon="pi pi-upload"
|
||||
chooseIcon="pi pi-file"
|
||||
uploadIcon="pi pi-file"
|
||||
name="file"
|
||||
[url]="getUploadUrl()"
|
||||
accept=".pdf,.epub"
|
||||
maxFileSize="104857600"
|
||||
(onSelect)="onSelect()"
|
||||
(onClear)="onClear($event)"
|
||||
(onUpload)="onUpload($event)"
|
||||
[disabled]="!selectedLibrary || !selectedPath"
|
||||
(onProgress)="onProgress($event)"
|
||||
(onSend)="onSend($event)"
|
||||
(onError)="onError($event)"
|
||||
[disabled]="!selectedLibrary || !selectedPath || isUploading"
|
||||
class="w-full">
|
||||
</p-fileupload>
|
||||
|
||||
<div class="flex flex-row items-center justify-between w-full">
|
||||
<p-button label="Upload" (onClick)="fu.upload()" severity="primary" [disabled]="!fu.files.length" class=""/>
|
||||
<p-button label="Clear" (click)="fu.clear()" severity="info" [disabled]="!fu.files.length" class=""/>
|
||||
|
||||
<div class="flex flex-row items-center gap-4">
|
||||
<p-button
|
||||
icon="pi pi-upload"
|
||||
label="Upload"
|
||||
(onClick)="fu.upload()"
|
||||
severity="info"
|
||||
[disabled]="!fu.files.length || isUploading"/>
|
||||
<p *ngIf="isUploading" class="text-base">Progress: {{ progressPercent }}%</p>
|
||||
<p *ngIf="errorMessage" class="text-base text-red-600">{{ errorMessage }}</p>
|
||||
</div>
|
||||
|
||||
<p-button
|
||||
icon="pi pi-times"
|
||||
label="Clear"
|
||||
(click)="fu.clear()"
|
||||
severity="warn"
|
||||
[hidden]="!fu.files.length || isUploading"/>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,91 @@
|
||||
import { Component, inject } from '@angular/core';
|
||||
import { FileProgressEvent, FileSendEvent, FileUpload, FileUploadErrorEvent, FileUploadEvent } from 'primeng/fileupload';
|
||||
import { Button } from 'primeng/button';
|
||||
import { LibraryService } from '../../../book/service/library.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { LibraryState } from '../../../book/model/state/library-state.model';
|
||||
import { AsyncPipe, NgIf } from '@angular/common';
|
||||
import { DropdownModule } from 'primeng/dropdown';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { Library, LibraryPath } from '../../../book/model/library.model';
|
||||
import { MessageService } from 'primeng/api';
|
||||
import { DynamicDialogRef } from 'primeng/dynamicdialog';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-uploader',
|
||||
imports: [
|
||||
FileUpload,
|
||||
Button,
|
||||
AsyncPipe,
|
||||
DropdownModule,
|
||||
FormsModule,
|
||||
NgIf
|
||||
],
|
||||
templateUrl: './book-uploader.component.html',
|
||||
styleUrl: './book-uploader.component.scss'
|
||||
})
|
||||
export class BookUploaderComponent {
|
||||
|
||||
private uploadUrl = 'http://localhost:8080/api/v1/files/upload';
|
||||
|
||||
private libraryService = inject(LibraryService);
|
||||
private messageService = inject(MessageService);
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
|
||||
libraryState$: Observable<LibraryState> = this.libraryService.libraryState$;
|
||||
|
||||
selectedLibrary: Library | null = null;
|
||||
selectedPath: LibraryPath | null = null;
|
||||
isUploading = false;
|
||||
progressPercent = 0;
|
||||
errorMessage: string | null = null;
|
||||
|
||||
getUploadUrl() {
|
||||
return `${this.uploadUrl}?libraryId=${this.selectedLibrary?.id}&pathId=${this.selectedPath?.id}`;
|
||||
}
|
||||
|
||||
onSelect() {
|
||||
this.errorMessage = null;
|
||||
}
|
||||
|
||||
onClear($event: Event) {
|
||||
this.errorMessage = null;
|
||||
}
|
||||
|
||||
onSend($event: FileSendEvent) {
|
||||
this.errorMessage = null;
|
||||
this.isUploading = true;
|
||||
}
|
||||
|
||||
onProgress($event: FileProgressEvent) {
|
||||
this.progressPercent = $event.progress;
|
||||
}
|
||||
|
||||
onUpload($event: FileUploadEvent) {
|
||||
this.dynamicDialogRef.close();
|
||||
this.messageService.add({severity: 'success', summary: 'Success', detail: 'Book successfully uploaded'});
|
||||
this.isUploading = false;
|
||||
this.progressPercent = 0;
|
||||
}
|
||||
|
||||
onError($event: FileUploadErrorEvent) {
|
||||
let error: any = $event.error;
|
||||
if (error?.status === 409) {
|
||||
this.errorMessage = 'File already exists';
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Upload Failed',
|
||||
detail: 'The file already exists.'
|
||||
});
|
||||
} else {
|
||||
this.errorMessage = 'Upload failed';
|
||||
this.messageService.add({
|
||||
severity: 'error',
|
||||
summary: 'Upload Failed',
|
||||
detail: 'There was an error uploading the file.'
|
||||
});
|
||||
}
|
||||
this.isUploading = false;
|
||||
this.progressPercent = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
<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>
|
||||
@@ -1,28 +0,0 @@
|
||||
.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;
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
import {Component, inject} 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 {
|
||||
|
||||
selectedLibrary: Library | null = null;
|
||||
selectedPath: string = '';
|
||||
selectedFileName: string = '';
|
||||
isUploading: boolean = false;
|
||||
errorMessage: string | null = null;
|
||||
|
||||
private libraryService = inject(LibraryService);
|
||||
private fileUploadService = inject(FileUploadService);
|
||||
private toastService = inject(ToastService);
|
||||
private dynamicDialogRef = inject(DynamicDialogRef);
|
||||
|
||||
libraryState$: Observable<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({
|
||||
next: () => {
|
||||
this.toastService.showSuccess("File uploaded", file.name);
|
||||
this.isUploading = false;
|
||||
this.dynamicDialogRef.close();
|
||||
},
|
||||
error: (e) => {
|
||||
this.isUploading = false;
|
||||
this.toastService.showError("Upload failed", e.error.message);
|
||||
this.errorMessage = "Upload failed. " + e.error.message + ".";
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} 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';
|
||||
|
||||
private httpClient = inject(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.httpClient.post(this.uploadUrl, formData).pipe(
|
||||
catchError((error) => {
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user