Frontend: Entry Media Modal
- Improved filter logic. - Added toasts. - Removed MediaUploadModal for better user experience. - Synced entry media state.
This commit is contained in:
1 parent
8533076080
commit
6bf27e5519
2 files changed
+100
-47
No files matched your search
@@ -9,37 +9,40 @@
|
||||
<div class="nav-item-group d-flex">
|
||||
<li class="nav-item">
|
||||
<button class="nav-link active" id="posters-tab" data-bs-toggle="tab" data-bs-target="#posters" type="button"
|
||||
role="tab" aria-controls="posters" aria-selected="true" (click)="setTab('poster')">Posters</button>
|
||||
role="tab" aria-controls="posters" aria-selected="true" (click)="selectMediaType('poster')">Posters</button>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<button class="nav-link" id="backdrops-tab" data-bs-toggle="tab" data-bs-target="#backdrops" type="button"
|
||||
role="tab" aria-controls="backdrops" aria-selected="false" (click)="setTab('backdrop')">Backdrops</button>
|
||||
role="tab" aria-controls="backdrops" aria-selected="false"
|
||||
(click)="selectMediaType('backdrop')">Backdrops</button>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<button class="nav-link" id="logos-tab" data-bs-toggle="tab" data-bs-target="#logos" type="button" role="tab"
|
||||
aria-controls="logos" aria-selected="false" (click)="setTab('logo')">Logos</button>
|
||||
aria-controls="logos" aria-selected="false" (click)="selectMediaType('logo')">Logos</button>
|
||||
</li>
|
||||
</div>
|
||||
<li class="nav-item">
|
||||
<button class="btn btn-primary rounded-0 p-1 bg-transparent border-0 add-btn d-flex align-items-center"
|
||||
type="button" (click)="openUploadModal()">
|
||||
type="button" (click)="fileInput.click()">
|
||||
<span class="material-symbols-outlined me-1">
|
||||
add_photo_alternate
|
||||
</span>
|
||||
Add An Image
|
||||
</button>
|
||||
<input type="file" #fileInput style="display: none" (change)="onFileSelected($event)" hidden>
|
||||
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="tab-content" id="mediaTabContent">
|
||||
<div class="tab-pane fade show active" id="posters" role="tabpanel" aria-labelledby="posters-tab">
|
||||
<div class="row row-cols-1 row-cols-md-4 g-4">
|
||||
<div class="col" *ngFor="let media of filterMedia('poster')">
|
||||
<div class="row row-cols-1 row-cols-md-4 g-4" *ngIf="tab == 'poster'">
|
||||
<div class="col" *ngFor="let media of filteredMedia">
|
||||
<div class="card image-hover-container">
|
||||
<img [src]="getPath('posters', media.path)" class="card-img-top" alt="Media Image">
|
||||
<div class="image-hover-overlay">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media.id)">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media)">
|
||||
<span class="material-symbols-outlined">delete</span>
|
||||
</button>
|
||||
<button class="btn btn-icon btn-icon-select" (click)="selectMedia(media)">
|
||||
@@ -54,12 +57,12 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="backdrops" role="tabpanel" aria-labelledby="backdrops-tab">
|
||||
<div class="row row-cols-1 row-cols-md-2 g-4">
|
||||
<div class="col" *ngFor="let media of filterMedia('backdrop')">
|
||||
<div class="row row-cols-1 row-cols-md-2 g-4" *ngIf="tab == 'backdrop'">
|
||||
<div class="col" *ngFor="let media of filteredMedia">
|
||||
<div class="card image-hover-container">
|
||||
<img [src]="getPath('backdrops', media.path)" class="card-img-top" alt="Media Image">
|
||||
<div class="image-hover-overlay">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media.id)">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media)">
|
||||
<span class="material-symbols-outlined">delete</span>
|
||||
</button>
|
||||
<button class="btn btn-icon btn-icon-select" (click)="selectMedia(media)">
|
||||
@@ -74,12 +77,12 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="tab-pane fade" id="logos" role="tabpanel" aria-labelledby="logos-tab">
|
||||
<div class="row row-cols-1 row-cols-md-2 g-4">
|
||||
<div class="col" *ngFor="let media of filterMedia('logo')">
|
||||
<div class="row row-cols-1 row-cols-md-2 g-4" *ngIf="tab == 'logo'">
|
||||
<div class="col" *ngFor="let media of filteredMedia">
|
||||
<div class="card image-hover-container">
|
||||
<img [src]="getPath('logos', media.path)" class="card-img-top" alt="Media Image">
|
||||
<div class="image-hover-overlay">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media.id)">
|
||||
<button class="btn btn-icon btn-icon-delete" (click)="deleteMedia(media)">
|
||||
<span class="material-symbols-outlined">delete</span>
|
||||
</button>
|
||||
<button class="btn btn-icon btn-icon-select" (click)="selectMedia(media)">
|
||||
|
||||
@@ -8,6 +8,10 @@ import { Entry } from '../../../shared/models/entry.model';
|
||||
import { MediaUploadModalComponent } from '../media-upload-modal/media-upload-modal.component';
|
||||
import { EntryService } from '../../../core/services/entry.service';
|
||||
import { ConfigService } from '../../../core/services/config.service';
|
||||
import { LibraryService } from '../../../core/services/library.service';
|
||||
import { ToastService } from '../../../shared/services/toast.service';
|
||||
import { Media } from '../../../shared/models/media.model';
|
||||
import { ConfirmModalComponent } from '../../../shared/components/modals/confirm-modal/confirm-modal.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-media-modal',
|
||||
@@ -17,19 +21,22 @@ import { ConfigService } from '../../../core/services/config.service';
|
||||
export class MediaModalComponent implements OnInit {
|
||||
entry!: Entry;
|
||||
tab: string = 'poster';
|
||||
filteredMedia: Media[] = [];
|
||||
|
||||
constructor(
|
||||
public dialogRef: MatDialogRef<MediaModalComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public data: any,
|
||||
private dialog: MatDialog,
|
||||
private entryService: EntryService,
|
||||
private configService: ConfigService
|
||||
private libraryService: LibraryService,
|
||||
private configService: ConfigService,
|
||||
private toast: ToastService
|
||||
) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
if (this.data && this.data.entry) {
|
||||
this.entry = this.data.entry;
|
||||
// console.log(this.entry.medias);
|
||||
this.filterMedia(this.tab);
|
||||
} else {
|
||||
this.dialogRef.close();
|
||||
}
|
||||
@@ -39,64 +46,107 @@ export class MediaModalComponent implements OnInit {
|
||||
this.dialogRef.close();
|
||||
}
|
||||
|
||||
filterMedia(type: string) {
|
||||
return this.entry.medias.filter((media) => media.type == type);
|
||||
selectMediaType(type: string) {
|
||||
this.tab = type;
|
||||
this.filterMedia(type);
|
||||
}
|
||||
|
||||
openUploadModal() {
|
||||
const dialogRef = this.dialog.open(MediaUploadModalComponent, {
|
||||
data: {
|
||||
entryId: this.entry.id,
|
||||
mediaType: this.tab,
|
||||
},
|
||||
});
|
||||
filterMedia(type: string) {
|
||||
this.filteredMedia = this.entry.medias.filter(
|
||||
(media) => media.type === type
|
||||
);
|
||||
}
|
||||
|
||||
dialogRef.afterClosed().subscribe((result) => {
|
||||
if (result) {
|
||||
this.entry.medias.push(result.data);
|
||||
}
|
||||
});
|
||||
onFileSelected(event: Event) {
|
||||
const target = event.target as HTMLInputElement;
|
||||
const file = target.files?.[0];
|
||||
|
||||
if (file) {
|
||||
const formData = new FormData();
|
||||
formData.append(this.tab, file);
|
||||
this.entryService.addEntryMedia(this.entry.id, formData).subscribe({
|
||||
next: (result) => {
|
||||
this.entry.medias.unshift(result);
|
||||
this.libraryService.updateEntry(this.entry);
|
||||
this.filterMedia(this.tab);
|
||||
this.toast.show('', `File uploaded.`, 'success');
|
||||
},
|
||||
error: (error) => {
|
||||
// console.log(`Error uploading file: ${error}`);
|
||||
this.toast.show('', `Error uploading file.`, 'error');
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
getPath(fileType: string, filePath: string) {
|
||||
return `${this.configService.getBackendApiUrl()}/media/entries/${fileType}/preview/${filePath}`;
|
||||
}
|
||||
|
||||
setTab(tab: string) {
|
||||
this.tab = tab;
|
||||
}
|
||||
deleteMedia(media: Media) {
|
||||
const dialogRef = this.dialog.open(ConfirmModalComponent, {
|
||||
data: {
|
||||
title: 'Delete file',
|
||||
message: `Are you sure you want to delete this ${this.tab}?`,
|
||||
type: 'danger',
|
||||
},
|
||||
});
|
||||
|
||||
deleteMedia(id: number) {
|
||||
this.entryService.deleteEntryMedia(this.entry.id, id).subscribe({
|
||||
next: (result) => {
|
||||
// TODO add confirm modal
|
||||
this.entry.medias = this.entry.medias.filter((m) => m.id != id);
|
||||
},
|
||||
error: (error) => {
|
||||
console.log(error);
|
||||
},
|
||||
dialogRef.afterClosed().subscribe((result) => {
|
||||
if (result && result.confirmed) {
|
||||
this.entryService.deleteEntryMedia(this.entry.id, media.id).subscribe({
|
||||
next: (result) => {
|
||||
this.entry.medias = this.entry.medias.filter(
|
||||
(m) => m.id != media.id
|
||||
);
|
||||
|
||||
// ? if the deleted media was selected, select the first one
|
||||
if (media.isSelected) {
|
||||
this.entry.medias.forEach((m) => {
|
||||
if (m.type == this.tab) {
|
||||
m.isSelected = true;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
this.libraryService.updateEntry(this.entry);
|
||||
this.filterMedia(this.tab);
|
||||
this.toast.show('', `File deleted.`, 'success');
|
||||
},
|
||||
error: (error) => {
|
||||
this.toast.show('', `Error deleting file.`, 'error');
|
||||
// console.log(`Error deleting file: ${error}`);
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
selectMedia(media: any) {
|
||||
selectMedia(media: Media) {
|
||||
this.entryService.selectEntryMedia(this.entry.id, media.id).subscribe({
|
||||
next: (result) => {
|
||||
this.entry.medias.forEach((m) => {
|
||||
if (m.type == media.type) {
|
||||
m.isSelected = false;
|
||||
}
|
||||
if ((m.id = result.id)) {
|
||||
m.isSelected = true;
|
||||
|
||||
if (m.id == result.id) {
|
||||
m.isSelected = true;
|
||||
console.log(`found and selected: `, m);
|
||||
}
|
||||
}
|
||||
});
|
||||
this.libraryService.updateEntry(this.entry);
|
||||
this.toast.show('', `File selected.`, 'success');
|
||||
},
|
||||
error: (error) => {
|
||||
console.log(error);
|
||||
this.toast.show('', `Error selecting file.`, 'error');
|
||||
// console.log(`Error selecting file: ${error}`);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
expandMedia(media: any) {
|
||||
expandMedia(media: Media) {
|
||||
const fullSizePath = `${this.configService.getBackendApiUrl()}/media/entries/${
|
||||
media.type
|
||||
}s/original/${media.path}`;
|
||||
|
||||
Reference in new issue
Block a user