Build a Finance SaaS Platform -20 (Update Transaction Data)

Add Select Account Componet

Add features/accounts/hooks/use-select-account.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { useRef, useState } from "react";

import { useGetAccounts } from "@/features/accounts/api/use-get-accounts";
import { useCreateAccount } from "@/features/accounts/api/use-create-account";

import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle
} from "@/components/ui/dialog";

import { Button } from "@/components/ui/button";
import { Select } from "@/components/select";

export const useSelectAccount = (): [()=> JSX.Element,()=> Promise<unknown>] => {
const accountQuery = useGetAccounts();
const accountMutation = useCreateAccount();
const onCreateAccount = (name: string)=> accountMutation.mutate({
name
});

const accountOptions = (accountQuery.data ?? []).map((account) =>({
label: account.name,
value: account.id,
}));

const [promise, setPromise] = useState<{ resolve:(value: string | undefined)=> void} | null>(null);

const selectValue = useRef<string>();

const confirm = () => new Promise((resolve, reject) =>{
setPromise({ resolve });
})

const handleClose = () => {
setPromise(null);
}

const handleConfirm = ()=> {
promise?.resolve(selectValue.current);
handleClose();
}

const handleCancel = () => {
promise?.resolve(undefined);
handleClose();
}

const ComfirmationDialog = () => (
<Dialog open={promise != null}>
<DialogContent>
<DialogHeader>
<DialogTitle>
Select Account
</DialogTitle>
<DialogDescription>
Please select an account to continue.
</DialogDescription>
</DialogHeader>
<Select
placeholder="Select an account"
options={accountOptions}
onCreate={onCreateAccount}
onChange={(value) => selectValue.current = value}
disabled={accountQuery.isLoading || accountMutation.isPending}
/>
<DialogFooter className="pt-2">
<Button
onClick={handleCancel}
variant="outline"
>
Cancel
</Button>
<Button onClick={handleConfirm}>
Confirm
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);

return [ComfirmationDialog, confirm];
}

Modify Page

Modify app/(dashboard)/transactions/page.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

...
import { toast } from "sonner";
import { transactions as transactionSchema } from "@/db/schema";
import { useSelectAccount } from "@/features/accounts/hooks/use-select-account";
import { useBulkCreateTransactions } from "@/features/transactions/api/use-bulk-create-transactions";
...
const [AccountDialog, confirm] = useSelectAccount();
const createTransactions = useBulkCreateTransactions();
...
const onSubmitImport = async (
values: typeof transactionSchema.$inferInsert[],
)=>{
const accountId = await confirm();

if(!accountId) {
return toast.error("Please select an account to continue.");
}

const data = values.map((value) =>({
...value,
accountId: accountId as string,
}));

createTransactions.mutate(data, {
onSuccess: () => {
handleCancleImport();
}
});
};
...
if (variant === VARIANTS.IMPORT) {
return(
<>
<AccountDialog />
<ImportCard
data={importResult.data}
onCancel={handleCancleImport}
onSummit={onSubmitImport}
/>
</>
)
...

请我喝杯咖啡吧~

支付宝
微信