diff --git a/src/GlobalRedux/Features/balance/balanceSlice.tsx b/src/GlobalRedux/Features/balance/balanceSlice.tsx index b56a090..aff8e7e 100644 --- a/src/GlobalRedux/Features/balance/balanceSlice.tsx +++ b/src/GlobalRedux/Features/balance/balanceSlice.tsx @@ -1,3 +1,30 @@ +'use client'; + +import { createSlice, PayloadAction } from '@reduxjs/toolkit'; + +interface BalanceState { + value: string | undefined; +} + +const initialState: BalanceState = { + value: '', +}; + +const balanceSlice = createSlice({ + name: 'balance', + initialState, + reducers: { + setBalance: (state, action: PayloadAction) => { + state.value = action.payload; + }, + }, +}); + +export const { setBalance } = balanceSlice.actions; +export default balanceSlice.reducer; + + +/* import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; import useGetBalance from '@/app/hooks/useGetBalance'; interface BalanceState { @@ -45,3 +72,4 @@ const balanceSlice = createSlice({ }); export default balanceSlice.reducer; + */ \ No newline at end of file diff --git a/src/app/components/CreditCard/CreditCard.tsx b/src/app/components/CreditCard/CreditCard.tsx index 4ea71e3..6da4df2 100644 --- a/src/app/components/CreditCard/CreditCard.tsx +++ b/src/app/components/CreditCard/CreditCard.tsx @@ -6,9 +6,10 @@ import truncateEthAddress from "truncate-eth-address"; import { motion } from "framer-motion"; import { usePrivySmartAccount } from "@zerodev/privy"; import useGetBalance from "@/app/hooks/useGetBalance"; -import { useSelector } from "react-redux"; +import { useDispatch, useSelector } from "react-redux"; import { useEffect, useState } from "react"; +import { setBalance } from "@/GlobalRedux/Features/balance/balanceSlice"; export default function CreditCard() { @@ -20,17 +21,25 @@ export default function CreditCard() { const {user } = usePrivySmartAccount() const reduxBalance = useSelector((state: any) => state.balance.value) + + const hookBalance = useGetBalance(address as string) + const dispatch = useDispatch() + useEffect(() => { + console.log('redux balance', reduxBalance) setBalanceToShow(reduxBalance) if (hookBalance !== reduxBalance) { setBalanceToShow(hookBalance) + dispatch(setBalance(hookBalance)) } }, [hookBalance, reduxBalance]) + + return ( <>