Fixed logic used to show/hide donate banner (#11403)

* Donate banner - fixed logic used to show/hide donate banner

* no need to remove the item. Simply replacing it with a new value later in the implementation is sufficient
This commit is contained in:
Mavis Ou 2023-11-16 14:05:46 -08:00 коммит произвёл GitHub
Родитель 2b59149408
Коммит 0fcd9c5d2d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 15 добавлений и 7 удалений

Просмотреть файл

@ -3,12 +3,10 @@
*/
const DonateBanner = {
DISMISS_KEY: "donate banner dismiss day",
init: function () {
const today = new Date();
const DISMISS_KEY = "donate banner dismiss day";
const banner = document.querySelector(`.donate-banner`);
const hideBanner =
parseInt(localStorage.getItem(DISMISS_KEY)) === today.getDay();
const hideBanner = this.shouldHideBanner();
const closeButton = hideBanner
? undefined
: banner?.querySelector(`a.banner-close`);
@ -21,8 +19,6 @@ const DonateBanner = {
closeButton?.addEventListener(`click`, (e) => {
e.preventDefault();
localStorage.removeItem(DISMISS_KEY);
banner.style.position = "absolute";
banner.style.top = "0px";
@ -62,10 +58,22 @@ const DonateBanner = {
closeAnimation.onfinish = () => {
banner.classList.add(`tw-hidden`);
localStorage.setItem(DISMISS_KEY, today.getDay());
this.setDismissDate();
};
});
},
getDismissDate() {
return localStorage.getItem(this.DISMISS_KEY);
},
setDismissDate() {
const today = new Date();
localStorage.setItem(this.DISMISS_KEY, today.toDateString()); // e.g., Thu Nov 09 2023
},
shouldHideBanner() {
const today = new Date();
return this.getDismissDate() === today.toDateString();
},
};
export default DonateBanner;