-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
431 lines (396 loc) · 9.86 KB
/
app.js
File metadata and controls
431 lines (396 loc) · 9.86 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
* Selectors
*/
const app = document.getElementById("app");
/**
* State
*/
let state = {};
const initialState = {
page: 1,
loading: false,
selectedPhoto: false,
photos: []
};
/**
* Api
*/
const API_BASE = "https://api.unsplash.com/photos";
const params = {
page: initialState.page,
per_page: 24,
client_id: "ec4779e6804bf4e4c72ac5f5d70a81480712b24f2ecfe46494169d9b74ee9f83"
};
/**
* Listen for Document, to be ready and boot the app up!
*/
document.addEventListener("DOMContentLoaded", boot);
function boot() {
/**
* Set state, do first render
*/
setState(initialState);
render(state);
/**
* Fetch Data from API, render app again
*/
const endpoint = API_BASE + queryString(params);
fetchData(endpoint).then(response => {
const formattedResponse = formatResponse(response, "unsplash");
setState({ photos: formattedResponse });
render(state);
});
}
/**
* Utility functions
*/
/**
* [formatResponse Not much to see here, we could easily implement other providers with this pattern]
* @param {array} response [description]
* @param {string} provider [unsplash]
* @return {array} [description]
*/
function formatResponse(response, provider) {
if ("unsplash" === provider) {
return response.map(photo => ({
id: photo.id,
color: photo.color,
urls: photo.urls,
links: photo.links,
user: photo.user,
created_at: photo.created_at
}));
}
return response;
}
/**
* [queryString description]
* @return {[type]} [description]
*/
function queryString(params) {
let query = Object.keys(params)
.map(key => key + "=" + params[key])
.join("&");
return "?" + query;
}
/**
* [setState description]
* @param {object} nextState [description]
*/
function setState(nextState) {
state = Object.assign(state, nextState);
return state;
}
/**
* [toggleClass from http://youmightnotneedjquery.com/]
* @param {DOM} el []
* @return {sideeffect} []
*/
function toggleClass(el, className) {
if (el.classList) {
el.classList.toggle(className);
} else {
let classes = el.className.split(" ");
let existingIndex = classes.indexOf(className);
if (existingIndex >= 0) {
classes.splice(existingIndex, 1);
} else {
classes.push(className);
}
el.className = classes.join(" ");
}
}
/**
* [findById description]
* @param {any} id [In the case of unsplash we use strings, but it could be an int]
* @param {array} arr [description]
* @return {false|object} [description]
*/
function findById(id, arr) {
let match = false;
arr.map((item, idx) => {
if (id === item.id) {
match = {
idx,
item
};
}
});
return match;
}
/**
* Core functionality
*/
/**
* [fetchData Fetch data, and cache it in localStorage]
* @param {string} endpoint [https://api.image.com/featured]
* @return {promise} [description]
*/
function fetchData(endpoint) {
const cache = localStorage.getItem(endpoint);
return new Promise((resolve, reject) => {
const options = {
method: "GET"
};
if (cache) {
resolve(JSON.parse(cache));
} else {
fetch(endpoint, options)
.then(response => {
if (200 !== response.status) {
reject("Error");
}
return response.json();
})
.then(json => {
localStorage.setItem(endpoint, JSON.stringify(json));
resolve(json);
});
}
});
}
/**
* [changePhoto description]
* @param {[type]} state [description]
* @param {[type]} direction [description]
* @return {[type]} [description]
*/
function changePhoto(state, direction = "next") {
let { selectedPhoto, photos } = state;
let currentPhoto = findById(selectedPhoto.id, state.photos);
let nextIndex =
"next" === direction ? currentPhoto.idx + 1 : currentPhoto.idx - 1;
/**
* @note, This logic seems to work, w/o conditional check for direction.
* Might need a bit of debugging
*/
if (nextIndex >= photos.length) {
nextIndex = 0;
}
if (-1 === nextIndex) {
nextIndex = photos.length - 1;
}
setState({ selectedPhoto: state.photos[nextIndex] });
}
/**
* [addEventListeners Once render completes, we will want to hook up event listeners]
*/
function addEventListeners() {
const images = document.querySelectorAll(".image");
if (images.length) {
for (var i = 0; i < images.length; i++) {
images[i].addEventListener("click", e => {
toggleClass(document.body, "no-scroll");
setState({ selectedPhoto: findById(e.target.id, state.photos).item });
render(state);
});
}
}
const overlay = document.getElementById("overlay");
const closeBtn = document.getElementById("close");
const nextBtn = document.getElementById("next");
const prevBtn = document.getElementById("prev");
const loadBtn = document.getElementById("load");
if (overlay) {
overlay.addEventListener("click", close);
}
if (closeBtn) {
closeBtn.addEventListener("click", close);
}
if (nextBtn) {
nextBtn.addEventListener("click", next);
}
if (prevBtn) {
prevBtn.addEventListener("click", prev);
}
if (loadBtn) {
loadBtn.addEventListener("click", load);
}
}
/**
* [next Change to the next photo]
* @param {[type]} e [description]
* @return {[type]} [description]
*/
function close(e) {
e.preventDefault();
toggleClass(document.body, "no-scroll");
setState({ selectedPhoto: false });
render(state);
}
/**
* [next Change to the next photo]
* @param {[type]} e [description]
* @return {[type]} [description]
*/
function next(e) {
e.preventDefault();
changePhoto(state);
render(state);
}
/**
* [prev, Previous Photo]
* @param {[type]} e [description]
* @return {[type]} [description]
*/
function prev(e) {
e.preventDefault();
changePhoto(state, "prev");
render(state);
}
/**
* [load Load more data from unsplash]
* @param {[type]} e [description]
* @return {[type]} [description]
*/
function load(e) {
e.preventDefault();
setState({ loading: true, page: state.page + 1 });
params.page = state.page;
let endpoint = API_BASE + queryString(params);
fetchData(endpoint).then(response => {
let formattedResponse = formatResponse(response, "unsplash");
setState({
loading: false,
photos: [...state.photos, ...formattedResponse]
});
render(state);
});
}
/**
* Render Methods
*/
/**
* [renderModalControls description]
* @param {[type]} state [description]
* @return {[type]} [description]
*/
function renderModalControls(state) {
return `
<div class="modal__controls mb2 right-align">
<a id="close" class="btn btn-outline rounded black mr2 left" href="#" style="font-size: 24px">×</a>
<a id="prev" class="btn btn-outline rounded black" href="#">Prev</a>
<a id="next" class="btn btn-outline rounded black " href="#">Next</a>
</div>
`;
}
/**
* [renderModalPhoto description]
* @param {[type]} state [description]
* @return {[type]} [description]
*/
function renderModalPhoto(state) {
const { selectedPhoto } = state;
if (!selectedPhoto) {
return "";
}
const photoDate = new Date(selectedPhoto.created_at);
return `
<div>
<div class="md-col md-col-9 center">
<a class="center" href="${selectedPhoto.links.download}">
<img
id="${selectedPhoto.id}"
class="image--modal"
src="${selectedPhoto.urls.regular}"
alt="Photo"
/>
</a>
</div>
<div class="md-col md-col-3">
<div class="border p2 rounded bg-light-gray">
<p class="m0">
<a href="${selectedPhoto.user.links.html}">${selectedPhoto.user.name}</a>
</p>
<p class="m0 h6 gray">${photoDate.toLocaleDateString()}, ${photoDate.toLocaleTimeString()}</p>
<input class="field col-12 mt1" type="text" value="${
selectedPhoto.links.html
}" />
</div>
</div>
</div>
`;
}
/**
* [renderModal renders the modal component]
* @param {object} state [description]
* @return {string} [description]
*/
function renderModal(state) {
const { selectedPhoto } = state;
return `
<div class="modal ${selectedPhoto ? "modal--active" : ""}">
<div id="overlay" class="modal__overlay"></div>
<div class="modal__inner p2 bg-white rounded">
${renderModalControls(state)}
${renderModalPhoto(state)}
</div>
</div>
`;
}
/**
* [renderPhotos description]
* @param {object} state [description]
* @return {string} [description]
*/
function renderPhotos(state) {
if (state.photos.length === 0) {
return '<h1 class="center py4">Loading...</h1>';
}
let classNames = "image image-col col col-6 md-col md-col-3";
let photos = state.photos.map(
photo => `
<div
id="${photo.id}"
class="${classNames}"
tabindex="0"
style="
background-image:url(${photo.urls.small});
background-color: ${photo.color}
"></div>
`
);
return `<div class="clearfix">${photos.join("")}</div>`;
}
function renderLoader(state) {
let btn = `<a id="load" href="#" class="btn btn-primary col-12 bg-black center">Load More</a>`;
if (state.photos.length === 0) {
return "";
}
return `
<div class="container clearfix p2">
${btn}
</div>
`;
}
/**
* [renderHeader description]
* @param {object} state [description]
* @return {string} [description]
*/
function renderHeader(state) {
return `
<div class="py3 px2">
<ul class="list-reset">
<li class="blue inline-block mr2">unsplash</li>
</ul>
<h1 class="black line-height-1 m0">
recent photos
</h1>
</div>
`;
}
/**
* [render Main render function, similar to react]
* @param {object} state [the application state]
* @return {side-effect} [Could return a string but for simplicity just append to app]
*/
function render(state) {
app.innerHTML = [
renderHeader(state),
renderModal(state),
renderPhotos(state),
renderLoader(state)
].join("");
addEventListeners();
}