diff --git a/components/core/CarouselSidebarData.js b/components/core/CarouselSidebarData.js index d5578026..ba942a4c 100644 --- a/components/core/CarouselSidebarData.js +++ b/components/core/CarouselSidebarData.js @@ -498,8 +498,8 @@ class CarouselSidebarData extends React.Component { const response = UserBehaviors.deleteTag(tag); if (response.success) { - /* this.setState({ suggestions: response.tags }); */ this.props.onUpdateViewer({ tags: response.tags }); + this.updateSuggestions(); } if (Events.hasError(response)) { diff --git a/node_common/managers/library.js b/node_common/managers/library.js index 6ef16198..b3673b76 100644 --- a/node_common/managers/library.js +++ b/node_common/managers/library.js @@ -134,3 +134,19 @@ export const editItem = ({ user, update }) => { } return user.data; }; + +export const removeTagFromItems = ({ user, tag }) => { + const { library } = user.data; + for (let i = 0; i < library[0].children.length; i++) { + let item = library[0].children[i]; + + if (!item.tags) continue; + + let tagIndex = item.tags.indexOf(tag); + if (tagIndex > -1) { + item.tags.splice(tagIndex, 1); + } + } + + return user.data; +}; diff --git a/pages/api/tags/delete-tag.js b/pages/api/tags/delete-tag.js index f1adc0ea..2724b411 100644 --- a/pages/api/tags/delete-tag.js +++ b/pages/api/tags/delete-tag.js @@ -2,6 +2,7 @@ import * as Utilities from "~/node_common/utilities"; import * as Data from "~/node_common/data"; import * as ViewerManager from "~/node_common/managers/viewer"; import * as SearchManager from "~/node_common/managers/search"; +import * as LibraryManager from "~/node_common/managers/library"; export default async (req, res) => { const id = Utilities.getIdFromCookie(req); @@ -48,7 +49,7 @@ export default async (req, res) => { let tags = slate.data.tags; let tagIndex = tags.indexOf(tagToDelete); - if (tagIndex && tagIndex > -1) { + if (tagIndex > -1) { tags.splice(tagIndex, 1); } @@ -67,15 +68,15 @@ export default async (req, res) => { SearchManager.updateSlate(newSlate, "EDIT"); } - // NOTE(daniel): Remove tag from object - for (const item of user.data.library[0].children) { - if (!item.tags) continue; + // NOTE(daniel): Remove tag from all data objects + let newUserData = LibraryManager.removeTagFromItems({ user, tag: tagToDelete }); + let updateResponse = await Data.updateUserById({ + id: user.id, + data: newUserData, + }); - let tags = item.tags; - let tagIndex = tags.indexOf(tagToDelete); - if (tagIndex > -1) { - tags.splice(tagIndex, 1); - } + if (!updateResponse || updateResponse.error) { + return res.status(500).send({ decorator: "SERVER_EDIT_DATA_NOT_UPDATED", error: true }); } let tags; @@ -84,7 +85,6 @@ export default async (req, res) => { ViewerManager.hydratePartialSlates(slates, id); tags = Utilities.getUserTags({ library: user.data.library[0].children, slates }); - console.log(tags); } return res.status(200).send({ decorator: "SERVER_DELETE_TAGS", success: true, tags });