Skip to content

Commit 745236e

Browse files
committed
Create withdraw request email
1 parent ba2ecd2 commit 745236e

File tree

7 files changed

+881
-19
lines changed

7 files changed

+881
-19
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
"use client";
2+
3+
/* eslint-disable react-hooks/exhaustive-deps */
4+
5+
import {
6+
Box,
7+
Flex,
8+
Heading,
9+
Text,
10+
Image,
11+
Button,
12+
Center,
13+
Container,
14+
FormControl,
15+
Input,
16+
useToast,
17+
} from "@chakra-ui/react";
18+
import { usePathname, useRouter } from "next/navigation";
19+
import jwt from "jsonwebtoken";
20+
import { useEffect, useMemo, useState } from "react";
21+
import axios from "axios";
22+
import { useAccount, useNetwork } from "wagmi";
23+
import { CustomConnectButton } from "@/components/ConnectButton";
24+
import { User } from "@/data-schema/types";
25+
import { NetworkType, UserType } from "@/data-schema/enums";
26+
import { useAuthStore } from "@/store/auth/authStore";
27+
import { shallow } from "zustand/shallow";
28+
import { PermissionType } from "@/data-schema/enums";
29+
import { IUser } from "@/models/User";
30+
import {
31+
createUser,
32+
getUserByWalletAddress,
33+
} from "@/services/mongo/routes/user";
34+
import { createActivity } from "@/services/mongo/routes/activity";
35+
import {
36+
deleteInvitation,
37+
getInvitation,
38+
} from "@/services/mongo/routes/invitation";
39+
import { TestnetNetworks } from "@/data-schema/enums";
40+
import mongoose from "mongoose";
41+
import { convertTimestampToSeconds } from "@/utils/dateTime";
42+
import { ethers } from "ethers";
43+
import { getSignerAddress } from "@/blockchain/utils";
44+
45+
interface DecodedToken {
46+
accountId: mongoose.Schema.Types.ObjectId;
47+
parentAddress: string;
48+
sandboxMode: boolean;
49+
familyName: string;
50+
email: string;
51+
exp: number;
52+
}
53+
54+
const PerformSettlement = () => {
55+
const [countdown, setCountdown] = useState(5);
56+
const [initialUserCheck, setInitialUserCheck] = useState(false);
57+
const [decodedData, setDecodedData] = useState<DecodedToken | null>(null);
58+
const [inviteAccepted, setInviteAccepted] = useState(false);
59+
60+
const pathname = usePathname();
61+
const router = useRouter();
62+
const toast = useToast();
63+
64+
const token = useMemo(() => {
65+
return pathname?.split("/")[2];
66+
}, [pathname]);
67+
68+
const { address, isDisconnected } = useAccount() as any;
69+
const { chain } = useNetwork();
70+
const [inviteNonExistent, setInviteNonExistent] = useState(false);
71+
const [username, setUsername] = useState("");
72+
73+
const { reset } = useAuthStore(
74+
(state) => ({
75+
reset: state.reset,
76+
}),
77+
shallow
78+
);
79+
80+
const createMember = async (decodedToken: DecodedToken) => {
81+
if (!decodedToken) return;
82+
83+
const { email, accountId } = decodedToken;
84+
85+
try {
86+
let userPayload = {
87+
accountId,
88+
termsAgreed: true,
89+
email,
90+
defaultNetwork: TestnetNetworks.GOERLI,
91+
defaultNetworkType: NetworkType.TESTNET,
92+
wallet: address,
93+
username,
94+
userType: UserType.MEMBER,
95+
sandboxMode: false,
96+
permissions: [...Object.values(PermissionType)],
97+
} as IUser;
98+
99+
const user = await createUser(userPayload);
100+
const error = user.response?.data?.error || user.error;
101+
102+
if (error) {
103+
toast({
104+
description: "Database error. Please try again later.",
105+
status: "error",
106+
});
107+
throw new Error(error);
108+
}
109+
110+
await createActivity({
111+
accountId,
112+
wallet: address,
113+
date: convertTimestampToSeconds(Date.now()),
114+
type: "Invitation Accepted.",
115+
});
116+
} catch (err) {
117+
console.error(err);
118+
}
119+
};
120+
121+
const handleToken = async () => {
122+
if (!username) {
123+
toast({
124+
title: "Error",
125+
description: "Please enter a username.",
126+
status: "error",
127+
});
128+
return;
129+
}
130+
131+
if (!token && !initialUserCheck && !isDisconnected) return;
132+
133+
setInitialUserCheck(true);
134+
try {
135+
jwt.verify(
136+
token!,
137+
process.env.NEXT_PUBLIC_JWT_SECRET || "",
138+
async (err, decodedToken) => {
139+
if (err) {
140+
throw err;
141+
} else {
142+
// check if invite still exists
143+
const { exp } = decodedToken as DecodedToken;
144+
if (exp < Math.floor(Date.now() / 1000)) {
145+
setInviteNonExistent(true);
146+
return;
147+
}
148+
setDecodedData(decodedToken as DecodedToken);
149+
}
150+
}
151+
);
152+
} catch (err) {
153+
console.error(err);
154+
}
155+
};
156+
157+
const Logo = () => {
158+
return (
159+
<Flex align="center" ml={5} mt={5}>
160+
<Image
161+
src={"/logos/pig_logo.png"}
162+
alt="Loader"
163+
width="50"
164+
height="50"
165+
/>
166+
167+
<Heading size="lg" ml={5}>
168+
Defikids
169+
</Heading>
170+
</Flex>
171+
);
172+
};
173+
174+
// Check if wallet has already been registered and if invite has already been accepted
175+
useEffect(() => {
176+
if (!decodedData) return;
177+
178+
if (!address) return;
179+
180+
const verifyUser = async () => {
181+
const { accountId, email } = decodedData;
182+
try {
183+
const user = await getUserByWalletAddress(address);
184+
const invitation = await getInvitation(accountId!, email);
185+
186+
if (user._id) {
187+
toast({
188+
title: "Error",
189+
description: "Wallet already registered.",
190+
status: "error",
191+
});
192+
return;
193+
}
194+
195+
if (!invitation) {
196+
setInviteNonExistent(true);
197+
return;
198+
}
199+
200+
await createMember(decodedData);
201+
await deleteInvitation(invitation._id);
202+
203+
setInviteAccepted(true);
204+
} catch (error) {
205+
console.error("Error fetching data:", error);
206+
}
207+
};
208+
209+
verifyUser();
210+
}, [decodedData]);
211+
212+
if (true) {
213+
return <Heading>Perform Settlement</Heading>;
214+
}
215+
216+
if (inviteAccepted) {
217+
return (
218+
<Box h="100vh">
219+
{Logo()}
220+
<Flex direction="column" align="center" justify="center" h="90vh">
221+
<Heading size={"lg"}>Invite Accepted!</Heading>
222+
<Text my={5} color="gray" fontSize="lg">
223+
Your account was successfully created.
224+
</Text>
225+
<Text my={5} color="gray" fontSize="lg">
226+
You may close this window.
227+
</Text>
228+
<Button
229+
mt="3rem"
230+
colorScheme="gray"
231+
size="lg"
232+
style={{
233+
cursor: "pointer",
234+
borderRadius: "10px",
235+
padding: "15px",
236+
}}
237+
onClick={() => {
238+
reset();
239+
router.push(`/member-dashboard/${address}`);
240+
}}
241+
>
242+
<Text fontSize={"lg"}>Go to Dashboard</Text>
243+
</Button>
244+
</Flex>
245+
</Box>
246+
);
247+
}
248+
249+
if (inviteNonExistent) {
250+
return (
251+
<Box h="100vh">
252+
{Logo()}
253+
<Flex direction="column" align="center" justify="center" h="90vh">
254+
<Heading size={"lg"}>Invitation Expired</Heading>
255+
<Text my={5} color="gray" fontSize="lg">
256+
Request a new invite.
257+
</Text>
258+
</Flex>
259+
</Box>
260+
);
261+
}
262+
263+
if (!address) {
264+
return (
265+
<Box h="100vh">
266+
{Logo()}
267+
<Flex direction="column" align="center" justify="center" height="100vh">
268+
<Container size="lg" mb="10rem">
269+
<Heading textAlign="center" size={"lg"}>
270+
Welcome
271+
</Heading>
272+
273+
<Text my={5} textAlign="center">
274+
{`You have been invited to join a DefiKids family. To accept this
275+
invitation, please connect your
276+
wallet.`}
277+
</Text>
278+
<Center mt="5rem">
279+
<CustomConnectButton />
280+
</Center>
281+
</Container>
282+
</Flex>
283+
</Box>
284+
);
285+
}
286+
287+
return (
288+
<Box h="100vh">
289+
{Logo()}
290+
<Flex direction="column" align="center" justify="center" height="100vh">
291+
<Container size="lg" mb="10rem">
292+
<Flex direction="column" justify="center">
293+
{chain?.unsupported ? (
294+
<Text align="center" my={5}>
295+
Your wallet is currently connected to an unsupported network.
296+
Click the button below to change networks.
297+
</Text>
298+
) : (
299+
<Text align="center" my={5}>
300+
This is the wallet you are currently connected to and will be
301+
used to create your DefiKids account.
302+
</Text>
303+
)}
304+
<Center>
305+
<CustomConnectButton />
306+
</Center>
307+
</Flex>
308+
309+
{/* username */}
310+
{!chain?.unsupported && (
311+
<>
312+
<Flex direction="row" align="center" mt="3rem" mx={5}>
313+
<FormControl>
314+
<Input
315+
type="text"
316+
placeholder="Create a username"
317+
value={username}
318+
onChange={(e) => setUsername(e.target.value)}
319+
_hover={{
320+
borderColor: "gray.300",
321+
}}
322+
_focus={{
323+
borderColor: "blue.500",
324+
}}
325+
sx={{
326+
"::placeholder": {
327+
color: "gray.400",
328+
},
329+
}}
330+
/>
331+
</FormControl>
332+
</Flex>
333+
334+
<Center mt={5}>
335+
<Button
336+
mt="3rem"
337+
colorScheme="gray"
338+
size="lg"
339+
style={{
340+
cursor: "pointer",
341+
borderRadius: "10px",
342+
padding: "15px",
343+
}}
344+
onClick={handleToken}
345+
>
346+
<Text fontSize={"lg"}>Accept Invitation</Text>
347+
</Button>
348+
</Center>
349+
</>
350+
)}
351+
</Container>
352+
</Flex>
353+
</Box>
354+
);
355+
};
356+
357+
export default PerformSettlement;

0 commit comments

Comments
 (0)