Skip to content

Commit

Permalink
enhancement: merging both buy ram forms into one
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Oct 5, 2024
1 parent c81f654 commit afa1a16
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 189 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/input/bytes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
valid?: boolean;
validMinimum?: boolean;
validMaximum?: boolean;
value: number;
value: number | undefined;
debug?: boolean;
}
Expand Down Expand Up @@ -56,7 +56,7 @@
const oldMultiplier = UNIT_MULTIPLIERS[unit];
console.log({ value, oldMultiplier });
const newValue = value / oldMultiplier;
const newValue = (value || 0) / oldMultiplier;
input = String(newValue);
});
Expand Down
8 changes: 3 additions & 5 deletions src/routes/[network]/(account)/ram/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
const network = String(data.network);
return [
{ href: `/${network}/ram`, text: 'Overview' },
{ href: `/${network}/ram/buy/tokens`, text: 'Buy RAM' },
{ href: `/${network}/ram/sell/tokens`, text: 'Sell RAM' }
{ href: `/${network}/ram/buy`, text: 'Buy RAM' },
{ href: `/${network}/ram/sell`, text: 'Sell RAM' }
];
});
Expand All @@ -20,9 +20,7 @@
let options = $derived(
tabOptions.map((option) => ({
...option,
active:
`/${$page.url.pathname.split('/').slice(2).slice(0, -1).join('/')}` ===
option.href.split('/').slice(0, -1).join('/')
active: $page.url.pathname.replace(/^\/[^\/]+/, '') === option.href
}))
);
Expand Down
33 changes: 0 additions & 33 deletions src/routes/[network]/(account)/ram/buy/+layout.svelte

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import Stack from '$lib/components/layout/stack.svelte';
import Transaction from '$lib/components/transaction.svelte';
import AssetInput from '$lib/components/input/asset.svelte';
import BytesInput from '$lib/components/input/bytes.svelte';
import { BuyRAMState } from '../state.svelte.js';
import { BuyRAMState } from './state.svelte.js';
import { preventDefault } from '$lib/utils.js';
const context = getContext<UnicoveContext>('state');
Expand All @@ -23,8 +24,6 @@
const buyRamState: BuyRAMState = $state(new BuyRAMState(data.network.chain));
buyRamState.format = 'units';
let bytesInput: AssetInput | undefined = $state();
let transactionId: Checksum256 | undefined = $state();
Expand Down Expand Up @@ -80,13 +79,24 @@
<form onsubmit={preventDefault(handleBuyRAM)}>
<Stack class="gap-3">
<Label for="bytesInput">Amount to buy</Label>
<AssetInput
id="bytesInput"
bind:this={bytesInput}
bind:value={buyRamState.kbsAmount}
placeholder="0.0000 KB"
autofocus
/>
<div class="flex gap-4">
<div class="flex-1">
<AssetInput
id="assetInput"
bind:this={bytesInput}
bind:value={buyRamState.tokens}
autofocus
/>
</div>
<div class="flex-1">
<BytesInput
id="bytesInput"
bind:this={bytesInput}
bind:value={buyRamState.bytes}
placeholder="0 bytes"
/>
</div>
</div>
{#if buyRamState.insufficientBalance}
<p class="text-red-500">Insufficient balance. Please enter a smaller amount.</p>
{/if}
Expand Down
7 changes: 4 additions & 3 deletions src/routes/[network]/(account)/ram/buy/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export class BuyRAMState {
public balance: Asset = $state(defaultQuantity);
public chain: ChainDefinition = $state(Chains.EOS);
public pricePerKB: Asset = $state(defaultQuantity);
public format: 'asset' | 'units' = $state('asset');

public bytes: number = $derived(this.kbsAmount.value * 1000);
public bytes: number | undefined = $state();

public format: 'asset' | 'bytes' = $derived(this.bytes ? 'bytes' : 'asset');

public pricePerByte: Asset = $derived(
Asset.fromUnits(this.pricePerKB.value / 1000, this.pricePerKB.symbol)
Expand Down Expand Up @@ -56,7 +57,7 @@ export class BuyRAMState {

public valid: boolean = $derived(
!!(
((this.format === 'units' && this.bytes) ||
((this.format === 'bytes' && this.bytes) ||
(this.format === 'asset' && this.tokens.value > 0)) &&
this.bytesCost.units.lte(this.balance.units) &&
this.payer.value &&
Expand Down
136 changes: 0 additions & 136 deletions src/routes/[network]/(account)/ram/buy/tokens/+page.svelte

This file was deleted.

0 comments on commit afa1a16

Please sign in to comment.