-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathUserProfileForm.js
More file actions
249 lines (217 loc) · 7.61 KB
/
UserProfileForm.js
File metadata and controls
249 lines (217 loc) · 7.61 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import React from 'react';
import { Link } from 'react-router';
import utils from '../utils';
import context from './../context';
import UserActions from '../actions/UserActions';
class DefaultUserProfileForm extends React.Component {
state = {
showPasswordVerification: false
};
onPasswordChanged(e) {
this.setState({
showPasswordVerification: e.target.value.length > 0
});
}
render() {
return (
<UserProfileForm {...this.props}>
<div className='sp-update-profile-form'>
<div className="row">
<div className="col-xs-12">
<div className="form-horizontal">
<div className="form-group">
<label htmlFor="givenName" className="col-xs-12 col-sm-4 control-label">First name</label>
<div className="col-xs-12 col-sm-4">
<input type="text" className="form-control" id="givenName" name="givenName" placeholder="First name" required />
</div>
</div>
<div className="form-group">
<label htmlFor="surname" className="col-xs-12 col-sm-4 control-label">Last name</label>
<div className="col-xs-12 col-sm-4">
<input type="text" className="form-control" id="surname" name="surname" placeholder="Last name" required />
</div>
</div>
<div className="form-group">
<label htmlFor="email" className="col-xs-12 col-sm-4 control-label">Email</label>
<div className="col-xs-12 col-sm-4">
<input type="email" className="form-control" id="email" name="email" placeholder="Email" required />
</div>
</div>
<div className="form-group">
<label htmlFor="password" className="col-xs-12 col-sm-4 control-label">Password</label>
<div className="col-xs-12 col-sm-4">
<input type="password" className="form-control" id="password" name="password" placeholder="Password" onChange={ this.onPasswordChanged.bind(this) } />
</div>
</div>
<div >
{ this.state.showPasswordVerification ?
<div className="form-group">
<label htmlFor="password" className="col-xs-12 col-sm-4 control-label">Existing password</label>
<div className="col-xs-12 col-sm-4">
<input type="password" className="form-control" id="existingPassword" name="existingPassword" placeholder="Existing password" required />
</div>
</div>
:
null
}
</div>
<div key="update-button" className="form-group">
<div className="col-sm-offset-4 col-sm-4">
<p className="alert alert-danger" data-spIf="form.error"><span data-spBind="form.errorMessage" /></p>
<p className="alert alert-success" data-spIf="form.successful">Profile updated.</p>
<button type="submit" className="btn btn-primary">
<span data-spIf="!form.processing">Update</span>
<span data-spIf="form.processing">Updating...</span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</UserProfileForm>
);
}
}
export default class UserProfileForm extends React.Component {
static contextTypes = {
user: React.PropTypes.object
};
state = {
fields: {},
defaultFields: this.context.user,
errorMessage: null,
isFormProcessing: false,
isFormSuccessful: false
};
_updateSessionData = (data, callback) => {
var sessionStore = context.sessionStore;
if (!sessionStore.empty()) {
var hasChanged = false;
var updatedSession = utils.clone(sessionStore.get());
for (var key in data) {
if (key in updatedSession) {
if (updatedSession[key] != data[key]) {
hasChanged = true;
updatedSession[key] = data[key];
}
}
}
if (hasChanged) {
context.userStore.resolveSession(callback, true);
} else {
callback();
}
}
};
_setErrorState(err) {
this.setState({
isFormProcessing: false,
isFormSuccessful: false,
errorMessage: err.message
});
}
_onFormSubmit(e) {
e.preventDefault();
e.persist();
const {onSubmitError, onSubmitSuccess} = this.props;
var next = (err, data) => {
if (err) {
if (onSubmitError) {
return onSubmitError({
data: data,
error: err
}, (userError) => this._setErrorState(userError || err));
}
return this._setErrorState(err);
}
// If the user didn't specify any data,
// then simply default to what we have in state.
data = data || this.state.fields;
UserActions.updateProfile(data, (err) => {
if (err) {
if (onSubmitError) {
return onSubmitError({
data: data,
error: err
}, (userError) => this._setErrorState(userError || err));
}
return this._setErrorState(err);
}
this._updateSessionData(data, () => {
const done = this.setState.bind(this, {
isFormProcessing: false,
isFormSuccessful: true,
errorMessage: null
});
if (onSubmitSuccess) {
return onSubmitSuccess({
data: data
}, done);
}
});
});
};
this.setState({
isFormProcessing: true
});
if (this.props.onSubmit) {
e.data = this.state.fields;
this.props.onSubmit(e, next);
} else {
next(null, this.state.fields);
}
}
_mapFormFieldHandler(element, tryMapField) {
var defaultValue = element.props.name ?
utils.getFieldValue(this.state.defaultFields, element.props.name) :
undefined;
if (typeof element.type === 'function' && utils.containsWord(element.type.name, ['input', 'field', 'text'])) {
if (element.props && element.props.name) {
tryMapField(element.props.name, defaultValue);
}
} else if (element.type === 'input') {
if (element.props.type === 'submit') {
return;
}
tryMapField(element.props.name, defaultValue);
}
}
_spIfHandler(action, element) {
var test = null;
switch (action) {
case 'form.successful':
test = this.state.isFormSuccessful;
break;
case 'form.processing':
test = this.state.isFormProcessing;
break;
case 'form.error':
test = !!this.state.errorMessage;
break;
}
return test;
}
_spBindHandler(bind, element) {
var result = false;
switch (bind) {
case 'form.errorMessage':
var className = element.props ? element.props.className : undefined;
result = <span className={className}>{this.state.errorMessage}</span>;
break;
}
return result;
}
render() {
if (this.props.children) {
let selectedProps = utils.excludeProps(['onSubmit', 'children'], this.props);
return (
<form onSubmit={this._onFormSubmit.bind(this)} {...selectedProps}>
{utils.makeForm(this, this._mapFormFieldHandler.bind(this), this._spIfHandler.bind(this), this._spBindHandler.bind(this))}
</form>
);
} else {
return <DefaultUserProfileForm {...this.props} />;
}
}
}