1- import React , { useCallback , useEffect , useMemo , useState } from "react" ;
1+ import React , { useCallback , useMemo , useState } from "react" ;
22import { FormattedNumber } from "react-intl" ;
33import { Button , Card , CardContent , CardFooter , CardHeader , CardTitle , Snackbar , Switch } from "@akashnetwork/ui/components" ;
44import { LinearProgress } from "@mui/material" ;
@@ -10,31 +10,24 @@ import Layout from "@src/components/layout/Layout";
1010import { Title } from "@src/components/shared/Title" ;
1111import { PaymentSuccessAnimation } from "@src/components/user/payment/PaymentSuccessAnimation" ;
1212import { useWalletBalance } from "@src/hooks/useWalletBalance" ;
13- import { usePaymentMethodsQuery , useWalletSettingsMutations , useWalletSettingsQuery } from "@src/queries" ;
13+ import { useDefaultPaymentMethodQuery , useWalletSettingsMutations , useWalletSettingsQuery } from "@src/queries" ;
1414
1515export const AccountOverview : React . FunctionComponent = ( ) => {
16- const [ selectedPaymentMethodId , setSelectedPaymentMethodId ] = useState < string | undefined > ( ) ;
1716 const [ showPaymentPopup , setShowPaymentPopup ] = useState ( false ) ;
1817 const [ showPaymentSuccess , setShowPaymentSuccess ] = useState < { amount : string ; show : boolean } > ( { amount : "" , show : false } ) ;
1918 const { enqueueSnackbar } = useSnackbar ( ) ;
20- const { data : paymentMethods = [ ] , isLoading : isLoadingPaymentMethods } = usePaymentMethodsQuery ( ) ;
19+ const { data : defaultPaymentMethod , isLoading : isLoadingDefaultPaymentMethod } = useDefaultPaymentMethodQuery ( ) ;
2120 const { balance : walletBalance , isLoading : isWalletBalanceLoading } = useWalletBalance ( ) ;
2221 const { data : walletSettings } = useWalletSettingsQuery ( ) ;
2322 const { updateWalletSettings, createWalletSettings } = useWalletSettingsMutations ( ) ;
2423
25- const isLoading = isLoadingPaymentMethods ;
24+ const isLoading = isLoadingDefaultPaymentMethod ;
2625
27- useEffect ( ( ) => {
28- if ( paymentMethods . length > 0 ) {
29- const defaultPaymentMethod = paymentMethods . find ( method => method . isDefault ) ;
26+ const defaultPaymentMethodId = useMemo ( ( ) => {
27+ return defaultPaymentMethod ?. id ;
28+ } , [ defaultPaymentMethod ] ) ;
3029
31- if ( defaultPaymentMethod ) {
32- setSelectedPaymentMethodId ( defaultPaymentMethod . id ) ;
33- }
34- }
35- } , [ paymentMethods , selectedPaymentMethodId ] ) ;
36-
37- const onReloadChange = useCallback (
30+ const toggleAutoReload = useCallback (
3831 async ( autoReloadEnabled : boolean ) => {
3932 try {
4033 const settings = {
@@ -52,16 +45,15 @@ export const AccountOverview: React.FunctionComponent = () => {
5245 autoHideDuration : 3000
5346 } ) ;
5447 } catch ( error : unknown ) {
55- console . error ( "Failed to update auto-reload settings:" , error ) ;
5648 enqueueSnackbar ( < Snackbar title = "Failed to update Auto Reload settings" iconVariant = "error" /> , { variant : "error" } ) ;
5749 }
5850 } ,
5951 [ createWalletSettings , enqueueSnackbar , updateWalletSettings , walletSettings ]
6052 ) ;
6153
6254 const isReloadChangeDisabled = useMemo ( ( ) => {
63- return paymentMethods . length === 0 || updateWalletSettings . isPending || createWalletSettings . isPending ;
64- } , [ updateWalletSettings . isPending , createWalletSettings . isPending , paymentMethods . length ] ) ;
55+ return ! defaultPaymentMethod || updateWalletSettings . isPending || createWalletSettings . isPending ;
56+ } , [ defaultPaymentMethod , updateWalletSettings . isPending , createWalletSettings . isPending ] ) ;
6557
6658 if ( isLoading ) {
6759 return (
@@ -103,7 +95,7 @@ export const AccountOverview: React.FunctionComponent = () => {
10395 size = "icon"
10496 className = "h-8 w-8 text-xs"
10597 onClick = { ( ) => setShowPaymentPopup ( true ) }
106- disabled = { isWalletBalanceLoading || ! selectedPaymentMethodId }
98+ disabled = { isWalletBalanceLoading || ! defaultPaymentMethod }
10799 >
108100 < Plus />
109101 </ Button >
@@ -118,7 +110,7 @@ export const AccountOverview: React.FunctionComponent = () => {
118110 < CardContent className = "pt-2" >
119111 < div >
120112 < div className = "pt-4" >
121- < Switch checked = { walletSettings ?. autoReloadEnabled ?? false } onCheckedChange = { onReloadChange } disabled = { isReloadChangeDisabled } />
113+ < Switch checked = { walletSettings ?. autoReloadEnabled ?? false } onCheckedChange = { toggleAutoReload } disabled = { isReloadChangeDisabled } />
122114 </ div >
123115 < div className = "pt-2 text-sm" > Add funds automatically</ div >
124116 </ div >
@@ -137,7 +129,7 @@ export const AccountOverview: React.FunctionComponent = () => {
137129 < PaymentPopup
138130 open = { showPaymentPopup }
139131 onClose = { ( ) => setShowPaymentPopup ( false ) }
140- selectedPaymentMethodId = { selectedPaymentMethodId }
132+ selectedPaymentMethodId = { defaultPaymentMethodId }
141133 setShowPaymentSuccess = { setShowPaymentSuccess }
142134 />
143135 ) }
0 commit comments