From a58a192387e549ec3392f10b84c584fbe9cd411d Mon Sep 17 00:00:00 2001 From: counterweight Date: Mon, 24 Feb 2025 11:20:24 +0100 Subject: [PATCH] sat field --- src/public/css/offers.css | 17 +++++----- src/public/javascript/offers.js | 57 +++++++++++++++++++++++++-------- src/views/offers.ejs | 21 +++++++++--- 3 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/public/css/offers.css b/src/public/css/offers.css index b0e9be0..087f59f 100644 --- a/src/public/css/offers.css +++ b/src/public/css/offers.css @@ -1,25 +1,24 @@ -#eur-amount { +.money-amount-input-area { padding: 0; - width: 50%; + width: 10em; } -#eur-amount > * { +.money-amount-input-area > * { display: inline-block; font-size: 1em; height: 2em; vertical-align: middle; } -#input-eur-amount { - width:75%; +.input-money-amount { + width: 75%; } - -#eur-amount #eur-symbol { +.money-amount-input-area .curr-symbol { background-color: #d8d8d8; - width: 25%; + width: 12; } -#eur-amount #eur-symbol #eur-character { +.money-amount-input-area .curr-symbol .curr-character { display: flex; justify-content: center; align-items: center; diff --git a/src/public/javascript/offers.js b/src/public/javascript/offers.js index db01c0a..1b686a5 100644 --- a/src/public/javascript/offers.js +++ b/src/public/javascript/offers.js @@ -19,6 +19,7 @@ const buttonDecreasePremium = document.getElementById( ); const eurAmountInput = document.getElementById('input-eur-amount'); +const btcAmountInput = document.getElementById('input-btc-amount'); function toggleCreateOfferControls() { createOfferControls.style.display = @@ -35,14 +36,6 @@ function modifyPremiumValue(delta) { premiumValue.innerText = newValue; } -buttonStartCreateOffer.addEventListener('click', () => { - toggleCreateOfferControls(); -}); - -closeOfferControls.addEventListener('click', () => { - toggleCreateOfferControls(); -}); - function toggleBuyOrSellButtonGroup() { buyOrSellButtons.forEach((button) => { if (button.classList.contains('selected')) { @@ -53,16 +46,26 @@ function toggleBuyOrSellButtonGroup() { }); } -function validateAndFormatEurAmountInput() { +function readIntFromEurAmountInput() { const eurAmountFieldValue = eurAmountInput.value; - const regularExpression = /€?\s?(\d+[.,]?\d*)\s?€?/; + const regularExpression = /([\d\s]+)/; const matchResult = eurAmountFieldValue.match(regularExpression); + if (!matchResult) { + return null; + } + + const numberString = matchResult[1]; + const cleanInputNumber = parseInt(numberString.replace(/\s/gi, '')); + + return cleanInputNumber; +} + +function validateAndFormatEurAmountInput() { + const cleanInputNumber = readIntFromEurAmountInput(); eurAmountInput.classList.remove('input-is-valid', 'input-is-invalid'); - if (matchResult) { - const numberString = matchResult[1]; - const cleanInputNumber = parseInt(numberString); - eurAmountInput.value = `${cleanInputNumber} €`; + if (cleanInputNumber) { + eurAmountInput.value = formatNumberWithSpaces(cleanInputNumber); eurAmountInput.classList.add('input-is-valid'); return; } @@ -70,6 +73,27 @@ function validateAndFormatEurAmountInput() { eurAmountInput.classList.add('input-is-invalid'); } +function formatNumberWithSpaces(num) { + return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' '); +} + +function updateBtcInput() { + const eurToSatRate = 1021; + const cleanEurAmount = readIntFromEurAmountInput(); + + const satsAmount = cleanEurAmount * eurToSatRate; + const formattedSatsAmount = formatNumberWithSpaces(satsAmount); + btcAmountInput.value = formattedSatsAmount; +} + +buttonStartCreateOffer.addEventListener('click', () => { + toggleCreateOfferControls(); +}); + +closeOfferControls.addEventListener('click', () => { + toggleCreateOfferControls(); +}); + buyOrSellButtons.forEach((button) => { button.addEventListener('click', () => { toggleBuyOrSellButtonGroup(); @@ -86,4 +110,9 @@ buttonDecreasePremium.addEventListener('click', () => { eurAmountInput.addEventListener('blur', () => { validateAndFormatEurAmountInput(); + updateBtcInput(); +}); + +eurAmountInput.addEventListener('input', () => { + eurAmountInput.value = eurAmountInput.value.replace(/[^0-9]/g, ''); }); diff --git a/src/views/offers.ejs b/src/views/offers.ejs index 4ec4fbe..15e9669 100644 --- a/src/views/offers.ejs +++ b/src/views/offers.ejs @@ -42,14 +42,27 @@

¿Cuánto?

-
+
- + /> +
+ +
+
+
+ + +
+ SAT