From 9399ae7ced6105dc70b493a5e9586d048f585fdb Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Sun, 14 Jul 2024 11:16:15 -0700 Subject: [PATCH] Ensure projects are removed from galleries, even if they somehow didn't have a gallery ID in them. --- CHANGELOG.md | 1 + src/components/project/Collaborators.svelte | 8 +++++--- src/db/GalleryDatabase.ts | 18 +++++++++++++----- src/db/ProjectsDatabase.ts | 14 +++++++++----- src/routes/gallery/[galleryid]/+page.svelte | 8 ++++++-- src/routes/projects/+page.svelte | 6 +++++- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9227a5c5..7cb0b039b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http: - Improved `MissingInput` conflict. - Changed value of divide by zero to non-a-number; defined not-a-number literal. - [#524](https://github.com/wordplaydev/wordplay/issues/524) Fixed color of drop downs in dark mode. +- [#525](https://github.com/wordplaydev/wordplay/issues/525) Ensure projects are removed from galleries, even if they somehow didn't have a gallery ID in them. ## 0.10.4 2024-07-08 diff --git a/src/components/project/Collaborators.svelte b/src/components/project/Collaborators.svelte index 055a29ef0..e7cf9ffbb 100644 --- a/src/components/project/Collaborators.svelte +++ b/src/components/project/Collaborators.svelte @@ -67,10 +67,12 @@ }; }), ]} - change={(value) => { + change={(galleryID) => { // Ask the gallery database to put this project in the gallery. - if (value) Galleries.addProject(project, value); - else Galleries.removeProject(project); + if (galleryID) Galleries.addProject(project, galleryID); + else { + Galleries.removeProject(project, null); + } }} /> diff --git a/src/db/GalleryDatabase.ts b/src/db/GalleryDatabase.ts index 724320ad6..1f54d13ca 100644 --- a/src/db/GalleryDatabase.ts +++ b/src/db/GalleryDatabase.ts @@ -276,8 +276,14 @@ export default class GalleryDatabase { } // Remove the project from the gallery that it's in. - async removeProject(project: Project) { - const galleryID = project.getGallery(); + async removeProject(project: Project, galleryID: string | null) { + // Revise the project with a gallery ID. + await this.removeProjectFromGallery(project); + + // Find the gallery from the given or the project, if provided. + galleryID = galleryID ?? project.getGallery(); + + // No gallery? No edit. if (galleryID === null) return; // Find the gallery. @@ -286,13 +292,15 @@ export default class GalleryDatabase { // No matching gallery? Bail. if (gallery === undefined) return; - // Revise the project with a gallery ID. - this.database.Projects.edit(project.withGallery(null), false, true); - // Revise the gallery with a project ID. this.edit(gallery.withoutProject(project.getID())); } + async removeProjectFromGallery(project: Project) { + // Revise the project with a gallery ID. + this.database.Projects.edit(project.withGallery(null), false, true); + } + // Remove the given creator from the gallery, and all of their projects. async removeCreator(gallery: Gallery, uid: string) { const projectsToKeep = await this.removeCreatorPojrects(gallery, uid); diff --git a/src/db/ProjectsDatabase.ts b/src/db/ProjectsDatabase.ts index 389f4e45a..9201ee503 100644 --- a/src/db/ProjectsDatabase.ts +++ b/src/db/ProjectsDatabase.ts @@ -348,8 +348,9 @@ export default class ProjectsDatabase { return newProject.getID(); } - copy(project: Project, newOwner: string | null) { - const clone = project.copy(newOwner); + copy(project: Project, newOwner: string | null, gallery: string | null) { + const clone = project.copy(newOwner).withGallery(gallery); + this.track(clone, true, PersistenceType.Online, false); return clone.getID(); } @@ -479,8 +480,9 @@ export default class ProjectsDatabase { const current = history.getCurrent(); // If the project is in a gallery, remove it. - if (current.getGallery()) - await this.database.Galleries.removeProject(current); + const gallery = current.getGallery(); + if (gallery) + await this.database.Galleries.removeProject(current, gallery); // Mark the project archived after its removed from the gallery. this.edit(current.asArchived(archive), false, true); @@ -506,7 +508,9 @@ export default class ProjectsDatabase { if (project === undefined) return; // Remove the project from it's gallery. - await this.database.Galleries.removeProject(project); + const gallery = project.getGallery(); + if (gallery) + await this.database.Galleries.removeProject(project, gallery); // Delete the project doc await deleteDoc(doc(firestore, ProjectsCollection, id)); diff --git a/src/routes/gallery/[galleryid]/+page.svelte b/src/routes/gallery/[galleryid]/+page.svelte index e666aa01b..563161db2 100644 --- a/src/routes/gallery/[galleryid]/+page.svelte +++ b/src/routes/gallery/[galleryid]/+page.svelte @@ -145,6 +145,7 @@ const newProjectID = Projects.copy( template, $user?.uid ?? null, + gallery.getID(), ); Galleries.edit( gallery.withProject(newProjectID), @@ -184,8 +185,11 @@ ), action: () => gallery - ? Galleries.removeProject(project) - : undefined, + ? Galleries.removeProject( + project, + gallery.getID(), + ) + : false, label: '⨉', } : false; diff --git a/src/routes/projects/+page.svelte b/src/routes/projects/+page.svelte index 41cda4f25..31663f3d4 100644 --- a/src/routes/projects/+page.svelte +++ b/src/routes/projects/+page.svelte @@ -53,7 +53,11 @@ /> { - const newProjectID = Projects.copy(template, $user?.uid ?? null); + const newProjectID = Projects.copy( + template, + $user?.uid ?? null, + null, + ); goto(`/project/${newProjectID}`); }} />