From 3e4db6cb3c7811f705d98a6d3a7d7023247a7032 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 7 Jun 2019 23:03:24 -0600 Subject: [PATCH] Dismiss announcements with cookies that expire --- src/constants.js | 1 - src/js/announcements.js | 23 +++++++++++------------ src/js/setupListeners.js | 5 ++--- src/php/router.php | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/constants.js b/src/constants.js index 644516e..4ab6656 100644 --- a/src/constants.js +++ b/src/constants.js @@ -66,4 +66,3 @@ export const DEFAULT_PAGE_SIZE = 50; export const LOCAL_STORAGE_KEY = 'dictionary'; export const SETTINGS_KEY = 'settings'; -export const DISMISSED_ANNOUNCEMENTS_KEY = 'dismissedAnnouncements'; diff --git a/src/js/announcements.js b/src/js/announcements.js index 281ec50..70f8d3d 100644 --- a/src/js/announcements.js +++ b/src/js/announcements.js @@ -1,21 +1,20 @@ -import { DISMISSED_ANNOUNCEMENTS_KEY } from "../constants"; import { fadeOutElement } from "./utilities"; +import { setCookie, getCookie } from "./StackOverflow/cookie"; -export function getDismissed() { - let dismissed = window.localStorage.getItem(DISMISSED_ANNOUNCEMENTS_KEY); - if (!dismissed) { - dismissed = []; - } else { - dismissed = JSON.parse(dismissed); - } - return dismissed; +export function isDismissed(announcementId) { + let dismissed = getCookie(announcementId); + + return dismissed === 'dismissed'; } export function dismiss(announcement) { if (announcement.id) { - const dismissed = getDismissed(); - dismissed.push(announcement.id); - window.localStorage.setItem(DISMISSED_ANNOUNCEMENTS_KEY, JSON.stringify(dismissed)); + const expireDate = announcement.dataset.expires; + const now = new Date(); + const expire = new Date(expireDate); + const timeDiff = Math.abs(now.getTime() - expire.getTime()); + const dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24)); + setCookie(announcement.id, 'dismissed', dayDifference + 1); } fadeOutElement(announcement) } diff --git a/src/js/setupListeners.js b/src/js/setupListeners.js index 0bdc10f..6e0b548 100644 --- a/src/js/setupListeners.js +++ b/src/js/setupListeners.js @@ -11,7 +11,7 @@ import { showSearchModal, clearSearchText, checkAllPartsOfSpeechFilters, uncheck import helpFile from '../markdown/help.md'; import termsFile from '../markdown/terms.md'; import privacyFile from '../markdown/privacy.md'; -import { dismiss, getDismissed } from './announcements'; +import { dismiss, isDismissed } from './announcements'; import { fadeOutElement } from './utilities'; export default function setupListeners() { @@ -39,9 +39,8 @@ export function setupHeaderButtons() { function setupAnnouncements() { const announcements = document.querySelectorAll('.announcement'); - const dismissed = getDismissed(); Array.from(announcements).forEach(announcement => { - if (announcement.id && dismissed.includes(announcement.id)) { + if (announcement.id && isDismissed(announcement.id)) { fadeOutElement(announcement); } else { announcement.querySelector('.close-button').addEventListener('click', () => dismiss(announcement)); diff --git a/src/php/router.php b/src/php/router.php index ec73357..cd191e6 100644 --- a/src/php/router.php +++ b/src/php/router.php @@ -75,7 +75,7 @@ switch ($view) { foreach ($announcements as $announcement) { $expire = strtotime($announcement['expire']); if (time() < $expire) { - $announcements_html .= '
+ $announcements_html .= '
×︎

' . $announcement['header'] . '

' . $announcement['body'] . '