-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): add account wallet store
- Loading branch information
Showing
8 changed files
with
10,984 additions
and
5,978 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"cSpell.words": [ | ||
"EXCHANGERATES", | ||
"hono", | ||
"openai" | ||
"openai", | ||
"tanstack" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { getHonoClient } from '@/lib/client' | ||
import type { WalletFormValues } from '@6pm/validation' | ||
import { createId } from '@paralleldrive/cuid2' | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' | ||
import { keyBy } from 'lodash-es' | ||
import { useMemo } from 'react' | ||
import type { StoreHookQueryOptions } from '../core/stores' | ||
import { useCreateTransaction } from '../transaction/hooks' | ||
import { walletQueries } from './queries' | ||
import { useWalletStore } from './store' | ||
|
||
export const useWalletListQueryOptions = ( | ||
queryOptions?: StoreHookQueryOptions, | ||
) => { | ||
const wallets = useWalletStore().wallets | ||
const setWalletsState = useWalletStore((state) => state.setWallets) | ||
return { | ||
...walletQueries.all({ setWalletsState }), | ||
initialData: wallets?.length > 0 ? wallets : undefined, | ||
...queryOptions, | ||
} | ||
} | ||
|
||
export const useWalletList = (queryOptions?: StoreHookQueryOptions) => { | ||
const wallets = useWalletStore().wallets | ||
const queryOpts = useWalletListQueryOptions(queryOptions) | ||
|
||
const query = useQuery(queryOpts) | ||
|
||
const walletsDict = useMemo(() => keyBy(wallets, 'id'), [wallets]) | ||
|
||
return { | ||
...query, | ||
wallets, | ||
walletsDict, | ||
} | ||
} | ||
|
||
export const useWallet = (walletId: string) => { | ||
const wallets = useWalletStore().wallets | ||
const wallet = useMemo( | ||
() => wallets.find((wallet) => wallet.id === walletId) || null, | ||
[wallets, walletId], | ||
) | ||
|
||
return { wallet } | ||
} | ||
|
||
export const useUpdateWallet = () => { | ||
const updateWalletInStore = useWalletStore((state) => state.updateWallet) | ||
const { walletsDict } = useWalletList() | ||
const { mutateAsync: mutateCreateTransaction } = useCreateTransaction() | ||
const queryClient = useQueryClient() | ||
|
||
const mutation = useMutation( | ||
{ | ||
mutationFn: async ({ | ||
id: walletId, | ||
data, | ||
}: { | ||
id: string | ||
data: WalletFormValues | ||
}) => { | ||
const { balance, ...walletData } = data | ||
const hc = await getHonoClient() | ||
const wallet = walletsDict[walletId] | ||
|
||
if (!wallet) { | ||
return | ||
} | ||
|
||
// Update wallet info and create adjust balance transaction | ||
const promises = [ | ||
hc.v1.wallets[':walletId'].$put({ | ||
param: { walletId }, | ||
json: walletData, | ||
}), | ||
] | ||
|
||
if (balance) { | ||
promises.push( | ||
mutateCreateTransaction({ | ||
id: createId(), | ||
data: { | ||
amount: balance ?? 0, | ||
walletAccountId: wallet.id, | ||
currency: wallet.preferredCurrency, | ||
date: new Date(), | ||
note: 'Adjust balance', | ||
}, | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
}) as any, | ||
) | ||
} | ||
|
||
const [result] = await Promise.all(promises) | ||
|
||
return result | ||
}, | ||
onMutate: async ({ id, data: { balance, ...data } }) => { | ||
let wallet = walletsDict[id] | ||
if (!wallet) { | ||
return | ||
} | ||
|
||
const newBallance = (balance ?? 0) + (wallet.balance ?? 0) | ||
|
||
wallet = { | ||
...wallet, | ||
...data, | ||
balance: newBallance, | ||
updatedAt: new Date(), | ||
} | ||
|
||
updateWalletInStore(wallet) | ||
|
||
return wallet | ||
}, | ||
}, | ||
queryClient, | ||
) | ||
|
||
return mutation | ||
} | ||
|
||
// export const useCreateWallet = () => {} | ||
|
||
// export const useDeleteWallet = () => {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getHonoClient } from '@/lib/client' | ||
import { | ||
type WalletAccountWithBalance, | ||
WalletAccountWithBalanceSchema, | ||
} from '@6pm/validation' | ||
import { createQueryKeys } from '@lukemorales/query-key-factory' | ||
import { z } from 'zod' | ||
|
||
export const walletQueries = createQueryKeys('wallet', { | ||
all: ({ | ||
setWalletsState, | ||
}: { | ||
setWalletsState: (wallets: WalletAccountWithBalance[]) => void | ||
}) => ({ | ||
queryKey: [{}], | ||
queryFn: async () => { | ||
const hc = await getHonoClient() | ||
const res = await hc.v1.wallets.$get() | ||
|
||
if (!res.ok) { | ||
throw new Error(await res.text()) | ||
} | ||
|
||
try { | ||
const items = await res.json() | ||
const wallets = items.map((item) => | ||
WalletAccountWithBalanceSchema.extend({ | ||
id: z.string().cuid2(), | ||
}).parse(item), | ||
) | ||
|
||
setWalletsState(wallets) | ||
return wallets | ||
} catch (error) { | ||
// biome-ignore lint/suspicious/noConsoleLog: <explanation> | ||
console.log(error) | ||
} | ||
}, | ||
}), | ||
}) |
Oops, something went wrong.