Skip to content

Commit 4bdc63c

Browse files
fix: bugs (#126)
Co-authored-by: Aimen Sahnoun <[email protected]>
1 parent 1ef66a4 commit 4bdc63c

File tree

14 files changed

+181
-61
lines changed

14 files changed

+181
-61
lines changed

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"@radix-ui/react-separator": "^1.1.7",
3232
"@radix-ui/react-slot": "^1.1.2",
3333
"@radix-ui/react-tabs": "^1.1.3",
34-
"@radix-ui/react-tooltip": "^1.2.7",
34+
"@radix-ui/react-tooltip": "^1.2.8",
3535
"@reown/appkit": "^1.6.8",
3636
"@reown/appkit-adapter-ethers5": "^1.6.8",
3737
"@tanstack/react-query": "^4.36.1",

src/app/i/[id]/page.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { api } from "@/trpc/server";
77
import { ArrowLeft } from "lucide-react";
88
import type { Metadata } from "next";
99
import Link from "next/link";
10-
import { notFound } from "next/navigation";
10+
import { notFound, redirect } from "next/navigation";
1111

1212
export const metadata: Metadata = {
1313
title: "Invoice Me | EasyInvoice",
@@ -22,11 +22,16 @@ export default async function InvoiceMePage({
2222
// TODO solve unauthenticated access
2323
// TODO solve not found error like the subscription plan page
2424
const invoiceMeLink = await api.invoiceMe.getById.query(params.id);
25+
const currentUser = await api.auth.getSessionInfo.query();
2526

2627
if (!invoiceMeLink) {
2728
notFound();
2829
}
2930

31+
if (currentUser.user && currentUser.user.id === invoiceMeLink.user.id) {
32+
redirect("/dashboard");
33+
}
34+
3035
const invoiceCount = await getInvoiceCount(invoiceMeLink.user.id);
3136

3237
return (
@@ -54,6 +59,15 @@ export default async function InvoiceMePage({
5459
clientEmail: invoiceMeLink.user.email ?? "",
5560
userId: invoiceMeLink.user.id,
5661
}}
62+
currentUser={
63+
currentUser.user
64+
? {
65+
id: currentUser.user.id,
66+
name: currentUser.user.name ?? "",
67+
email: currentUser.user.email ?? "",
68+
}
69+
: undefined
70+
}
5771
invoiceCount={invoiceCount}
5872
/>
5973
</main>

src/components/batch-payout.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ import {
4949
TableHeader,
5050
TableRow,
5151
} from "@/components/ui/table/table";
52+
5253
import {
5354
PAYOUT_CURRENCIES,
5455
type PayoutCurrency,
5556
formatCurrencyLabel,
5657
getPaymentCurrenciesForPayout,
5758
} from "@/lib/constants/currencies";
5859
import { handleBatchPayment } from "@/lib/helpers/batch-payment";
60+
61+
import { useSwitchNetwork } from "@/lib/hooks/use-switch-network";
5962
import { payoutSchema } from "@/lib/schemas/payment";
6063
import { api } from "@/trpc/react";
6164
import { z } from "zod";
@@ -74,6 +77,7 @@ export type BatchPaymentFormValues = z.infer<typeof batchPaymentFormSchema>;
7477

7578
export function BatchPayout() {
7679
const { mutateAsync: batchPay } = api.payment.batchPay.useMutation();
80+
const { switchToPaymentNetwork } = useSwitchNetwork();
7781

7882
const [paymentStatus, setPaymentStatus] = useState<
7983
"idle" | "processing" | "success" | "error"
@@ -211,6 +215,8 @@ export function BatchPayout() {
211215
return;
212216
}
213217

218+
await switchToPaymentNetwork(data.payouts[0].paymentCurrency);
219+
214220
try {
215221
const ethersProvider = new ethers.providers.Web3Provider(walletProvider);
216222

src/components/create-recurring-payment/blocks/create-recurring-payment-form.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import {
2020
SelectTrigger,
2121
SelectValue,
2222
} from "@/components/ui/select";
23+
2324
import {
2425
RECURRING_PAYMENT_CURRENCIES,
2526
formatCurrencyLabel,
2627
} from "@/lib/constants/currencies";
28+
2729
import { useCreateRecurringPayment } from "@/lib/hooks/use-create-recurring-payment";
30+
import { useSwitchNetwork } from "@/lib/hooks/use-switch-network";
2831
import { paymentApiSchema } from "@/lib/schemas/payment";
2932
import { RecurrenceFrequency } from "@/server/db/schema";
3033
import { zodResolver } from "@hookform/resolvers/zod";
@@ -51,6 +54,8 @@ type RecurringPaymentFormValues = z.infer<typeof recurringPaymentFormSchema>;
5154
export function CreateRecurringPaymentForm() {
5255
const router = useRouter();
5356

57+
const { switchToPaymentNetwork } = useSwitchNetwork();
58+
5459
const { address, isConnected } = useAppKitAccount();
5560
const { open } = useAppKit();
5661
const { walletProvider } = useAppKitProvider("eip155");
@@ -91,6 +96,8 @@ export function CreateRecurringPaymentForm() {
9196
return;
9297
}
9398

99+
await switchToPaymentNetwork(data.invoiceCurrency);
100+
94101
const recurringPaymentCurrency = data.invoiceCurrency;
95102
const recurringPaymentBody = {
96103
payee: data.payee,

src/components/dashboard/invoices-received.tsx

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import {
1010
TableHeader,
1111
TableRow,
1212
} from "@/components/ui/table/table";
13-
import { ID_TO_APPKIT_NETWORK, NETWORK_TO_ID } from "@/lib/constants/chains";
13+
import { NETWORK_TO_ID } from "@/lib/constants/chains";
1414
import { handleBatchPayment } from "@/lib/helpers/batch-payment";
1515
import {
1616
calculateTotalsByCurrency,
1717
formatCurrencyTotals,
1818
} from "@/lib/helpers/currency";
19+
import { useSwitchNetwork } from "@/lib/hooks/use-switch-network";
1920
import type { Request } from "@/server/db/schema";
2021
import { api } from "@/trpc/react";
2122
import {
2223
useAppKit,
2324
useAppKitAccount,
24-
useAppKitNetwork,
2525
useAppKitProvider,
2626
} from "@reown/appkit/react";
2727
import { ethers } from "ethers";
@@ -73,7 +73,7 @@ export const InvoicesReceived = ({
7373
const { open } = useAppKit();
7474
const { isConnected, address } = useAppKitAccount();
7575
const { walletProvider } = useAppKitProvider("eip155");
76-
const { chainId, switchNetwork } = useAppKitNetwork();
76+
const { switchToChainId } = useSwitchNetwork();
7777

7878
const { data: invoices } = api.invoice.getAllIssuedToMe.useQuery(undefined, {
7979
initialData: initialReceivedInvoices,
@@ -134,27 +134,12 @@ export const InvoicesReceived = ({
134134
return;
135135
}
136136

137-
const targetChain =
138-
NETWORK_TO_ID[lastSelectedNetwork as keyof typeof NETWORK_TO_ID];
139-
140-
if (targetChain !== chainId) {
141-
const targetAppkitNetwork =
142-
ID_TO_APPKIT_NETWORK[targetChain as keyof typeof ID_TO_APPKIT_NETWORK];
137+
try {
138+
const targetChainId =
139+
NETWORK_TO_ID[lastSelectedNetwork as keyof typeof NETWORK_TO_ID];
143140

144-
toast("Switching to network", {
145-
description: `Switching to ${targetAppkitNetwork.name} network`,
146-
});
141+
await switchToChainId(targetChainId);
147142

148-
try {
149-
switchNetwork(targetAppkitNetwork);
150-
} catch (_) {
151-
toast("Error switching network");
152-
setIsPayingInvoices(false);
153-
return;
154-
}
155-
}
156-
157-
try {
158143
const ethersProvider = new ethers.providers.Web3Provider(
159144
walletProvider as ethers.providers.ExternalProvider,
160145
);

src/components/direct-payout.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ import {
3838
SelectTrigger,
3939
SelectValue,
4040
} from "@/components/ui/select";
41+
4142
import {
4243
PAYOUT_CURRENCIES,
4344
type PayoutCurrency,
4445
formatCurrencyLabel,
4546
getPaymentCurrenciesForPayout,
4647
} from "@/lib/constants/currencies";
48+
49+
import { useSwitchNetwork } from "@/lib/hooks/use-switch-network";
4750
import { paymentApiSchema } from "@/lib/schemas/payment";
4851
import { api } from "@/trpc/react";
4952
import type { z } from "zod";
@@ -57,6 +60,7 @@ export type DirectPaymentFormValues = z.infer<typeof directPaymentFormSchema>;
5760

5861
export function DirectPayment() {
5962
const { mutateAsync: pay } = api.payment.pay.useMutation();
63+
const { switchToPaymentNetwork } = useSwitchNetwork();
6064

6165
const [paymentStatus, setPaymentStatus] = useState<
6266
"idle" | "processing" | "success" | "error"
@@ -118,6 +122,7 @@ export function DirectPayment() {
118122
return;
119123
}
120124

125+
await switchToPaymentNetwork(data.paymentCurrency);
121126
setPaymentStatus("processing");
122127

123128
try {

src/components/invoice-creator.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,19 @@ export function InvoiceCreator({
3737
const router = useRouter();
3838
const isInvoiceMe = !!recipientDetails?.userId;
3939
const utils = api.useUtils();
40-
const { mutate: createInvoice, isLoading } = isInvoiceMe
40+
41+
const { mutateAsync: createInvoice, isLoading } = isInvoiceMe
4142
? api.invoice.createFromInvoiceMe.useMutation({
42-
onSuccess: () => {
43-
toast.success("Invoice created successfully", {
44-
description: "You can safely close this page now",
45-
});
43+
onSuccess: async () => {
44+
if (!currentUser) {
45+
toast.success("Invoice created successfully", {
46+
description: "You can safely close this page now",
47+
});
48+
return;
49+
}
50+
toast.success("Invoice created successfully");
51+
await utils.invoice.getAll.invalidate();
52+
router.push("/dashboard");
4653
},
4754
onError: (error) => {
4855
toast.error("Failed to create invoice", {

src/components/invoice-form.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const checkPaymentDetailsApproval = (
9090
interface InvoiceFormProps {
9191
currentUser: User;
9292
form: UseFormReturn<InvoiceFormValues>;
93-
onSubmit: (data: InvoiceFormValues) => void;
93+
onSubmit: (data: InvoiceFormValues) => Promise<void>;
9494
isLoading: boolean;
9595
recipientDetails?: {
9696
clientName: string;
@@ -1037,7 +1037,7 @@ export function InvoiceForm({
10371037
<Button
10381038
type="submit"
10391039
className="bg-black hover:bg-zinc-800 text-white transition-colors"
1040-
disabled={isLoading || isSubmitting}
1040+
disabled={isLoading || isSubmitting || invoiceCreated}
10411041
>
10421042
{isLoading || isSubmitting
10431043
? "Creating..."

src/components/payment-section.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import { api } from "@/trpc/react";
1515
import {
1616
useAppKit,
1717
useAppKitAccount,
18-
useAppKitNetwork,
1918
useAppKitProvider,
2019
useDisconnect,
2120
} from "@reown/appkit/react";
2221
import { ethers } from "ethers";
2322
import { AlertCircle, CheckCircle, Clock, Loader2, Wallet } from "lucide-react";
2423

24+
import { useSwitchNetwork } from "@/lib/hooks/use-switch-network";
2525
import {
2626
getPaymentSectionStatusClass,
2727
getStatusDisplayText,
@@ -106,8 +106,8 @@ export function PaymentSection({ serverInvoice }: PaymentSectionProps) {
106106
const { open } = useAppKit();
107107
const { disconnect } = useDisconnect();
108108
const { isConnected, address } = useAppKitAccount();
109+
const { chainId, switchToChainId } = useSwitchNetwork();
109110
const { walletProvider } = useAppKitProvider("eip155");
110-
const { chainId, switchNetwork } = useAppKitNetwork();
111111
const [showRoutes, setShowRoutes] = useState(false);
112112
const [selectedRoute, setSelectedRoute] = useState<PaymentRouteType | null>(
113113
null,
@@ -343,12 +343,7 @@ export function PaymentSection({ serverInvoice }: PaymentSectionProps) {
343343
description: `Switching to ${targetAppkitNetwork?.name} network`,
344344
});
345345

346-
try {
347-
await switchNetwork(targetAppkitNetwork);
348-
} catch (_) {
349-
toast("Error switching network");
350-
return;
351-
}
346+
await switchToChainId(targetChain);
352347
}
353348

354349
const ethersProvider = new ethers.providers.Web3Provider(

0 commit comments

Comments
 (0)