This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChangePassword.js
155 lines (150 loc) · 4.47 KB
/
ChangePassword.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
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
import React, { Component } from "react";
import { withStyles } from "@material-ui/core";
import TextField from "@material-ui/core/TextField/TextField";
import Typography from "@material-ui/core/Typography/Typography";
import Button from "@material-ui/core/Button";
import FormError from "./components/FormError";
import axios from "axios";
import Cookies from "js-cookie";
axios.defaults.headers.post["X-CSRFToken"] = Cookies.get("csrftoken");
class ChangePassword extends Component {
state = {
oldPassword: "",
newPassword: "",
confirmPassword: "",
passwordUpdated: false,
errors: []
};
confirmPasswordError() {
return this.state.newPassword !== this.state.confirmPassword;
}
canContinue() {
return (
this.state.oldPassword !== undefined &&
this.state.oldPassword !== "" &&
this.state.newPassword !== undefined &&
this.state.newPassword !== "" &&
this.state.confirmPassword !== undefined &&
this.state.confirmPassword !== "" &&
!this.confirmPasswordError()
);
}
onClick() {
this.setState({ passwordUpdated: false });
const passwords = {
old_password: this.state.oldPassword,
new_password: this.state.newPassword
};
axios
.post("/api/change_password/", passwords)
.then(response => {
this.setState({
oldPassword: "",
newPassword: "",
confirmPassword: "",
passwordUpdated: true,
errors: []
});
})
.catch(error => this.setState({ errors: error.response.data.messages }));
}
onEnter(e) {
if (e.key === "Enter") {
document.getElementById("op-button").click();
}
}
render() {
const { classes } = this.props;
return (
<div className={classes.root}>
{/* <Typography variant={"h4"} className={classes.title}>
Account Settings
</Typography>
<TextField
label="Email Address"
className={classes.textField}
value={this.state.email}
onChange={e => this.setState({ email: e.target.value })}
margin={"normal"} />
<Typography variant={"body1"} color={"inherit"}>
Update your email address on your
<LinkColorless to={routes.profile.route}> profile page </LinkColorless>
</Typography> */}
<FormError errors={this.state.errors} />
<Typography className={classes.title} variant={"h5"} color={"inherit"}>
Change Your Password
</Typography>
<TextField
label="Old Password"
className={classes.textField}
value={this.state.oldPassword}
onChange={e => this.setState({ oldPassword: e.target.value })}
margin={"normal"}
type={"password"}
/>
<TextField
label="New Password"
className={classes.textField}
value={this.state.newPassword}
onChange={e => this.setState({ newPassword: e.target.value })}
margin={"normal"}
type={"password"}
/>
<TextField
label="Confirm Password"
className={classes.textField}
value={this.state.confirmPassword}
onChange={e => this.setState({ confirmPassword: e.target.value })}
margin={"normal"}
type={"password"}
error={this.confirmPasswordError()}
helperText={
this.confirmPasswordError() ? "Passwords do not match" : ""
}
onKeyPress={this.onEnter.bind(this)}
/>
{this.state.passwordUpdated ? (
<Typography
className={classes.verify}
variant={"title"}
color={"primary"}
align={"center"}
>
Password updated successfully!
</Typography>
) : null}
<Button
disabled={!this.canContinue()}
color={"primary"}
variant={"contained"}
className={classes.spaced}
onClick={this.onClick.bind(this)}
id={"op-button"}
>
Update Password
</Button>
</div>
);
}
}
const styles = theme => ({
root: {
display: "flex",
flexDirection: "column",
alignItems: "center"
},
title: {
marginTop: theme.spacing.unit * 5,
fontWeight: 500
},
textField: {
marginLeft: theme.spacing.unit * 25,
marginRight: theme.spacing.unit * 25,
width: "100%"
},
spaced: {
marginTop: theme.spacing.unit * 3
},
verify: {}
});
export default withStyles(styles)(ChangePassword);