add entry media upload menu

This commit is contained in:
X4Lo committed 2024-08-30 14:25:58 +01:00
1 parent 4c318fb569
commit c46309e458
14 files changed
+616 -2

No files matched your search

@@ -0,0 +1,63 @@
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();
}
}