From dd0bcbe57dfb26419d2e610b5cc67e01cca3fb91 Mon Sep 17 00:00:00 2001 From: Kara Hoagland Date: Sun, 27 Nov 2022 17:31:13 -0800 Subject: [PATCH] moved from details to localStorage --- source/assets/scripts/ReviewDetails.js | 11 +---------- source/assets/scripts/localStorage.js | 8 ++++++++ 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/source/assets/scripts/ReviewDetails.js b/source/assets/scripts/ReviewDetails.js index 2fa27a0..89483c2 100644 --- a/source/assets/scripts/ReviewDetails.js +++ b/source/assets/scripts/ReviewDetails.js @@ -178,16 +178,7 @@ function setupUpdate(){ } newData["reviewID"] = currID; - - //Get diff of tags and update storage - //Note: we can either get the difference twice or keep track when we delete and add - //if keep track also need to make sure that a tag doesn't end up on both list - let deletedTags = currReview["tags"].filter(x => !newData["tags"].includes(x)); - let addedTags = newData["tags"].filter(x => !currReview["tags"].includes(x)); - - deleteTagsFromStorage(currID, deletedTags); - addTagsToStorage(currID, addedTags); - + updateReviewToStorage(currID, newData); updateDiv.classList.add("hidden"); diff --git a/source/assets/scripts/localStorage.js b/source/assets/scripts/localStorage.js index 35b95c6..2aaf914 100644 --- a/source/assets/scripts/localStorage.js +++ b/source/assets/scripts/localStorage.js @@ -40,6 +40,14 @@ export function getReviewFromStorage(ID){ * @param {Object} review to store */ export function updateReviewToStorage(ID, review){ + let oldReview = JSON.parse(localStorage.getItem(`review${ID}`)); + + //Get diff of tags and update storage + let deletedTags = oldReview["tags"].filter(x => !review["tags"].includes(x)); + let addedTags = review["tags"].filter(x => !oldReview["tags"].includes(x)); + deleteTagsFromStorage(currID, deletedTags); + addTagsToStorage(currID, addedTags); + // set the review entry with ID to the review object localStorage.setItem(`review${ID}`, JSON.stringify(review)); }