64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import { Component, Inject, OnInit } from '@angular/core';
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
|
import { EntryService } from '../../../core/services/entry.service';
|
|
|
|
@Component({
|
|
selector: 'app-media-upload-modal',
|
|
templateUrl: './media-upload-modal.component.html',
|
|
styleUrls: ['./media-upload-modal.component.css'],
|
|
})
|
|
export class MediaUploadModalComponent implements OnInit {
|
|
fileForm: FormGroup;
|
|
mediaType!: string;
|
|
entryId!: number;
|
|
|
|
constructor(
|
|
public dialogRef: MatDialogRef<MediaUploadModalComponent>,
|
|
@Inject(MAT_DIALOG_DATA) public data: any,
|
|
private formBuilder: FormBuilder,
|
|
private entryService: EntryService
|
|
) {
|
|
this.fileForm = this.formBuilder.group({
|
|
file: [null],
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
if (this.data && this.data.mediaType && this.data.entryId) {
|
|
this.mediaType = this.data.mediaType;
|
|
this.entryId = this.data.entryId;
|
|
} else {
|
|
this.dialogRef.close();
|
|
}
|
|
}
|
|
|
|
onFileChange(event: any) {
|
|
const file = event.target.files[0];
|
|
this.fileForm.patchValue({ file: file });
|
|
this.fileForm.get('file')?.updateValueAndValidity();
|
|
}
|
|
|
|
onSubmit() {
|
|
if (!this.fileForm.valid) return;
|
|
|
|
const formData = new FormData();
|
|
|
|
const fileValue = this.fileForm.get('file')?.value;
|
|
if (fileValue) formData.append(this.mediaType, fileValue);
|
|
|
|
this.entryService.addEntryMedia(this.entryId, formData).subscribe({
|
|
next: (result) => {
|
|
this.dialogRef.close({ data: result });
|
|
},
|
|
error: (error) => {
|
|
console.log(error);
|
|
},
|
|
});
|
|
}
|
|
|
|
onClose() {
|
|
this.dialogRef.close();
|
|
}
|
|
}
|