Skip to content

Commit 73fe510

Browse files
committed
Refactor getArticle to fetchAndPopulateFormFields
- makes a bit more sense split out
1 parent 74bdfce commit 73fe510

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

quick-weblog.js

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
function getArticle(url, api_key) {
2-
3-
if (url === "") {
4-
updateStatus("You must fill out <strong>Post URL</strong> to use Auto Fill.");
5-
return;
6-
} else {
7-
updateStatus("Attempting to fetch information and Auto Fill...");
8-
}
9-
10-
disableForm();
11-
1+
function fetchArticleData(url, api_key) {
122
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=';
13-
143
const options = {
154
method: 'GET',
165
headers: {
@@ -19,37 +8,55 @@ function getArticle(url, api_key) {
198
}
209
};
2110

22-
fetch(base_url + url, options)
23-
.then(response => {
24-
if (response.status !== 200) {
25-
throw new Error(`Request failed with status: ${response.status}`);
26-
}
27-
return response.json();
28-
})
29-
.then(response => {
30-
console.log(response);
11+
return fetch(base_url + url, options)
12+
.then(response => {
13+
if (response.status !== 200) {
14+
throw new Error(`Request failed with status: ${response.status}`);
15+
}
16+
return response.json();
17+
})
18+
.then(response => {
19+
if (response.error > 0) {
20+
throw new Error(`Request received error: ${response.message}`);
21+
}
22+
return response.data;
23+
})
24+
.catch(err => {
25+
throw new Error(`Error fetching article data: ${err.message}`);
26+
});
27+
}
3128

32-
if (response.error > 0) {
33-
throw new Error(`Request received error: ${response.message}`);
34-
}
29+
function updateFormFields(data) {
30+
document.getElementById("quick-weblog-title").value = data?.title;
31+
document.getElementById("quick-weblog-image_url").value = data?.image;
32+
document.getElementById("quick-weblog-image_description").value = "image via " + data?.source;
33+
document.getElementById("quick-weblog-quote").value = data?.description;
34+
}
35+
36+
function fetchAndPopulateFormFields(articleUrl, rapidApiKey) {
37+
if (articleUrl === "") {
38+
updateStatus("You must fill out <strong>Post URL</strong> to use Auto Fill.");
39+
return;
40+
}
3541

36-
document.getElementById("quick-weblog-title").value = response.data?.title;
37-
document.getElementById("quick-weblog-image_url").value = response.data?.image;
38-
document.getElementById("quick-weblog-image_description").value = "image via " + response.data?.source;
39-
document.getElementById("quick-weblog-quote").value = response.data?.description;
42+
updateStatus("Attempting to fetch information and Auto Fill...");
43+
disableForm();
4044

41-
updateStatus("Successfully used Auto Fill to populate form!");
42-
enableForm();
43-
})
44-
.catch(err => {
45-
updateStatus(err);
46-
console.error(err);
47-
enableForm();
48-
});
45+
fetchArticleData(articleUrl, rapidApiKey)
46+
.then(data => {
47+
updateFormFields(data);
48+
updateStatus("Successfully used Auto Fill to populate form!");
49+
enableForm();
50+
})
51+
.catch(err => {
52+
updateStatus(err);
53+
console.error(err);
54+
enableForm();
55+
});
4956
}
5057

5158
function toggleFormElements(enable) {
52-
const form= document.getElementById("quick-weblog");
59+
const form = document.getElementById("quick-weblog");
5360
const formElements = form.querySelectorAll("input, select, textarea");
5461

5562
formElements.forEach(element => {

0 commit comments

Comments
 (0)