update client pages

- Fixed minor bugs
- Added event calls for sell and buy
- sell and buy events to check for valid inputs
This commit is contained in:
sairajzero 2021-09-02 04:56:10 +05:30
parent 0cc525b28e
commit 0d84962b8a
2 changed files with 48 additions and 21 deletions

View File

@ -125,6 +125,10 @@ function logout() {
function buy(quantity, max_price) {
return new Promise((resolve, reject) => {
if (typeof quantity !== "number" || quantity <= 0)
return reject(INVALID(`Invalid quantity (${quantity})`));
else if (typeof max_price !== "number" || max_price <= 0)
return reject(INVALID(`Invalid max_price (${max_price})`));
fetch('/buy', {
method: "POST",
headers: {
@ -144,6 +148,10 @@ function buy(quantity, max_price) {
function sell(quantity, min_price) {
return new Promise((resolve, reject) => {
if (typeof quantity !== "number" || quantity <= 0)
return reject(INVALID(`Invalid quantity (${quantity})`));
else if (typeof min_price !== "number" || min_price <= 0)
return reject(INVALID(`Invalid min_price (${min_price})`));
fetch('/sell', {
method: "POST",
headers: {

View File

@ -17,7 +17,7 @@
<legend>Login</legend>
<input type="password" name="priv-key" placeholder="Enter Private Key" />
<input type="text" name="sid" style="display: none;" />
<input type="button" name="login" value="login" /><br/>
<input type="button" name="login" value="login" onclick="UI_evt.login();" /><br />
<input type="checkbox" name="remember-me" checked />RememberMe
</fieldset>
</form>
@ -27,22 +27,22 @@
<span id="user_id"></span><br />
FLO: <span id="flo_bal"></span><br />
Rupee: <span id="rupee_bal"></span><br />
<button id="logout">logout</button>
<button onclick="UI_evt.logout();">logout</button>
</fieldset>
<form id="buy">
<form id="buy-form">
<fieldset>
<legend>Buy</legend>
<input type="number" name="quantity" placeholder="Enter Quantity" />
<input type="number" name="max-price" placeholder="Enter Max Price" />
<input type="button" name="buy" value="buy" />
<input type="button" name="buy" value="buy" onclick="UI_evt.buy();" />
</fieldset>
</form>
<form id="sell">
<form id="sell-form">
<fieldset>
<legend>Sell</legend>
<input type="number" name="quantity" placeholder="Enter Quantity" />
<input type="number" name="min-price" placeholder="Enter Min Price" />
<input type="button" name="sell" value="sell" />
<input type="button" name="sell" value="sell" onclick="UI_evt.sell();" />
</fieldset>
</form>
</div>
@ -153,16 +153,19 @@
function account() {
getAccount().then(acc => {
console.debug(acc);
document.getElementById("login-form").style.display = "none";
document.getElementById('user-container').style.display = "block";
document.getElementById("user_id").textContent = acc.floID;
let flo_total = acc.coins.reduce((a, x) => x.quantity, 0);
let flo_locked = acc.sellOrders.reduce((a, x) => x.quantity);
let flo_total = acc.coins.reduce((a, x) => a + x.quantity, 0);
let flo_locked = acc.sellOrders.reduce((a, x) => a + x.quantity, 0);
let flo_net = flo_total - flo_locked;
console.debug("FLO", flo_total, flo_locked, flo_net);
document.getElementById("flo_bal").textContent = flo_net + "(+" + flo_locked + ")";
let rupee_total = acc.rupee_total;
let rupee_locked = acc.buyOrders.reduce((a, x) => x.quantity * x.maxPrice, 0);
let rupee_locked = acc.buyOrders.reduce((a, x) => a + x.quantity * x.maxPrice, 0);
let rupee_net = rupee_total - rupee_locked;
console.debug("RUPEE", rupee_total, rupee_locked, rupee_net);
document.getElementById("rupee_bal").textContent = rupee_net + "(+" + rupee_locked + ")";
}).catch(error => {
if (error instanceof ResponseError) {
@ -177,14 +180,16 @@
})
}
document.getElementById("logout").onclick = (evt) => {
const UI_evt = {};
UI_evt.logout = function() {
logout().then(result => {
console.warn(result);
location.reload();
}).catch(error => console.error(error));
};
document.forms['login-form']['login'].onclick = evt => {
UI_evt.login = function() {
let formInputs = document.forms['login-form'];
let privKey = formInputs['priv-key'].value;
let sid = formInputs['sid'].value;
@ -193,7 +198,21 @@
console.log(result);
account();
}).catch(error => console.error(error));
}
};
UI_evt.sell = function() {
let formInputs = document.forms['sell-form'];
sell(parseFloat(formInputs["quantity"].value), parseFloat(formInputs["min-price"].value))
.then(result => console.log(result))
.catch(error => console.error(error));
};
UI_evt.buy = function() {
let formInputs = document.forms['buy-form'];
buy(parseFloat(formInputs["quantity"].value), parseFloat(formInputs["max-price"].value))
.then(result => console.log(result))
.catch(error => console.error(error));
};
(function init() {
account();