-
Notifications
You must be signed in to change notification settings - Fork 23
/
UpdateProfile.jsx
143 lines (133 loc) · 4.14 KB
/
UpdateProfile.jsx
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Col, Row } from 'react-materialize';
import { Redirect } from 'react-router-dom';
import Header from '../Header';
import updateProfile from '../../actions/updateProfile';
import LoginRedirect from '../auth/LoginRedirect';
/**
* Component to update user profile
*
* @class UpdateProfile
*/
export class UpdateProfile extends Component {
state = {
firstName: this.props.user.firstName,
lastName: this.props.user.lastName,
password: '',
newPassword: '',
}
/**
* updates component state with changes in the form fields
*
* @param {any} event DOM change event
*
* @return {Undefined } updates component state
*/
handleChange = (event) => {
event.preventDefault();
const formField = event.target.name;
const user = { ...this.state };
user[formField] = event.target.value.trim();
this.setState(() => user);
}
/**
* handles form submission
*
* @param {Object} event DOM onSubmit event
*
* @return {undefined} sends a network request and sets state
*/
handleSubmit = (event) => {
event.preventDefault();
this.props.updateProfile(this.state)
.then(() => this.setState(() => ({ redirect: true })))
.catch(() => {})
;
}
/**
* class method that renders a component to the DOM
*
* @return {Object} JSX element
*/
render() {
const renderPage = this.state.redirect === true ?
<Redirect to='/dashboard' /> :
<div>
<Header
activeLink='dashboard'
/>
<main>
<Row>
<Col s={12} m={6} className="offset-m3">
<form onSubmit={this.handleSubmit}>
<div style={{ marginBottom: '50px' }}>
<h5 className="center" >
Update your profile information
</h5>
</div>
<div className="input-field">
<h6>First Name:</h6>
<input
onChange={this.handleChange}
name="firstName"
value={this.state.firstName || ''}
/>
</div>
<div className="input-field">
<h6>Last Name:</h6>
<input
onChange={this.handleChange}
name="lastName"
value={this.state.lastName || ''}
/>
</div>
<h6 className="red-text darken-4" >
Leave the password fields empty unless you actually want
to change your password
</h6>
<div className="input-field">
<h6>Current Password:</h6>
<input
onChange={this.handleChange}
name="password"
type="password"
value={this.state.currentPassword}
/>
</div>
<div className="input-field">
<h6>New Password:</h6>
<input
onChange={this.handleChange}
type="password"
name="newPassword"
value={this.state.newPassword}
/>
</div>
<div className="input-field">
<input
className="btn teal darken-4 waves-effect waves-light"
style={{ width: '100%' }}
type="submit"
value="Submit"
/>
</div>
</form>
</Col>
</Row>
</main>
</div>;
return this.props.isLoggedIn ? renderPage : <LoginRedirect />;
}
}
UpdateProfile.propTypes = {
user: PropTypes.object.isRequired,
isLoggedIn: PropTypes.bool.isRequired,
updateProfile: PropTypes.func.isRequired,
};
const mapStateToProps = ({ authReducer }) => ({
user: authReducer.user,
isLoggedIn: authReducer.isLoggedIn,
});
export default connect(mapStateToProps, { updateProfile })(UpdateProfile);