-
Notifications
You must be signed in to change notification settings - Fork 14
/
withdrawModal.tsx
121 lines (105 loc) · 3.35 KB
/
withdrawModal.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use client";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import {
getUser,
getUserShopBalance,
withdrawBalanceFromShop,
} from "@/db/utils";
import { useSession } from "next-auth/react";
import { useEffect, useState } from "react";
/*
This WithdrawModal component is loaded on the client side and is responsible for withdrawing
the balance from the shop.
*/
export default function WithdrawModal() {
const [showDialog, setShowDialog] = useState<boolean>(false); // Whether the dialog is shown
const [loading, setLoading] = useState<boolean>(true); // Whether the submission is loading
const [success, setSuccess] = useState<boolean>(false); // Whether the withdrawal was successfully created
const [error, setError] = useState<boolean>(false); // Whether there was an error withdrawing the balance
const [amount, setAmount] = useState<string>("0"); // The amount of balance in the shop
const { data: session } = useSession(); // The current session
/*
On page load, get the current user and the amount of balance in the shop.
*/
useEffect(() => {
/*
TODO: Get the withdraw URL query parameter, and if it exists, set the showDialog state to true.
*/
/*
TODO: If the session exists and the user is logged in, get the user from the database and
use the user's balance to update the balance state.
*/
}, [session]);
/*
Withdraw the balance from the shop.
*/
const onWithdraw = async () => {
/*
TODO: Set loading to true.
*/
/*
TODO: Return if the session or the session name does not exist.
*/
/*
TODO: Get the user from the database, and if the user does not exist, set error to true and return.
*/
/*
TODO: Withdraw the balance from the shop and set success to true.
*/
/*
TODO: Remove the withdraw URL query parameter from the URL and set showDialog to false.
*/
};
return (
<Dialog open={showDialog}>
<DialogContent className="">
<DialogHeader>
<DialogTitle>Withdraw Sui from your shop</DialogTitle>
<DialogDescription>
{amount === "0" ? (
"You have nothing to withdraw from your shop. Please create a listing to earn."
) : (
<span>
You have <span className="text-green-500">${amount}</span> in
your shop. Click the button below to withdraw it.
</span>
)}
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
type="button"
variant={"secondary"}
onClick={() => {
window.history.replaceState(null, "", window.location.pathname); // remove URL fragment
setShowDialog(false);
}}
>
Cancel
</Button>
<Button
type="submit"
onClick={onWithdraw}
disabled={loading || amount === "0"}
>
{loading
? "Loading..."
: success
? "Success!"
: error
? "Error - please try again"
: "Withdraw"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}