Skip to content

Commit

Permalink
fix: fixed bytes input so it can handle decimal points
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 5, 2024
1 parent 02cffa7 commit 606d033
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
22 changes: 20 additions & 2 deletions src/lib/components/input/bytes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,39 @@
GB: 1000 * 1000 * 1000
};
let decimal = $state(false);
/** Convert the string into a usable number and update valid state */
$effect(() => {
console.log({ input });
const numericInput = Number(input);
console.log({ numericInput });
if (isNaN(numericInput) || numericInput < 0) {
console.log('invalid');
valid = false;
return;
}
decimal = false;
// Allowing decimal values
if (String(numericInput) !== input) {
decimal = true;
setTimeout(() => {
decimal = false;
}, 4000);
}
valid = true;
const multiplier = UNIT_MULTIPLIERS[unit];
value = numericInput * multiplier;
});
$effect(() => {
if (input !== String(value)) {
if (input !== String(value) && !decimal) {
input = value ? String(value / UNIT_MULTIPLIERS[unit]) : '';
}
});
Expand Down Expand Up @@ -89,7 +107,7 @@
bind:ref
bind:value={input}
placeholder="0 {unit}"
inputmode="decimal"
inputmode="numeric"
{autofocus}
{oninput}
{...props}
Expand Down
6 changes: 0 additions & 6 deletions src/routes/[network]/(account)/ram/buy/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,12 @@
function setAssetAmount() {
buyRamState.format = 'asset';
console.log({ expectedBytes: buyRamState.expectedBytes });
buyRamState.bytes = buyRamState.expectedBytes;
}
function setBytesAmount() {
buyRamState.format = 'bytes';
buyRamState.tokens = buyRamState.bytesValue;
console.log({
bytes: buyRamState.bytes,
bytesValue: buyRamState.bytesValue,
quant: buyRamState.bytesValue.value
});
assetInput?.set(buyRamState.bytesValue);
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/ram/sell/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
<p class="text-red-500">Insufficient RAM available. Please enter a smaller amount.</p>
{/if}
<p>
Available RAM:
RAM available:
{#if context.account}
{sellRamState.maxInKBs}
{:else}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/[network]/(account)/ram/sell/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class SellRAMState {
: Math.floor((this.tokens.value / this.pricePerKB.value) * 1000)
);

public kbsToSell: number | undefined = $derived((this.bytesToSell || 0) / 1000);
public kbsToSell: Asset | undefined = $derived(Asset.fromUnits(this.bytesToSell || 0, '3,KB'));

public maxValue: Asset = $derived(
Asset.from(this.max * this.pricePerKB.value, this.pricePerKB.symbol)
Expand Down

0 comments on commit 606d033

Please sign in to comment.