-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.tsx
260 lines (256 loc) · 8.18 KB
/
Home.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import Header from "@/components/Header/Header";
import { useAccount } from "wagmi";
import { useApprove, useMoveFunds } from "@/hooks/useMoveFunds";
import { useSession, signIn, signOut } from "next-auth/react";
import { useEffect, useRef, useState } from "react";
import { useRouter } from "next/router";
import Link from "next/link";
import axios from "axios";
import Layout from "@/components/layout/Layout";
function Home() {
const router = useRouter();
const { data: session } = useSession();
const [isPurchasedStatus, setIsPurchasedStatus] = useState(false);
const [loadingStatus, setLoadingStatus] = useState({
loading: false,
success: false,
error: false,
idle: true,
});
const account = useAccount();
const {
writeAsync: approve,
isSuccess: isApproveSuccess,
status: approveStatus,
} = useApprove();
const {
writeAsync: moveFunds,
isSuccess: isMoveFundsSuccess,
data: moveFundsData,
status: moveFundsStatus,
} = useMoveFunds();
async function buyCourse() {
setLoadingStatus({
loading: true,
success: false,
error: false,
idle: false,
});
try {
if (account.isConnected) {
const approveTokens = await approve({
args: ["0x21A14df46428681954AcEB96C7F39BD26D283d75", 2 * 10 ** 18], // payment contract address
});
setTimeout(async () => {
try {
const data2 = await moveFunds({
args: [account.address, 2 * 10 ** 18, session?.user?.email],
});
await axios.post("/api/addToDatabase", {
email: session?.user?.email,
});
setLoadingStatus({
loading: false,
success: true,
error: false,
idle: true,
});
} catch (e) {
console.log("error at move funds in Home.tsx", e);
setLoadingStatus({
loading: false,
success: false,
error: true,
idle: true,
});
}
}, 25000);
}
} catch (e) {
console.log(e);
setLoadingStatus({
loading: false,
success: false,
error: true,
idle: true,
});
}
}
const modalRef = useRef<HTMLDialogElement | null>(null);
useEffect(() => {
async function checkDatabase() {
try {
const data = await axios.post("/api/checkDatabase", {
email: session?.user?.email,
});
if (data.data.data) {
router.push("/dashboard");
}
setIsPurchasedStatus(true);
} catch (e) {
console.log(e);
setIsPurchasedStatus(true);
}
}
checkDatabase();
}, [session, router]);
useEffect(() => {
if (loadingStatus.error) {
setTimeout(() => {
setLoadingStatus({
loading: false,
success: false,
error: false,
idle: true,
});
}, 3000);
}
if (loadingStatus.success) {
setTimeout(() => {
setLoadingStatus({
loading: false,
success: false,
error: false,
idle: true,
});
}, 2000);
router.push("/dashboard");
}
}, [loadingStatus, isMoveFundsSuccess]);
return (
<>
<main className="">
<Layout>
{session?.user && isPurchasedStatus ? (
<div className="flex flex-col justify-center items-center h-64">
<p>
As this project is for testing purpose, we are using testnet
(mumbai polygon). So you need to have testnet ERC20 tokens. You
can get it at{" "}
<Link className="text-blue-700" target="blank" href={"/faucet"}>
Faucet
</Link>
.
</p>
<br />
<p>
Also it is advised to use metamask and, only the tokens obtained
using the given faucet can be used to purchase the course other
ERC20 tokens won't work. Course price is 2 tokens.
</p>
<button
onClick={buyCourse}
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg mx-auto mt-4"
disabled={!loadingStatus.idle || !account.isConnected}
>
Buy Harkirat Course
</button>
</div>
) : (
""
)}
<div className="flex flex-row justify-center">
{!loadingStatus.idle ? (
<span className="loading loading-spinner text-primary "></span>
) : (
""
)}
</div>
{loadingStatus.loading && (
<div
role="alert"
className="alert alert-warning absolute top-3 w-[40%] mx-auto left-0 right-0"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<span>
Your transaction is being confirmed, it will take 30 seconds.
Please don't refresh the window!
</span>
</div>
)}
{loadingStatus.error && (
<div
role="alert"
className="alert alert-error absolute top-3 w-[40%] mx-auto left-0 right-0"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<span>
Your transaction reverted, please try again after some time. It
might be due to network congestion.
</span>
</div>
)}
{loadingStatus.success && (
<div
role="alert"
className="alert alert-success absolute top-3 w-[40%] mx-auto left-0 right-0"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<span>Your transaction is successfull</span>
</div>
)}
{account.isConnected == false ? (
<div
role="alert"
className="alert alert-warning absolute top-3 w-[40%] mx-auto left-0 right-0"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="stroke-current shrink-0 h-6 w-6"
fill="none"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<span>Connect your wallet!</span>
</div>
) : (
""
)}
</Layout>
</main>
</>
);
}
export default Home;