Add file upload functionality

This commit is contained in:
aditya.chandel
2024-12-16 16:50:49 -07:00
parent 7973d309de
commit 696649a24f
34 changed files with 508 additions and 141 deletions
@@ -0,0 +1,52 @@
<div class="card">
<div class="flex">
<p-dropdown
[options]="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,89 @@
import {Component, OnInit} from '@angular/core';
import {Library} from '../../model/library.model';
import {LibraryService} from '../../service/library.service';
import {FileUploadService} from '../../../file-upload.service';
import {Button} from 'primeng/button';
import {NgIf} from '@angular/common';
import {DropdownModule} from 'primeng/dropdown';
import {FormsModule} from '@angular/forms';
import {ToastService} from '../../../toast.service';
import {DynamicDialogRef} from 'primeng/dynamicdialog';
@Component({
selector: 'app-file-upload-v2',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.scss'],
imports: [
Button,
NgIf,
DropdownModule,
FormsModule
],
})
export class FileUploadComponent implements OnInit {
libraries: Library[] = [];
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
) {
}
ngOnInit() {
this.libraries = this.libraryService.libraries();
}
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 + ".";
}
);
}
}