import { Component, OnInit } from '@angular/core';
import { Validators } from '@angular/forms';
import {
Button,
FormStructure,
Input,
InputFile,
FileChange
} from 'mat-dynamic-form';
@Component({
selector: 'app-file-upload',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.scss']
})
export class FileUploadComponent implements OnInit {
formStructure: FormStructure;
constructor() {
this.formStructure = new FormStructure();
this.formStructure.title = 'Document Upload';
this.formStructure.appearance = 'outline';
this.formStructure.nodes = [
new Input('documentName', 'Document Name').apply({
icon: 'description',
validator: Validators.required,
hint: 'Enter a name for your document'
}),
// Profile picture with image restrictions
new InputFile('profilePicture', 'Profile Picture').apply({
accept: ['png', 'jpg', 'jpeg'],
maxSize: 5, // 5 MB
filename: 'ProfilePic',
hint: 'Upload your profile picture (max 5 MB)',
errorMessage: 'Invalid file format or size',
dragLabel: 'Drag & drop your image here',
downloadHint: 'Download current picture',
removeHint: 'Remove picture',
onStatusChange: (status) => this.onFileStatusChange('profilePicture', status)
}),
// PDF document upload
new InputFile('pdfDocument', 'PDF Document').apply({
accept: ['pdf'],
maxSize: 10, // 10 MB
filename: 'Document',
hint: 'Upload a PDF file (max 10 MB)',
errorMessage: 'Please upload a valid PDF file',
dragLabel: 'Drop your PDF here',
downloadHint: 'Download PDF',
removeHint: 'Remove PDF',
validator: Validators.required,
onStatusChange: (status) => this.onFileStatusChange('pdfDocument', status)
}),
// Multiple file types allowed
new InputFile('attachment', 'Attachment').apply({
accept: ['pdf', 'doc', 'docx', 'txt', 'png', 'jpg'],
maxSize: 20, // 20 MB
filename: 'Attachment',
hint: 'Upload any document or image (max 20 MB)',
errorMessage: 'File type not supported or size exceeds limit',
dragLabel: 'Drag & drop your file here or click to browse',
downloadHint: 'Download attachment',
removeHint: 'Remove attachment',
retryHint: 'Retry upload',
onStatusChange: (status) => this.onFileStatusChange('attachment', status)
})
];
this.formStructure.validateActions = [
new Button('reset', 'Reset', {
onEvent: (param) => {
param.structure?.reset();
console.log('Form reset');
},
style: 'warn'
}).apply({
icon: 'refresh'
}),
new Button('submit', 'Upload', {
onEvent: (param) => {
const formValue = param.structure?.getValue();
console.log('Uploading files:', formValue);
// Here you would typically send the files to your backend
this.handleFileUpload(formValue);
},
style: 'primary'
}).apply({
validateForm: true,
icon: 'cloud_upload'
})
];
}
ngOnInit(): void {}
onFileStatusChange(fieldId: string, status: FileChange) {
console.log(`File status changed for ${fieldId}:`, status);
// Handle different file statuses
switch (status.status) {
case 'selected':
console.log(`File selected: ${status.file?.name}`);
break;
case 'invalid':
console.log(`Invalid file: ${status.error}`);
break;
case 'removed':
console.log('File removed');
break;
case 'downloaded':
console.log('File downloaded');
break;
}
}
handleFileUpload(formValue: any) {
// Example: Send files to backend
const formData = new FormData();
if (formValue.profilePicture) {
formData.append('profilePicture', formValue.profilePicture);
}
if (formValue.pdfDocument) {
formData.append('pdfDocument', formValue.pdfDocument);
}
if (formValue.attachment) {
formData.append('attachment', formValue.attachment);
}
// Send to your API
// this.http.post('/api/upload', formData).subscribe(...);
}
}