Files
Showy/frontend/src/app/features/collections/user-collection-info/collection-edit-modal/collection-edit-modal.component.ts
T

87 lines
2.4 KiB
TypeScript

import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {
trigger,
state,
style,
transition,
animate,
} from '@angular/animations';
@Component({
selector: 'app-collection-edit-modal',
templateUrl: './collection-edit-modal.component.html',
styleUrl: './collection-edit-modal.component.css',
animations: [
trigger('fadeInOut', [
state('in', style({ opacity: 1 })),
transition(':enter', [style({ opacity: 0 }), animate(600)]),
transition(':leave', [animate(600, style({ opacity: 0 }))]),
]),
trigger('scaleInOut', [
transition(':enter', [
state('in', style({ opacity: 1 })),
style({ transform: 'scale(0)' }),
animate('200ms ease-in-out', style({ transform: 'scale(1)' })),
]),
transition(':leave', [
animate('200ms ease-in-out', style({ transform: 'scale(0)' })),
]),
]),
],
})
export class CollectionEditModalComponent implements OnInit {
form!: FormGroup;
constructor(
public dialogRef: MatDialogRef<CollectionEditModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder
) {}
ngOnInit() {
if (this.data == null) return;
this.form = this.fb.group({
name: [this.data.name, Validators.required],
description: [this.data.description],
userRating: [this.data.userRating],
poster: [null],
});
}
onFileChange(event: any) {
const file = event.target.files[0];
this.form.patchValue({ poster: file });
this.form.get('poster')?.updateValueAndValidity();
}
onConfirm(): void {
if (this.form.invalid) return this.form.markAllAsTouched();
let formData = new FormData();
formData.append('name', this.form.get('name')?.value.toString().trim());
formData.append('description', this.form.get('description')?.value || '');
formData.append(
'userRating',
this.form.get('userRating')?.value?.toString() || '0'
);
// Check and append file if it exists
const posterFile = this.form.get('poster')?.value;
if (posterFile) {
formData.append('poster', posterFile);
}
formData.append('is_poster_link', 'false');
this.dialogRef.close({ formData: formData });
}
onClose(): void {
this.dialogRef.close();
}
}