Skip to content

Commit ab0ae17

Browse files
committed
Status Updates
- inform user as to what is happenining with auto fill form fetching
1 parent 82fdd07 commit ab0ae17

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

quick-weblog.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function getArticle(url, api_key) {
22

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+
310
disableForm();
411

512
const base_url = 'https://article-extractor2.p.rapidapi.com/article/parse?url=';
@@ -15,21 +22,27 @@ function getArticle(url, api_key) {
1522
fetch(base_url + url, options)
1623
.then(response => {
1724
if (response.status !== 200) {
18-
throw new Error(`Request failed with status ${response.status}`);
25+
throw new Error(`Request failed with status: ${response.status}`);
1926
}
2027
return response.json();
2128
})
2229
.then(response => {
30+
if (response.error >= 0) {
31+
throw new Error(`Request received error: ${response.message}`);
32+
}
33+
2334
console.log(response);
2435

2536
document.getElementById("quick-weblog-title").value = response.data?.title;
2637
document.getElementById("quick-weblog-image_url").value = "Image via " + response.data?.souce;
2738
document.getElementById("quick-weblog-image_description").value = response.data?.title;
2839
document.getElementById("quick-weblog-quote").value = response.data?.description;
2940

41+
updateStatus("Successfully used Auto Fill to populate form!");
3042
enableForm();
3143
})
3244
.catch(err => {
45+
updateStatus(err);
3346
console.error(err);
3447
enableForm();
3548
});
@@ -51,3 +64,7 @@ function enableForm() {
5164
function disableForm() {
5265
toggleFormElements(false);
5366
}
67+
68+
function updateStatus(status) {
69+
document.getElementById("quick-weblog-status").innerHTML = status;
70+
}

quick-weblog.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,25 @@ function quick_weblog_form() {
5252

5353
<script>
5454
window.addEventListener("DOMContentLoaded", (event) => {
55-
document.getElementById("quick-test").addEventListener("click", (click_event) => {
56-
getArticle(document.getElementById("quick-test-input").value, "<?php echo esc_js( wp_kses( $api_key, array() ) ); ?>" );
55+
document.getElementById("quick-weblog-auto").addEventListener("click", (click_event) => {
56+
click_event.preventDefault();
57+
getArticle(document.getElementById("quick-weblog-url").value, "<?php echo esc_js( wp_kses( $api_key, array() ) ); ?>" );
5758
});
5859
});
5960
</script>
6061

6162
<p id="quick-weblog-description">Quickly create a simple Post that highlights an existing news article. Posts include a captioned image and quote with URL citation of original article. All fields are required.</p>
6263

63-
<button id="quick-test">Quick Test</button>
64-
6564
<div class="card">
6665
<form id="quick-weblog" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
6766
<div>
6867
<label for="url"><?php _e( 'Post URL', 'quick-weblog' ); ?></label>
69-
<input id="quick-test-input" type="text" name="url" id="quick-weblog-url" required>
68+
<input type="text" name="url" id="quick-weblog-url" required>
69+
</div>
70+
71+
<div>
72+
<button id="quick-weblog-auto">Auto Fill</button>
73+
<span id="quick-weblog-status">Attempt to auto fill form based on <strong>Post URL</strong>.</span>
7074
</div>
7175

7276
<div>

0 commit comments

Comments
 (0)