-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChangeEmail.js
129 lines (123 loc) · 3.29 KB
/
ChangeEmail.js
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
import React from "react";
import { Form, Input, Message, Header } from "semantic-ui-react";
import Shell from "./Shell";
import { authAxios } from "../../utils";
import { changeEmailURL, emailURL } from "../../constants";
class ChangeEmail extends React.Component {
state = {
currentEmail: "",
email: "",
confirmEmail: "",
error: null,
loading: false
};
componentDidMount() {
this.handleUserDetails();
}
handleUserDetails = () => {
this.setState({
loading: true
});
authAxios
.get(emailURL)
.then(res => {
this.setState({
loading: false,
currentEmail: res.data.email
});
})
.catch(err => {
this.setState({
loading: false,
error: err.response.data.message
});
});
};
handleChange = e => {
this.setState({
[e.target.name]: e.target.value,
error: null
});
};
handleSubmit = e => {
e.preventDefault();
this.setState({
loading: true
});
const { email, confirmEmail } = this.state;
if (email === "" || confirmEmail === "") {
this.setState({
error: "Please fill in all the fields"
});
} else {
if (email === confirmEmail) {
authAxios
.post(changeEmailURL, {
email,
confirm_email: confirmEmail
})
.then(res => {
this.setState({
loading: false,
email: "",
confirmEmail: "",
currentEmail: email
});
})
.catch(err => {
this.setState({
loading: false,
error: err.response.data.message
});
});
} else {
this.setState({
loading: false,
error: "Your emails do not match"
});
}
}
};
render() {
const { currentEmail, email, confirmEmail, error, loading } = this.state;
return (
<Shell>
<Header as="h4">Change email</Header>
<Form onSubmit={this.handleSubmit} error={error !== null}>
<Form.Field>
<label>Current email</label>
<Input value={currentEmail} disabled />
</Form.Field>
<Form.Field required>
<label>New email</label>
<Input
value={email}
placeholder="New email"
type="email"
name="email"
onChange={this.handleChange}
/>
</Form.Field>
<Form.Field required>
<label>Confirm email</label>
<Input
value={confirmEmail}
placeholder="Confirm email"
type="email"
name="confirmEmail"
onChange={this.handleChange}
/>
</Form.Field>
{error && <Message error header="Error" content={error} />}
{/* <Button primary type="submit" loading={loading} disabled={loading}>
Submit
</Button> */}
<button type="submit" loading={loading} disabled={loading} className="bg-black font-bold hover:bg-gray-700
rounded-lg px-[24px] py-3 text-gray-100 hover:shadow-xl transition
duration-150 ">Submit</button>
</Form>
</Shell>
);
}
}
export default ChangeEmail;