Skip to content

Commit

Permalink
fix: numbering of wallets and accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Oct 18, 2024
1 parent ff79a1c commit 713bdbe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
20 changes: 14 additions & 6 deletions apps/mobile/src/store/accounts/accounts.write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { produce } from 'immer';
import { handleAppResetWithState, userAddsWallet, userRemovesWallet } from '../global-action';
import { BitcoinKeychain } from '../keychains/bitcoin/utils';
import { StacksKeychain } from '../keychains/stacks/utils';
import { Optional, entitySchema, handleEntityActionWith, makeAccountIdentifer } from '../utils';
import {
Optional,
entitySchema,
getWalletAccountsByAccountId,
handleEntityActionWith,
makeAccountIdentifer,
} from '../utils';
import { AccountStatus, AccountStore, accountStoreSchema } from './utils';

export const accountsAdapter = createEntityAdapter<AccountStore, string>({
Expand Down Expand Up @@ -52,10 +58,7 @@ export const accountsSlice = createSlice({
const firstAccountIndex = 0;
const id = makeAccountIdentifer(action.payload.wallet.fingerprint, firstAccountIndex);

accountsAdapter.addOne(
state,
addAccountDefaults({ account: { id }, accountIdx: state.ids.length })
);
accountsAdapter.addOne(state, addAccountDefaults({ account: { id }, accountIdx: 1 }));
})

.addCase(userRemovesWallet, (state, action) => {
Expand All @@ -65,9 +68,14 @@ export const accountsSlice = createSlice({
})

.addCase(userAddsAccount, (state, action) => {
const thisWalletsAccounts = getWalletAccountsByAccountId(state, action.payload.account.id);

return accountsAdapter.addOne(
state,
addAccountDefaults({ account: action.payload.account, accountIdx: state.ids.length })
addAccountDefaults({
account: action.payload.account,
accountIdx: thisWalletsAccounts.length + 1,
})
);
})

Expand Down
16 changes: 16 additions & 0 deletions apps/mobile/src/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
import { EntityState, PayloadAction, ThunkAction, UnknownAction } from '@reduxjs/toolkit';
import z from 'zod';

import { AccountStore } from './accounts/utils';
import { resetWallet } from './global-action';
import { RootState, store } from './index';
import { deleteAllMnemonics } from './storage-persistors';
Expand Down Expand Up @@ -47,6 +48,21 @@ export function destructAccountIdentifier(accountId: string) {
return { fingerprint, accountIndex: +accountIndex };
}

export function getWalletAccountsByAccountId(
state: EntityState<AccountStore, string>,
accountId: string
) {
const { fingerprint: thisWalletFingerprint } = destructAccountIdentifier(accountId);

return state.ids.filter(id => {
if (state.entities[id]?.id) {
const { fingerprint } = destructAccountIdentifier(state.entities[id].id);
return fingerprint === thisWalletFingerprint;
}
return false;
});
}

type AdapterMethod<T> = (state: EntityState<T, string>, args: any) => void;

export function handleEntityActionWith<State, Payload, R extends AdapterMethod<State>>(
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/store/wallets/wallets.write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const walletSlice = createSlice({
.addCase(userAddsWallet, (state, action) =>
walletAdapter.addOne(
state,
addWalletDefaults({ wallet: action.payload.wallet, walletIdx: state.ids.length })
addWalletDefaults({ wallet: action.payload.wallet, walletIdx: state.ids.length + 1 })
)
)

Expand Down

0 comments on commit 713bdbe

Please sign in to comment.