Move account selection to settings store

This commit is contained in:
Ilja 2022-02-21 14:46:45 +02:00
parent 99ab6286f2
commit c69121d673
4 changed files with 19 additions and 14 deletions

View File

@ -1,13 +1,15 @@
import { ReactEventHandler } from 'react' import SettingsStore from '@/store/SettingsStore'
import { useSnapshot } from 'valtio'
interface IProps { export default function AccountPicker() {
value: number const { account } = useSnapshot(SettingsStore.state)
onChange: ReactEventHandler<HTMLSelectElement>
}
export default function AccountPicker({ value, onChange }: IProps) {
return ( return (
<select value={Number(value)} onChange={onChange}> <select
value={account}
onChange={e => SettingsStore.setAccount(e.currentTarget.value)}
aria-label="accounts"
>
<option value={0}>Account 1</option> <option value={0}>Account 1</option>
<option value={1}>Account 2</option> <option value={1}>Account 2</option>
</select> </select>

View File

@ -5,18 +5,17 @@ import { EIP155_MAINNET_CHAINS, EIP155_TEST_CHAINS } from '@/data/EIP155Data'
import SettingsStore from '@/store/SettingsStore' import SettingsStore from '@/store/SettingsStore'
import { wallets } from '@/utils/WalletUtil' import { wallets } from '@/utils/WalletUtil'
import { Text } from '@nextui-org/react' import { Text } from '@nextui-org/react'
import { Fragment, useState } from 'react' import { Fragment } from 'react'
import { useSnapshot } from 'valtio' import { useSnapshot } from 'valtio'
export default function HomePage() { export default function HomePage() {
const [account, setAccount] = useState(0) const { testNets, account } = useSnapshot(SettingsStore.state)
const { testNets } = useSnapshot(SettingsStore.state)
const addresses = Object.keys(wallets) const addresses = Object.keys(wallets)
return ( return (
<Fragment> <Fragment>
<PageHeader title="Accounts"> <PageHeader title="Accounts">
<AccountPicker value={account} onChange={e => setAccount(Number(e.currentTarget.value))} /> <AccountPicker />
</PageHeader> </PageHeader>
<Text h4 css={{ marginBottom: '$5' }}> <Text h4 css={{ marginBottom: '$5' }}>
Mainnets Mainnets

View File

@ -5,13 +5,15 @@ import { proxy } from 'valtio'
*/ */
interface State { interface State {
testNets: boolean testNets: boolean
account: number
} }
/** /**
* State * State
*/ */
const state = proxy<State>({ const state = proxy<State>({
testNets: typeof localStorage !== 'undefined' ? Boolean(localStorage.getItem('TEST_NETS')) : true testNets: typeof localStorage !== 'undefined' ? Boolean(localStorage.getItem('TEST_NETS')) : true,
account: 0
}) })
/** /**
@ -20,6 +22,10 @@ const state = proxy<State>({
const SettingsStore = { const SettingsStore = {
state, state,
setAccount(value: number | string) {
state.account = Number(value)
},
toggleTestNets() { toggleTestNets() {
state.testNets = !state.testNets state.testNets = !state.testNets
if (state.testNets) { if (state.testNets) {

View File

@ -21,6 +21,4 @@ export function createOrRestoreWallet() {
[wallet1.address]: wallet1, [wallet1.address]: wallet1,
[wallet2.address]: wallet2 [wallet2.address]: wallet2
} }
console.log(wallets)
} }