Skip to content

Commit

Permalink
Ensure projects are removed from galleries, even if they somehow didn…
Browse files Browse the repository at this point in the history
…'t have a gallery ID in them.
  • Loading branch information
amyjko committed Jul 14, 2024
1 parent d5c6b33 commit 9399ae7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 5 additions & 3 deletions src/components/project/Collaborators.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}}
/>

Expand Down
18 changes: 13 additions & 5 deletions src/db/GalleryDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
14 changes: 9 additions & 5 deletions src/db/ProjectsDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
8 changes: 6 additions & 2 deletions src/routes/gallery/[galleryid]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
const newProjectID = Projects.copy(
template,
$user?.uid ?? null,
gallery.getID(),
);
Galleries.edit(
gallery.withProject(newProjectID),
Expand Down Expand Up @@ -184,8 +185,11 @@
),
action: () =>
gallery
? Galleries.removeProject(project)
: undefined,
? Galleries.removeProject(
project,
gallery.getID(),
)
: false,
label: '',
}
: false;
Expand Down
6 changes: 5 additions & 1 deletion src/routes/projects/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@
/>
<AddProject
add={(template) => {
const newProjectID = Projects.copy(template, $user?.uid ?? null);
const newProjectID = Projects.copy(
template,
$user?.uid ?? null,
null,
);
goto(`/project/${newProjectID}`);
}}
/>
Expand Down

0 comments on commit 9399ae7

Please sign in to comment.