Implemented 'custom forms' to replace individual scripts for deposit and supply forms
This commit is contained in:
parent
5ab0d1088f
commit
2bab323b86
4 changed files with 15 additions and 54 deletions
|
@ -1,15 +1,15 @@
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
// elements
|
// elements
|
||||||
let depositForm = document.getElementById("depositform");
|
let customForm = document.getElementById("customform");
|
||||||
let statusInfo = document.getElementById("statusinfo");
|
let statusInfo = document.getElementById("statusinfo");
|
||||||
let depositSubmitButton = document.getElementById("depositsubmitbtn");
|
let submitButton = document.getElementById("submitbtn");
|
||||||
// event listener for deposit form
|
// event listener for deposit form
|
||||||
// this implements a custom submit method
|
// this implements a custom submit method
|
||||||
depositForm.addEventListener("submit", (event) => {
|
customForm.addEventListener("submit", (event) => {
|
||||||
depositSubmitButton.disabled = true;
|
submitButton.disabled = true;
|
||||||
event.preventDefault(); // Don't do the default submit action!
|
event.preventDefault(); // Don't do the default submit action!
|
||||||
let xhr = new XMLHttpRequest();
|
let xhr = new XMLHttpRequest();
|
||||||
let formData = new FormData(depositForm);
|
let formData = new FormData(customForm);
|
||||||
xhr.addEventListener("load", (event) => {
|
xhr.addEventListener("load", (event) => {
|
||||||
status_ = event.target.status;
|
status_ = event.target.status;
|
||||||
response_ = event.target.responseText;
|
response_ = event.target.responseText;
|
||||||
|
@ -28,7 +28,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
||||||
statusInfo.innerText = "An error occured. Redirecting in 5 seconds...";
|
statusInfo.innerText = "An error occured. Redirecting in 5 seconds...";
|
||||||
window.setTimeout(() => { window.location.replace("/") }, 5000);
|
window.setTimeout(() => { window.location.replace("/") }, 5000);
|
||||||
})
|
})
|
||||||
xhr.open("POST", "/api/deposit");
|
xhr.open("POST", customForm.action);
|
||||||
xhr.send(formData);
|
xhr.send(formData);
|
||||||
});
|
});
|
||||||
});
|
});
|
|
@ -1,39 +0,0 @@
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
|
||||||
// elements
|
|
||||||
let supplyDescriptionElement = document.getElementById("supplydescription");
|
|
||||||
let supplyPriceElement = document.getElementById("supplyprice");
|
|
||||||
let supplyFormElement = document.getElementById("supplyform");
|
|
||||||
let statusInfoElement = document.getElementById("statusinfo");
|
|
||||||
let supplySubmitButton = document.getElementById("supplysubmitbtn");
|
|
||||||
// custom submit method
|
|
||||||
supplyFormElement.addEventListener("submit", (event) => {
|
|
||||||
supplySubmitButton.disabled = true;
|
|
||||||
event.preventDefault(); // Don't do the default submit action!
|
|
||||||
if (isNaN(parseFloat(supplyPriceElement.value)) || supplyDescriptionElement.value == "") {
|
|
||||||
statusInfoElement.innerText = "Please enter a description and price."
|
|
||||||
supplySubmitButton.disabled = false;
|
|
||||||
}
|
|
||||||
let xhr = new XMLHttpRequest();
|
|
||||||
let formData = new FormData(supplyFormElement);
|
|
||||||
xhr.addEventListener("load", (event) => {
|
|
||||||
status_ = event.target.status;
|
|
||||||
response_ = event.target.responseText;
|
|
||||||
if (status_ == 200 && response_ == "success") {
|
|
||||||
statusInfoElement.innerText = "Success.";
|
|
||||||
window.location.replace("/");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
statusInfoElement.classList.add("errortext");
|
|
||||||
statusInfoElement.innerText = "An error occured.";
|
|
||||||
window.setTimeout(() => { window.location.reload() }, 5000);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
xhr.addEventListener("error", (event) => {
|
|
||||||
statusInfoElement.classList.add("errortext");
|
|
||||||
statusInfoElement.innerText = "An error occured.";
|
|
||||||
window.setTimeout(() => { window.location.reload() }, 5000);
|
|
||||||
})
|
|
||||||
xhr.open("POST", "/api/supply");
|
|
||||||
xhr.send(formData);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -12,13 +12,13 @@
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<form id="depositform" class="flex flex-column flex-center appform gap-1rem">
|
<form id="customform" class="flex flex-column flex-center appform gap-1rem" action="/api/deposit">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<h1 class="formheading">{% translate "Deposit" %}</h1>
|
<h1 class="formheading">{% translate "Deposit" %}</h1>
|
||||||
<div class="flex forminput">
|
<div class="flex forminput">
|
||||||
<span>{% translate "Amount" %} {{ currency_suffix }}:</span>
|
<span>{% translate "Amount" %} {{ currency_suffix }}:</span>
|
||||||
<span>
|
<span>
|
||||||
<input type="number" name="depositamount" id="depositamount" class="keyboard-input" max="9999.99" min="1.00" step="0.01" autofocus>
|
<input type="number" name="depositamount" class="keyboard-input" max="9999.99" min="1.00" step="0.01" autofocus required>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div id="statusinfo"></div>
|
<div id="statusinfo"></div>
|
||||||
|
@ -28,9 +28,9 @@
|
||||||
<script src="/static/js/simple-keyboard_configure.js"></script>
|
<script src="/static/js/simple-keyboard_configure.js"></script>
|
||||||
<div class="flex-center buttons">
|
<div class="flex-center buttons">
|
||||||
<a href="/" class="button">{% translate "cancel" %}</a>
|
<a href="/" class="button">{% translate "cancel" %}</a>
|
||||||
<input type="submit" id="depositsubmitbtn" class="button" value='{% translate "confirm" %}'>
|
<input type="submit" id="submitbtn" class="button" value='{% translate "confirm" %}'>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<script src="/static/js/deposit.js"></script>
|
<script src="/static/js/custom_form.js"></script>
|
||||||
<script src="/static/js/autoreload.js"></script>
|
<script src="/static/js/autoreload.js"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -9,28 +9,28 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
{% if user.is_superuser or user.allowed_to_supply %}
|
{% if user.is_superuser or user.allowed_to_supply %}
|
||||||
<form id="supplyform" class="flex flex-column flex-center appform gap-1rem">
|
<form id="customform" class="flex flex-column flex-center appform gap-1rem" action="/api/supply">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<h1 class="formheading">{% translate "Supply" %}</h1>
|
<h1 class="formheading">{% translate "Supply" %}</h1>
|
||||||
<div class="flex forminput">
|
<div class="flex forminput">
|
||||||
<span>{% translate "Description" %}:</span>
|
<span>{% translate "Description" %}:</span>
|
||||||
<span>
|
<span>
|
||||||
<input type="text" name="supplydescription" id="supplydescription" autofocus>
|
<input type="text" name="supplydescription" autofocus required>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex forminput">
|
<div class="flex forminput">
|
||||||
<span>{% translate "Price" %} ({{ currency_suffix }}):</span>
|
<span>{% translate "Price" %} ({{ currency_suffix }}):</span>
|
||||||
<span>
|
<span>
|
||||||
<input type="number" name="supplyprice" id="supplyprice" max="9999.99" min="1.00" step="0.01">
|
<input type="number" name="supplyprice" max="9999.99" min="1.00" step="0.01" required>
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div id="statusinfo"></div>
|
<div id="statusinfo"></div>
|
||||||
<div class="buttons">
|
<div class="buttons">
|
||||||
<a href="/" class="button">{% translate "cancel" %}</a>
|
<a href="/" class="button">{% translate "cancel" %}</a>
|
||||||
<input type="submit" id="supplysubmitbtn" class="button" value='{% translate "submit" %}'>
|
<input type="submit" id="submitbtn" class="button" value='{% translate "submit" %}'>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<script src="/static/js/supply.js"></script>
|
<script src="/static/js/custom_form.js"></script>
|
||||||
<script src="/static/js/custom_number_input.js"></script>
|
<script src="/static/js/custom_number_input.js"></script>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="flex flex-center">
|
<div class="flex flex-center">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue