Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,828 changes: 1,565 additions & 3,263 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@11ty/eleventy": "^1.0.0"
},
"devDependencies": {
"@11ty/eleventy": "^2.0.0",
"npm-run-all": "^4.1.5",
"rimraf": "^2.7.1",
"sass": "^1.43.2"
Expand Down
4 changes: 2 additions & 2 deletions src/_includes/layouts/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
{% include "components/head.html" %}

<body class="{{ pageClass }}">
{% include "components/nav.html" %}
{% include "components/nav.html" %} {% include "components/header.html" %}

<main class="content" tabindex="-1">
<h1>{{ pageTitle }}</h1>

{{ content }}
</main>

<script src="/js/scripts.js"></script>
<script src="/js/scripts.js" type="module"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions src/css/styles.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/css/styles.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/js/modules/localStorageHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export function setWithExpiry(key, value, ttl) {
const now = new Date();
// `item` is an object which contains the original value
// as well as the time when it's supposed to expire
const item = {
value: value,
expiry: now.getTime() + ttl,
};
localStorage.setItem(key, JSON.stringify(item));
}

export function getWithExpiry(key) {
const itemStr = localStorage.getItem(key);
// if the item doesn't exist, return null
if (!itemStr) {
console.log("no item string");
return null;
}
// console.log("item string found!", itemStr);
const item = JSON.parse(itemStr);
const now = new Date();
// compare the expiry time of the item with the current time
if (now.getTime() > item.expiry) {
// If the item is expired, delete the item from storage
// and return null
localStorage.removeItem(key);
return null;
}
return item.value;
}
20 changes: 15 additions & 5 deletions src/js/scripts.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
// store the link plus the API key in a variable
import { setWithExpiry, getWithExpiry } from "./modules/localStorageHelpers.js";

const key = "pb3WbIQux4hi9ZGGD38jXNA1K5gjBt6j";
const API = `https://api.nytimes.com/svc/topstories/v2/nyregion.json?api-key=${key}`;
const storagePrefix = "nyt-autosave";

function getStories() {
fetch(API)
.then((response) => response.json())
.then((data) => showData(data.results));
const stories = getWithExpiry(storagePrefix);
if (!stories) {
console.warn(" stories expired - fetching again ");
fetch(API)
.then((response) => response.json())
.then((data) => showData(data.results));
} else {
console.warn(" stories not expired - no fetching ");
document.querySelector(".stories").innerHTML = stories;
}
}

function showData(stories) {
var looped = stories
const looped = stories
.map(
(story) => `
<div class="item">
Expand All @@ -27,6 +36,7 @@ function showData(stories) {
.join("");

document.querySelector(".stories").innerHTML = looped;
setWithExpiry(storagePrefix, looped, 1000 * 60);
}

if (document.querySelector(".home")) {
Expand Down
29 changes: 29 additions & 0 deletions src/scss/imports/_header.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
header {
max-width: 980px;
margin: 0 auto;
padding-top: 2rem;
h1 {
font-size: 2.5rem;
}

p {
display: none;
@media (min-width: 780px) {
display: block;
font-size: 1.5rem;
text-transform: uppercase;
line-height: 1.1;
margin-bottom: 1rem;
}
}

h1 + p {
padding-top: 1rem;
border-top: 3px double #dbd1b5;
}
p + p {
font-size: 1rem;
line-height: 1.1;
color: #999;
}
}
1 change: 1 addition & 0 deletions src/scss/styles.scss
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
@import "imports/base";
@import "imports/header";