Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: Add script to run frontend only #21

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading