Skip to content

Commit

Permalink
Merge pull request #1636 from Sefaria/feature/sc-20636/users-are-unab…
Browse files Browse the repository at this point in the history
…le-to-get-rid-of-strapi-banners

Banners (and modals) wouldn't go away but now they will
  • Loading branch information
relyks committed Sep 14, 2023
2 parents 1b08894 + 0c1d5f7 commit c9e07b2
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions static/js/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,15 @@ function StrapiDataProvider({ children }) {
let modals = result.data?.modals?.data;
let banners = result.data?.banners?.data;

let removeKeysFromLocalStorageWithPrefix = (prefix) => {
let removeContentKeysFromLocalStorage = ({ prefix = "", except = "" } = {}) => {
let keysToRemove = [];
// Removing keys while iterating affects the length of localStorage
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
if (key.startsWith(prefix)) {
if (
key.startsWith(prefix) &&
(except === "" || key !== prefix + except)
) {
keysToRemove.push(key);
}
}
Expand All @@ -217,8 +220,12 @@ function StrapiDataProvider({ children }) {
currentDate <= new Date(modal.attributes.modalEndDate)
);
if (modal) {
// Remove any other previous modals since there is a new modal to replace it
removeKeysFromLocalStorageWithPrefix("modal_");
// Remove any other previous modals since there is potentially new modal to replace it
// However, do not remove the existing modal if the eligible one found is the same as the current one
removeContentKeysFromLocalStorage({
prefix: "modal_",
except: modal.attributes.internalModalName,
});

// Check if there is a Hebrew translation for the modal
if (modal.attributes.localizations?.data?.length) {
Expand Down Expand Up @@ -262,8 +269,12 @@ function StrapiDataProvider({ children }) {
);

if (banner) {
// Remove any other previous banners since there is a new banner to replace it
removeKeysFromLocalStorageWithPrefix("banner_");
// Remove any other previous banner since there is potentially new modal to replace it
// However, do not remove the existing banner if the eligible one found is the same as the current one
removeContentKeysFromLocalStorage({
prefix: "banner_",
except: banner.attributes.internalBannerName,
});

// Check if there is a Hebrew translation
if (banner.attributes.localizations?.data?.length) {
Expand Down

0 comments on commit c9e07b2

Please sign in to comment.