-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (112 loc) · 4.06 KB
/
script.js
File metadata and controls
134 lines (112 loc) · 4.06 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
// Defining a baseURL and key to as part of the request URL
const baseURL = 'https://api.nytimes.com/svc/search/v2/articlesearch.json';
const key = 'fFtQQS5qQ0Vzx2gyCE9EryIzi6exRNqK'; // API key
// Grab references to all the DOM elements you'll need to manipulate
const searchTerm = document.querySelector('.search');
const startDate = document.querySelector('.start-date');
const endDate = document.querySelector('.end-date');
const searchForm = document.querySelector('form');
const nextBtn = document.querySelector('.next');
const previousBtn = document.querySelector('.prev');
const section = document.querySelector('section');
const nav = document.querySelector('nav');
const saveButton = document.querySelector('.save');
const greeting = document.querySelector('.name-greeting');
const para = document.querySelector('#greet');
const nameInput = document.querySelector('#name');
// Hide the "Previous"/"Next" navigation to begin with, as we don't need it immediately
nav.style.display = 'none';
// define the initial page number and status of the navigation being displayed
let pageNumber = 0;
// Event listeners to control the functionality
searchForm.addEventListener('submit', e => {
pageNumber = 0;
fetchResults(e);
});
nextBtn.addEventListener('click', nextPage);
previousBtn.addEventListener('click', previousPage);
saveButton.addEventListener('click', () => {
para.textContent = `Welcome back, ${nameInput.value}`
greeting.appendChild(para);
})
function fetchResults(e) {
e.preventDefault(); // explain: why we need preventDefault in this case?
// because the default action when submitting a form is sending request and (maybe)
// refresh the data, so we usually call preventDefault() to do smt with the data before
// it is sent -> mandatory action
// Assemble the full URL
let url = `${baseURL}?api-key=${key}&page=${pageNumber}&q=${searchTerm.value}&fq=document_type:("article")`;
if (startDate.value !== '') {
url += `&begin_date=${startDate.value}`;
};
if (endDate.value !== '') {
url += `&end_date=${endDate.value}`;
};
// Use fetch() to make the request to the API
fetch(url)
.then(response => response.json())
.then(json => displayResults(json))
.catch(error => console.error(`Error fetching data: ${error.message}`));
}
function displayResults(json) {
// delete all content of section area
while (section.firstChild) {
section.removeChild(section.firstChild);
}
const articles = json.response.docs;
if (articles.length === 10) {
nav.style.display = 'block';
}
else {
nav.style.display = 'none';
}
if (articles.length === 0) {
const para = document.createElement('p');
para.textContent = 'No results returned.'
section.appendChild(para);
}
else {
for (const current of articles) {
const article = document.createElement('article');
const heading = document.createElement('h2');
const link = document.createElement('a');
const img = document.createElement('img');
const para1 = document.createElement('p');
const keywordPara = document.createElement('p');
keywordPara.classList.add('keywords');
console.log(current);
link.href = current.web_url;
link.textContent = current.headline.main;
para1.textContent = current.snippet;
keywordPara.textContent = 'Keywords: ';
for (const keyword of current.keywords) {
const span = document.createElement('span');
span.textContent = `${keyword.value} `;
keywordPara.appendChild(span);
}
if (current.multimedia.length > 0) {
img.src = `http://www.nytimes.com/${current.multimedia[0].url}`;
img.alt = current.headline.main;
}
article.appendChild(heading);
heading.appendChild(link);
article.appendChild(img);
article.appendChild(para1);
article.appendChild(keywordPara);
section.appendChild(article);
}
}
};
function nextPage(e) {
pageNumber++;
fetchResults(e);
};
function previousPage(e) {
if (pageNumber > 0) {
pageNumber--;
}
else {
return;
}
fetchResults(e);
};