Skip to content

Commit af9585e

Browse files
authored
Merge pull request #42 from Code-the-Dream-School/fogort-password-send-email-feature
forget password reset email send to user
2 parents 360faa9 + e3789cc commit af9585e

File tree

1 file changed

+126
-44
lines changed

1 file changed

+126
-44
lines changed

src/components/ForgotPassword.jsx

+126-44
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,129 @@
1-
import React from "react";
2-
import { Input, Button, HStack, Link, Text, Group, VStack } from "@chakra-ui/react";
1+
import React, { useState } from "react";
2+
import {
3+
Input,
4+
Button,
5+
HStack,
6+
Link,
7+
Text,
8+
Group,
9+
VStack,
10+
} from "@chakra-ui/react";
311
import { Field } from "./ui/field";
4-
const ForgotPassword = ({ onFormSwitch }) => (
5-
<>
6-
<Group mx='auto'>
7-
<VStack>
8-
<Field
9-
marginTop="32px"
10-
label="Email"
11-
required
12-
helperText="We'll send you instructions for renewing your password."
13-
>
14-
<Input placeholder="Enter your email" w="308pxpx" />
15-
</Field>
16-
<Button
17-
backgroundColor="orange.700"
18-
h="50px"
19-
borderRadius="100px"
20-
marginTop="32px"
21-
width="308px"
22-
>
23-
<Text
24-
fontWeight="800"
25-
fontSize="16px"
26-
letterSpacing="0.9px"
27-
color="white"
28-
>
29-
SEND EMAIL
30-
</Text>
31-
</Button>
32-
</VStack>
33-
</Group>
34-
<HStack justifyContent="center" mt="4">
35-
<Link
36-
color="teal.500"
37-
onClick={() => onFormSwitch("login")}
38-
marginBottom="25px"
39-
fontWeight="700"
40-
>
41-
Back to Sign In
42-
</Link>
43-
</HStack>
44-
</>
45-
);
12+
import { Toaster, toaster } from "../components/ui/toaster";
13+
const ForgotPassword = ({ onFormSwitch }) => {
14+
const url = "http://localhost:8001/api/v1/forgot-password";
15+
//const [error, setError] = useState("This field is required");
16+
const [userEmail, setUserEmail] = useState({
17+
email: "",
18+
});
19+
const handleChange = (e) => {
20+
const { name, value } = e.target;
21+
setUserEmail((prevData) => ({
22+
...prevData,
23+
[name]: value,
24+
}));
25+
};
26+
const handleOnClickLoginBT = async (e) => {
27+
e.preventDefault();
28+
try {
29+
const response = await fetch(url, {
30+
method: "POST",
31+
headers: {
32+
"Content-Type": "application/json",
33+
},
34+
body: JSON.stringify({
35+
email: userEmail.email,
36+
}),
37+
});
38+
39+
if (response.ok) {
40+
const data = await response.json();
41+
console.log("Server Response:", data);
42+
setUserEmail({
43+
email: "",
44+
});
45+
toaster.create({
46+
title: "Password reset email sent!",
47+
type: "success",
48+
duration: 4000,
49+
action: {
50+
label: "x",
51+
},
52+
});
53+
} else {
54+
console.error("Failed to reset password!");
55+
toaster.create({
56+
title: "Password reset unsuccessful",
57+
description: "Please verify your email and try again.",
58+
type: "error",
59+
action: {
60+
label: "x",
61+
},
62+
});
63+
}
64+
} catch (error) {
65+
console.error("Error:", error);
66+
toaster.create({
67+
title: error,
68+
type: "error",
69+
action: {
70+
label: "x",
71+
},
72+
});
73+
}
74+
};
75+
return (
76+
<>
77+
<Group mx="auto">
78+
<VStack>
79+
<Field
80+
marginTop="32px"
81+
label="Email"
82+
//errorText={error}
83+
helperText="We'll send you instructions for renewing your password."
84+
>
85+
<Input
86+
placeholder="Enter your email"
87+
w="308pxpx"
88+
name="email"
89+
required
90+
invalid
91+
value={userEmail.email}
92+
onChange={handleChange}
93+
/>
94+
</Field>
95+
<Button
96+
backgroundColor="orange.700"
97+
h="50px"
98+
borderRadius="100px"
99+
marginTop="32px"
100+
width="308px"
101+
onClick={handleOnClickLoginBT}
102+
>
103+
<Text
104+
fontWeight="800"
105+
fontSize="16px"
106+
letterSpacing="0.9px"
107+
color="white"
108+
>
109+
SEND EMAIL
110+
</Text>
111+
</Button>
112+
</VStack>
113+
</Group>
114+
<HStack justifyContent="center" mt="4">
115+
<Link
116+
color="teal.500"
117+
onClick={() => onFormSwitch("login")}
118+
marginBottom="25px"
119+
fontWeight="700"
120+
>
121+
Back to Sign In
122+
</Link>
123+
</HStack>
124+
<Toaster />
125+
</>
126+
);
127+
};
46128

47129
export default ForgotPassword;

0 commit comments

Comments
 (0)