Skip to content

Commit 1519b96

Browse files
committed
fix: follow-up on Yaro's PR feedback
refs #1779
1 parent a78e10a commit 1519b96

File tree

9 files changed

+1170
-90
lines changed

9 files changed

+1170
-90
lines changed

apps/deploy-web/src/components/billing-usage/AccountOverview/AccountOverview.tsx

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useMemo, useState } from "react";
1+
import React, { useCallback, useMemo, useState } from "react";
22
import { FormattedNumber } from "react-intl";
33
import { Button, Card, CardContent, CardFooter, CardHeader, CardTitle, Snackbar, Switch } from "@akashnetwork/ui/components";
44
import { LinearProgress } from "@mui/material";
@@ -10,31 +10,24 @@ import Layout from "@src/components/layout/Layout";
1010
import { Title } from "@src/components/shared/Title";
1111
import { PaymentSuccessAnimation } from "@src/components/user/payment/PaymentSuccessAnimation";
1212
import { useWalletBalance } from "@src/hooks/useWalletBalance";
13-
import { usePaymentMethodsQuery, useWalletSettingsMutations, useWalletSettingsQuery } from "@src/queries";
13+
import { useDefaultPaymentMethodQuery, useWalletSettingsMutations, useWalletSettingsQuery } from "@src/queries";
1414

1515
export 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
)}

apps/deploy-web/src/components/billing-usage/PaymentMethodsContainer/PaymentMethodsContainer.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ type PaymentMethodsContainerProps = {
1616

1717
export const PaymentMethodsContainer: React.FC<PaymentMethodsContainerProps> = ({ children, dependencies: d = DEPENDENCIES }) => {
1818
const { data: paymentMethods = [], isLoading: isLoadingPaymentMethods, refetch: refetchPaymentMethods } = d.usePaymentMethodsQuery();
19-
const { setPaymentMethodAsDefault, removePaymentMethod } = d.usePaymentMutations();
19+
const paymentMutations = d.usePaymentMutations();
2020
const { data: setupIntent, mutate: createSetupIntent, reset: resetSetupIntent } = d.useSetupIntentMutation();
2121
const [showAddPaymentMethod, setShowAddPaymentMethod] = useState(false);
2222

2323
const onSetPaymentMethodAsDefault = useCallback(
2424
(id: string) => {
25-
setPaymentMethodAsDefault.mutateAsync(id);
25+
paymentMutations.setPaymentMethodAsDefault.mutate(id);
2626
},
27-
[setPaymentMethodAsDefault]
27+
[paymentMutations.setPaymentMethodAsDefault]
2828
);
2929

3030
const onRemovePaymentMethod = useCallback(
3131
(id: string) => {
32-
removePaymentMethod.mutateAsync(id);
32+
paymentMutations.removePaymentMethod.mutate(id);
3333
},
34-
[removePaymentMethod]
34+
[paymentMutations.removePaymentMethod]
3535
);
3636

3737
const onAddCardSuccess = async () => {

apps/deploy-web/src/components/billing-usage/PaymentMethodsView/PaymentMethodsRow.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export const PaymentMethodsRow: React.FC<PaymentMethodsRowProps> = ({
3535
}) => {
3636
const [open, setOpen] = useState(false);
3737

38-
function handleMenuClick() {
38+
function openMenu() {
3939
setOpen(true);
4040
}
4141

42-
const handleMenuClose = () => {
42+
const closeMenu = () => {
4343
setOpen(false);
4444
};
4545

@@ -75,12 +75,12 @@ export const PaymentMethodsRow: React.FC<PaymentMethodsRowProps> = ({
7575

7676
const setPaymentAsDefault = useCallback(() => {
7777
onSetPaymentMethodAsDefault(paymentMethod.id);
78-
handleMenuClose();
78+
closeMenu();
7979
}, [onSetPaymentMethodAsDefault, paymentMethod.id]);
8080

8181
const removePaymentMethod = useCallback(() => {
8282
onRemovePaymentMethod(paymentMethod.id);
83-
handleMenuClose();
83+
closeMenu();
8484
}, [onRemovePaymentMethod, paymentMethod.id]);
8585

8686
return (
@@ -94,7 +94,7 @@ export const PaymentMethodsRow: React.FC<PaymentMethodsRowProps> = ({
9494
{!paymentMethod.isDefault && (
9595
<d.DropdownMenu modal={false} open={open}>
9696
<d.DropdownMenuTrigger asChild>
97-
<d.Button onClick={handleMenuClick} size="icon" variant="ghost" className="rounded-full">
97+
<d.Button onClick={openMenu} size="icon" variant="ghost" className="rounded-full">
9898
<MoreHoriz />
9999
</d.Button>
100100
</d.DropdownMenuTrigger>

0 commit comments

Comments
 (0)