Skip to content

Commit

Permalink
add some expiry time on those localstorage - nobody likes stale local…
Browse files Browse the repository at this point in the history
…storage
  • Loading branch information
mraguso2 committed Nov 13, 2020
1 parent 3bc9d43 commit 9abefb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion components/Month.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const Month = ({ month, date }) => {

function handleDayClick(e) {
const { day } = e.target.dataset;
if (day === '00') return;
console.log(day);
if (day === '00' || !day) return;
const theYear = selectedMonth.slice(3);
const theMonth = selectedMonth.slice(0, 2);

Expand Down
16 changes: 12 additions & 4 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import allTideData from './Data/tidesDataCombined.json';

export function getMonthOfTides(month) {
const tideInfo = allTideData.data[`mm_${month}`];
// const now = new Date();
localStorage.setItem(month, JSON.stringify(tideInfo));
return tideInfo;
const expiry = Date.now() + 864000000;
const tidesWithExpiry = [...tideInfo, { expiry }];
localStorage.setItem(month, JSON.stringify(tidesWithExpiry));
return tidesWithExpiry;
}

export function buildDay(datetime = Date.now(), offset = 0) {
Expand Down Expand Up @@ -33,8 +34,15 @@ function tidesForTheMonth(monthValue) {
monthTides = getMonthOfTides(monthValue);
} else {
monthTides = JSON.parse(window.localStorage.getItem(monthValue));

// check if month is stale
const [{ expiry = Date.now() }] = monthTides.filter(item => item.expiry);
if (Date.now() > expiry) {
monthTides = getMonthOfTides(monthValue);
}
}
return monthTides;
const tidesWithOutExpiry = monthTides.filter(item => !item.expiry);
return tidesWithOutExpiry;
}

function comboTides(curr = {}, next = {}) {
Expand Down

0 comments on commit 9abefb3

Please sign in to comment.