-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathapp.js
152 lines (130 loc) · 4.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { SPACE_ID, ACCESS_TOKEN } from "./setup/credentials.js";
const endpoint = "https://graphql.contentful.com/content/v1/spaces/" + SPACE_ID;
const query = `{
microblogCollection {
items {
sys {
firstPublishedAt
id
}
text
image {
url
title
width
height
description
}
panther
link
linkText
}
}
}`;
const fetchOptions = {
method: "POST",
headers: {
Authorization: "Bearer " + ACCESS_TOKEN,
"Content-Type": "application/json",
},
body: JSON.stringify({ query }),
};
const getMonthStringFromInt = (int) => {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
return months[int];
};
const addLeadingZero = (num) => {
num = num.toString();
while (num.length < 2) num = "0" + num;
return num;
};
const renderFooterDate = () => {
const footerYearHolder = document.querySelector("[data-footer-year]");
const timestamp = Date.now();
const date = new Date(timestamp);
footerYearHolder.innerText = date.getFullYear();
};
const formatPublishedDateForDateTime = (dateString) => {
const timestamp = Date.parse(dateString);
const date = new Date(timestamp);
return `${date.getFullYear()}-${addLeadingZero(date.getMonth() + 1)}-${date.getDate()}`;
};
const formatPublishedDateForDisplay = (dateString) => {
const timestamp = Date.parse(dateString);
const date = new Date(timestamp);
return `${date.getDate()} ${getMonthStringFromInt(date.getMonth())} ${date.getFullYear()}`;
};
const microblogHolder = document.querySelector("[data-items]");
const itemClassNames = {
container: "item__container",
topRow: "item__topRow",
date: "item__date",
img: "item__img",
link: "item__link",
panther: "item__panther",
text: "item__text",
};
const renderItems = (items) => {
items.forEach((item) => {
const newItemEl = document.createElement("article");
newItemEl.setAttribute("id", item.sys.id);
newItemEl.className = itemClassNames.container;
const newTopRow = document.createElement("div");
newTopRow.className = itemClassNames.topRow;
const newPantherEl = document.createElement("img");
newPantherEl.src = `./panthers/${item.panther}.svg`;
newPantherEl.alt = `${item.panther} panther emote`;
newPantherEl.setAttribute("width", "50");
newPantherEl.setAttribute("height", "50");
newPantherEl.className = itemClassNames.panther;
newTopRow.appendChild(newPantherEl);
const newDateEl = document.createElement("time");
newDateEl.setAttribute("datetime", formatPublishedDateForDateTime(item.sys.firstPublishedAt));
newDateEl.innerText = formatPublishedDateForDisplay(item.sys.firstPublishedAt);
newDateEl.className = itemClassNames.date;
newTopRow.appendChild(newDateEl);
newItemEl.appendChild(newTopRow);
if (item.image) {
const newImgEl = document.createElement("img");
newImgEl.src = `${item.image.url}?w=500`;
newImgEl.alt = item.image.description;
newImgEl.setAttribute("width", item.image.width);
newImgEl.setAttribute("height", item.image.height);
newImgEl.className = itemClassNames.img;
newItemEl.appendChild(newImgEl);
}
if (item.text) {
const newTextEl = document.createElement("h2");
newTextEl.innerText = item.text;
newTextEl.className = itemClassNames.text;
newItemEl.appendChild(newTextEl);
}
if (item.link) {
const newLinkEl = document.createElement("a");
newLinkEl.href = item.link;
newLinkEl.innerText = item.linkText || "View more";
newLinkEl.setAttribute("target", "_blank");
newLinkEl.setAttribute("rel", "noopener noreferrer");
newLinkEl.className = itemClassNames.link;
newItemEl.appendChild(newLinkEl);
}
microblogHolder.appendChild(newItemEl);
});
};
renderFooterDate();
fetch(endpoint, fetchOptions)
.then((response) => response.json())
.then((data) => renderItems(data.data.microblogCollection.items));