Skip to content

Commit

Permalink
Merge pull request #21 from oasisprotocol/lw/frontend-only
Browse files Browse the repository at this point in the history
frontend: Add script to run frontend only
  • Loading branch information
lukaw3d authored Oct 30, 2024
2 parents 81f344d + f9b266d commit 8e641cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions faucet-frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ edit faucet-backend.toml
(cd ../faucet-backend && go1.17.6 build && ./faucet-backend)

open http://localhost:8080/


# run frontend only:
yarn dev-frontend-only
```


Expand Down
1 change: 1 addition & 0 deletions faucet-frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"scripts": {
"dev-frontend-only": "rm -rf .parcel-cache && parcel serve src/index.pug",
"dev": "rm -rf .parcel-cache && parcel watch src/index.pug",
"build": "rm -rf .parcel-cache && parcel build src/index.pug"
},
Expand Down
17 changes: 12 additions & 5 deletions faucet-frontend/src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// @ts-check

function showResponseStatus(status) {
function showResponseStatus(error, status, requestBody) {
document.querySelector('#request-form').style.display = 'none';
document.querySelector('#response-display').style.display = 'block';
document.querySelector('#response-display-text').textContent = status;
if (error) {
document.querySelector('#response-display-text').textContent = error;
} else {
document.querySelector('#response-display-text').textContent = status;
if (requestBody.get('paratime') === 'emerald') {}
if (requestBody.get('paratime') === 'sapphire') {}
}
}
function showLoading(bool) {
document.querySelector('#request-form-submit').disabled = bool;
Expand Down Expand Up @@ -31,21 +37,22 @@ document.querySelector('#request-form').addEventListener('submit', (event) => {
/** @type {HTMLFormElement} */
(event.currentTarget);
const url = form.action;
const requestBody = new URLSearchParams(new FormData(form))

fetch(url, {
method: 'POST',
body: new URLSearchParams(new FormData(form)),
body: requestBody,
headers: {
Accept: 'application/json',
},
})
.then(response => response.json())
.then((responseJson) => {
showLoading(false);
showResponseStatus(responseJson.result);
showResponseStatus(null, responseJson.result, requestBody);
}, (error) => {
showLoading(false);
showResponseStatus(error);
showResponseStatus(error, null, requestBody);
});

// Only prevent native form POST if no errors were thrown until `fetch`
Expand Down

0 comments on commit 8e641cb

Please sign in to comment.