Front: fix entry removed from collection not updating UI

This commit is contained in:
X4Lo committed 2024-10-21 15:55:36 +01:00
1 parent 302f2830ff
commit 1e1384d777
10 files changed
+146 -49

No files matched your search

+13 -15
View File
@@ -301,18 +301,18 @@ collectionRouter.delete('/:id', async (req, res) => {
}); });
// * POST - ADD ENTRY TO A COLLECTION // * POST - ADD ENTRY TO A COLLECTION
collectionRouter.post('/:userCollectionId/entries/:entryId', async (req, res) => { collectionRouter.post('/:collectionId/entries/:entryId', async (req, res) => {
try { try {
const { userCollectionId, entryId } = req.params; const { collectionId, entryId } = req.params;
const newUserCollectionEntry = await UserCollectionEntry.create({ const newUserCollectionEntry = await UserCollectionEntry.create({
user_collection_id: userCollectionId, user_collection_id: collectionId,
entry_id: entryId entry_id: entryId
}); });
// ? set a default poster if none exists // ? set a default poster if none exists
const userCollection = await Collection.findByPk(userCollectionId); const userCollection = await Collection.findByPk(collectionId);
if (!userCollection.poster_path || userCollection.poster_path.trim() === '') { if (!userCollection.poster_path || userCollection.poster_path.trim() == '') {
const entry = await Entry.findByPk(entryId); const entry = await Entry.findByPk(entryId);
userCollection.poster_path = entry.poster_path; userCollection.poster_path = entry.poster_path;
await userCollection.save(); await userCollection.save();
@@ -326,15 +326,15 @@ collectionRouter.post('/:userCollectionId/entries/:entryId', async (req, res) =>
}); });
// * PATCH - UPDATE ENTRY ORDER IN COLLECTION // * PATCH - UPDATE ENTRY ORDER IN COLLECTION
collectionRouter.patch('/:userCollectionId/entries/:entryId', async (req, res) => { collectionRouter.patch('/:collectionId/entries/:entryId', async (req, res) => {
try { try {
const { userCollectionId, entryId } = req.params; const { collectionId, entryId } = req.params;
const { newOrder } = req.body; const { newOrder } = req.body;
// ? getting the collectionEntry // ? getting the collectionEntry
const collectionEntry = await UserCollectionEntry.findOne({ const collectionEntry = await UserCollectionEntry.findOne({
where: { where: {
user_collection_id: userCollectionId, user_collection_id: collectionId,
entry_id: entryId entry_id: entryId
} }
}); });
@@ -354,23 +354,21 @@ collectionRouter.patch('/:userCollectionId/entries/:entryId', async (req, res) =
}); });
// * DELETE - REMOVE AN ENTRY FROM A COLLECTION // * DELETE - REMOVE AN ENTRY FROM A COLLECTION
collectionRouter.delete('/:userCollectionId/entries/:entryId', async (req, res) => { collectionRouter.delete('/:collectionId/entries/:entryId', async (req, res) => {
try { try {
const { userCollectionId, entryId } = req.params; const { collectionId, entryId } = req.params;
const deleted = await UserCollectionEntry.findOne({ const deleted = await UserCollectionEntry.findOne({
where: { where: {
user_collection_id: userCollectionId, user_collection_id: collectionId,
entry_id: entryId entry_id: entryId
} }
}); });
console.log(`deleteing : `, deleted)
const deletedCount = deleted.destroy();
if (deleted) { if (deleted) {
await deleted.destroy();
res.status(204).send(); res.status(204).send();
} else { } else {
res.status(404).json({ message: 'UserCollection entry not found.' }); res.status(404).json({ message: 'CollectionEntry not found.' });
} }
} catch (error) { } catch (error) {
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
@@ -23,7 +23,7 @@ export class CollectionService {
} }
// get a list by id // get a list by id
getCollection(collectionId: string) { getCollection(collectionId: number): Observable<Collection> {
return this.http.get<Collection>( return this.http.get<Collection>(
`${this.backend_apiUrl}/${this.apiAction}/${collectionId}` `${this.backend_apiUrl}/${this.apiAction}/${collectionId}`
); );
@@ -94,15 +94,15 @@ export class CollectionEntryCardComponent implements OnInit {
.removeEntryFromCollection(this.collectionId, this.entry.id) .removeEntryFromCollection(this.collectionId, this.entry.id)
.subscribe({ .subscribe({
next: (response) => { next: (response) => {
// TODO confirm notification // TODO toast: entry removed from collection
// console.log('Entry successfully removed from collection.', response);
console.log('Entry successfully removed from list.', response); this.emitRefrechEvent();
this.entry.id = 0; this.entry.id = 0;
this.entry.isInLibrary = false; this.entry.isInLibrary = false;
}, },
error: (error) => { error: (error) => {
// TODO error notification // TODO error notification
console.error('Error removing entry from list:', error); console.error('Error removing entry from collection:', error);
}, },
}); });
} }
@@ -116,6 +116,6 @@ export class CollectionEntryCardComponent implements OnInit {
// ? EVENT EMITTER // ? EVENT EMITTER
emitRefrechEvent() { emitRefrechEvent() {
this.refrechEvent.emit(); this.refrechEvent.emit({ type: 'delete', id: this.entry.id });
} }
} }
@@ -1,6 +1,6 @@
<div class="movies-container"> <div class="movies-container">
<ng-container *ngFor="let entry of entries"> <ng-container *ngFor="let entry of entries">
<app-collection-entry-card [entry]="entry" [collectionId]="collectionId" (refrechEvent)="refrechEventReceiver()" <app-collection-entry-card [entry]="entry" [collectionId]="collectionId" (refrechEvent)="onRefrechEvent($event)"
@animateDestroy> @animateDestroy>
</app-collection-entry-card> </app-collection-entry-card>
</ng-container> </ng-container>
@@ -1,4 +1,4 @@
import { Component, Input } from '@angular/core'; import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Entry } from '../../../../shared/models/entry.model'; import { Entry } from '../../../../shared/models/entry.model';
import { import {
@@ -23,8 +23,9 @@ import {
export class CollectionEntryGridComponent { export class CollectionEntryGridComponent {
@Input() entries: Entry[] = []; @Input() entries: Entry[] = [];
@Input() collectionId!: number; @Input() collectionId!: number;
@Output() refrechEvent = new EventEmitter();
refrechEventReceiver() { onRefrechEvent(eventData: any) {
console.log('event recieved'); this.refrechEvent.emit(eventData);
} }
} }
@@ -3,7 +3,7 @@
<div class="product-table"> <div class="product-table">
<div class="table-responsive white-space-nowrap"> <div class="table-responsive white-space-nowrap">
<table class="table align-middle"> <table class="table align-middle">
<thead class="table-light"> <thead class="">
<!-- * table head start --> <!-- * table head start -->
<tr> <tr>
<th> <th>
@@ -14,6 +14,10 @@
</div> </div>
</div> </div>
<!--end breadcrumb--> <!--end breadcrumb-->
<!-- <div class="card w-100" [ngStyle]="{'background-image': 'url(' + getCollectionBackdropPath(collection) + ')', 'background-size': 'cover', 'background-position': 'center'}"> -->
<!-- <div class="card-body p-0" style="background: rgba(0, 0, 0, 0.5);"> -->
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<div class="d-flex justify-content-between align-items-center mb-2"> <div class="d-flex justify-content-between align-items-center mb-2">
@@ -58,8 +62,8 @@
<!-- ! grid --> <!-- ! grid -->
<div id="gridDiv" *ngIf="viewMode == 'grid'"> <div id="gridDiv" *ngIf="viewMode == 'grid'">
<h1 *ngIf="screenMode">{{collection.name}}</h1> <h1 *ngIf="screenMode">{{collection.name}}</h1>
<app-collection-entry-grid [entries]="collection.entries" <app-collection-entry-grid [entries]="collection.entries" [collectionId]="this.collection.id"
[collectionId]="this.collection.id"></app-collection-entry-grid> (refrechEvent)="onRefrechEvent($event)"></app-collection-entry-grid>
</div> </div>
<!-- ! table --> <!-- ! table -->
@@ -8,6 +8,7 @@ import { MatDialog } from '@angular/material/dialog';
import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component'; import { ConfirmModalComponent } from '../../../../shared/components/modals/confirm-modal/confirm-modal.component';
import { ScreenshotService } from '../../../../core/services/screenshot.service'; import { ScreenshotService } from '../../../../core/services/screenshot.service';
import { CollectionEntryDTO } from '../../../../shared/models/dtos/collection-entry-dto.model'; import { CollectionEntryDTO } from '../../../../shared/models/dtos/collection-entry-dto.model';
import { environment } from '../../../../../environments/environment.prod';
@Component({ @Component({
selector: 'app-collection-info-page', selector: 'app-collection-info-page',
@@ -35,11 +36,18 @@ export class CollectionInfoPageComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
const id = this.route.snapshot.paramMap.get('id'); const id = this.route.snapshot.paramMap.get('id');
if (id == null) return; // todo redirect if no id is provided if (id == null) return; // todo redirect if no id is provided
this.collection.id = parseInt(id);
this.collectionService.getCollection(id.toString()).subscribe({ this.fetchCollection();
next: (data) => { }
this.collection = data as unknown as Collection;
this.collection.entries = this.collection.entries as CollectionEntryDTO[]; fetchCollection() {
this.collectionService.getCollection(this.collection.id).subscribe({
next: (result) => {
this.collection = result;
if (!this.collection.isUserCollection)
this.collection.entries = this.sortEntries(this.collection.entries);
this.createdAt = this.getCreatedDate()?.toString() || ''; this.createdAt = this.getCreatedDate()?.toString() || '';
this.updatedAt = this.getUpdatedAt()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || '';
@@ -54,6 +62,13 @@ export class CollectionInfoPageComponent implements OnInit {
}); });
} }
sortEntries(entries: CollectionEntryDTO[]) {
return entries.sort(
(a, b) =>
new Date(a.release_date).getTime() - new Date(b.release_date).getTime()
);
}
getListId() { getListId() {
return this.collection.id + ''; return this.collection.id + '';
} }
@@ -105,13 +120,13 @@ export class CollectionInfoPageComponent implements OnInit {
if (!this.collection) return; if (!this.collection) return;
const dialogRef = this.dialog.open(CollectionEditModalComponent, { const dialogRef = this.dialog.open(CollectionEditModalComponent, {
width: '360px', width: '50%',
data: { data: {
name: this.collection.name, name: this.collection.name,
description: this.collection.description, description: this.collection.description,
userRating: this.collection.userRating || 0, userRating: this.collection.userRating || 0,
poster: this.collection.posterPath, poster: this.getCollectionPosterPath(this.collection),
backdrop: this.collection.backdropPath, backdrop: this.getCollectionBackdropPath(this.collection),
}, },
}); });
@@ -158,9 +173,7 @@ export class CollectionInfoPageComponent implements OnInit {
if (result) { if (result) {
if (result.confirmed != true) return; if (result.confirmed != true) return;
this.collectionService this.collectionService.deleteCollection(this.collection.id).subscribe({
.deleteCollection(this.collection.id)
.subscribe({
next: () => { next: () => {
// TODO confirm notification // TODO confirm notification
this.router.navigate(['/collections']); this.router.navigate(['/collections']);
@@ -212,7 +225,8 @@ export class CollectionInfoPageComponent implements OnInit {
this.collectionService.refreshCollection(this.collection.id).subscribe({ this.collectionService.refreshCollection(this.collection.id).subscribe({
next: (data) => { next: (data) => {
this.collection = data as unknown as Collection; this.collection = data as unknown as Collection;
this.collection.entries = this.collection.entries as CollectionEntryDTO[]; this.collection.entries = this.collection
.entries as CollectionEntryDTO[];
this.createdAt = this.getCreatedDate()?.toString() || ''; this.createdAt = this.getCreatedDate()?.toString() || '';
this.updatedAt = this.getUpdatedAt()?.toString() || ''; this.updatedAt = this.getUpdatedAt()?.toString() || '';
@@ -228,9 +242,7 @@ export class CollectionInfoPageComponent implements OnInit {
} }
getCollectionParts() { getCollectionParts() {
this.collectionService this.collectionService.getCollectionParts(this.collection.id).subscribe({
.getCollectionParts(this.collection.id)
.subscribe({
next: (data) => { next: (data) => {
this.collection = data as unknown as Collection; this.collection = data as unknown as Collection;
this.collection.entries = this.collection this.collection.entries = this.collection
@@ -255,4 +267,88 @@ export class CollectionInfoPageComponent implements OnInit {
'_blank' '_blank'
); );
} }
getCollectionPosterPath(collection: Collection) {
// console.log(`collection media:`, collection.medias);
if (collection.medias && collection.medias.length != 0) {
const posters = collection.medias.filter((m) => m.type == 'poster');
if (posters.length != 0) {
const selectedPosters = posters.filter((m) => m.isSelected);
// console.log(selectedPosters);
let selectedPoster;
if (selectedPosters.length != 0) {
selectedPoster = selectedPosters[0].path;
} else {
selectedPoster = posters[0].path;
}
return (
environment.API_URL +
`/media/collections/posters/preview/${selectedPoster}`
);
}
}
// if (collection.entries && collection.entries.length != 0) {
// // * use the poster of the first entry in the collection
// // * if the collection is empty
// return this.getEntryBackdropPath(collection.entries[0]);
// } else
if (collection.posterPath) {
// * use first remote tmdb poster
// * if collection has none and has no entry
return `https://image.tmdb.org/t/p/w1280/${collection.posterPath}`;
} else {
// * use default poster
// * if collection has totaly no poster
return 'assets/images/default/default-poster.png';
}
}
getCollectionBackdropPath(collection: Collection) {
// console.log(`collection media:`, collection.medias);
if (collection.medias && collection.medias.length != 0) {
const backdrops = collection.medias.filter((m) => m.type == 'backdrop');
if (backdrops.length != 0) {
const selectedBackdrops = backdrops.filter((m) => m.isSelected);
// console.log(selectedBackdrops);
let selectedBackdrop;
if (selectedBackdrops.length != 0) {
selectedBackdrop = selectedBackdrops[0].path;
} else {
selectedBackdrop = backdrops[0].path;
}
return (
environment.API_URL +
`/media/collections/backdrops/preview/${selectedBackdrop}`
);
}
}
// if (collection.entries && collection.entries.length != 0) {
// // * use the poster of the first entry in the collection
// // * if the collection is empty
// return this.getEntryBackdropPath(collection.entries[0]);
// } else
if (collection.backdropPath) {
// * use first remote tmdb poster
// * if collection has none and has no entry
return `https://image.tmdb.org/t/p/w1280/${collection.backdropPath}`;
} else {
// * use default poster
// * if collection has totaly no poster
return 'assets/images/default/default-backdrop.png';
}
}
onRefrechEvent(eventData: any) {
if (eventData.type == 'delete') {
this.collection.entries = this.collection.entries.filter(
(e) => e.id != eventData.id
);
}
}
} }
@@ -3,8 +3,6 @@ import { Collection } from '../../../../shared/models/collection.model';
import { CollectionService } from '../../../../core/services/collection.service'; import { CollectionService } from '../../../../core/services/collection.service';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { trigger, transition, style, animate } from '@angular/animations'; import { trigger, transition, style, animate } from '@angular/animations';
import { environment } from '../../../../../environments/environment.prod';
import { Entry } from '../../../../shared/models/entry.model';
@Component({ @Component({
selector: 'app-collection-card', selector: 'app-collection-card',
@@ -27,7 +25,7 @@ import { Entry } from '../../../../shared/models/entry.model';
export class CollectionCardComponent { export class CollectionCardComponent {
@Input() collection!: Collection; @Input() collection!: Collection;
constructor(private listService: CollectionService, private router: Router) {} constructor(private collectionService: CollectionService, private router: Router) {}
getCreatedDate(): string { getCreatedDate(): string {
if (this.collection.createdAt == null) return ''; if (this.collection.createdAt == null) return '';
@@ -40,6 +38,6 @@ export class CollectionCardComponent {
} }
deleteList() { deleteList() {
this.listService.deleteCollection(this.collection.id); this.collectionService.deleteCollection(this.collection.id);
} }
} }
@@ -3,7 +3,7 @@ import { FormControl } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { CollectionService } from '../../../../core/services/collection.service'; import { CollectionService } from '../../../../core/services/collection.service';
import { NotificationPopupComponent } from '../../notification-popup/notification-popup.component'; import { NotificationPopupComponent } from '../../notification-popup/notification-popup.component';
import { NotificationService } from '../../../services/notification.service'; import { ToastService } from '../../../services/toast.service';
import { Collection } from '../../../models/collection.model'; import { Collection } from '../../../models/collection.model';
import { import {
@@ -50,7 +50,7 @@ export class UserCollectionsModalComponent implements OnInit {
public dialogRef: MatDialogRef<UserCollectionsModalComponent>, public dialogRef: MatDialogRef<UserCollectionsModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any, @Inject(MAT_DIALOG_DATA) public data: any,
private collectionService: CollectionService, private collectionService: CollectionService,
private notificationService: NotificationService private toastService: ToastService
) {} ) {}
ngOnInit() { ngOnInit() {
@@ -174,6 +174,6 @@ export class UserCollectionsModalComponent implements OnInit {
showNotification(title: string, message: string, type: string): void { showNotification(title: string, message: string, type: string): void {
// this.notificationComponent.showNotification(message, type); // this.notificationComponent.showNotification(message, type);
this.notificationService.showNotification(title, message, type); this.toastService.show(title, message, type);
} }
} }