-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathSetPasswordPage.js
executable file
·162 lines (142 loc) · 5.3 KB
/
SetPasswordPage.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
156
157
158
159
160
161
162
import React, {Component} from 'react';
import {
SafeAreaView,
View,
} from 'react-native';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import lodashGet from 'lodash/get';
import {
propTypes as validateLinkPropTypes,
defaultProps as validateLinkDefaultProps,
} from './validateLinkPropTypes';
import styles from '../styles/styles';
import * as Session from '../libs/actions/Session';
import ONYXKEYS from '../ONYXKEYS';
import SignInPageLayout from './signin/SignInPageLayout';
import withLocalize, {withLocalizePropTypes} from '../components/withLocalize';
import compose from '../libs/compose';
import NewPasswordForm from './settings/NewPasswordForm';
import FormAlertWithSubmitButton from '../components/FormAlertWithSubmitButton';
const propTypes = {
/* Onyx Props */
/** The details about the account that the user is signing in with */
account: PropTypes.shape({
/** An error message to display to the user */
error: PropTypes.string,
/** Whether a sign on form is loading (being submitted) */
loading: PropTypes.bool,
}),
/** The credentials of the logged in person */
credentials: PropTypes.shape({
/** The email the user logged in with */
login: PropTypes.string,
/** The password used to log in the user */
password: PropTypes.string,
}),
/** Session object */
session: PropTypes.shape({
/** An error message to display to the user */
error: PropTypes.string,
}),
/** User signup object */
userSignUp: PropTypes.shape({
/** Is Validating Email */
isValidating: PropTypes.bool,
/** Auth token used to change password */
authToken: PropTypes.string,
}),
/** The accountID and validateCode are passed via the URL */
route: validateLinkPropTypes,
...withLocalizePropTypes,
};
const defaultProps = {
account: {},
credentials: {},
route: validateLinkDefaultProps,
session: {
error: '',
authToken: '',
},
userSignUp: {
isValidating: false,
authToken: '',
},
};
class SetPasswordPage extends Component {
constructor(props) {
super(props);
this.validateAndSubmitForm = this.validateAndSubmitForm.bind(this);
this.state = {
password: '',
isFormValid: false,
};
}
componentDidMount() {
const accountID = lodashGet(this.props.route.params, 'accountID', '');
const validateCode = lodashGet(this.props.route.params, 'validateCode', '');
if (this.props.userSignUp.authToken) {
return;
}
Session.validateEmail(accountID, validateCode);
}
validateAndSubmitForm() {
if (!this.state.isFormValid) {
return;
}
const accountID = lodashGet(this.props.route.params, 'accountID', '');
const validateCode = lodashGet(this.props.route.params, 'validateCode', '');
if (this.props.userSignUp.authToken) {
Session.changePasswordAndSignIn(this.props.userSignUp.authToken, this.state.password);
} else {
Session.setPassword(this.state.password, validateCode, accountID);
}
}
render() {
const buttonText = this.props.userSignUp.isValidating ? this.props.translate('setPasswordPage.verifyingAccount') : this.props.translate('setPasswordPage.setPassword');
const sessionError = this.props.session.error && this.props.translate(this.props.session.error);
const error = sessionError || this.props.account.error;
return (
<SafeAreaView style={[styles.signInPage]}>
<SignInPageLayout
shouldShowWelcomeText
welcomeText={this.props.translate('setPasswordPage.passwordFormTitle')}
>
<View style={[styles.mb4]}>
<NewPasswordForm
password={this.state.password}
updatePassword={password => this.setState({password})}
updateIsFormValid={isValid => this.setState({isFormValid: isValid})}
onSubmitEditing={this.validateAndSubmitForm}
/>
</View>
<View>
<FormAlertWithSubmitButton
buttonText={buttonText}
isLoading={this.props.account.loading || this.props.userSignUp.isValidatingEmail}
onSubmit={this.validateAndSubmitForm}
containerStyles={[styles.mb2, styles.mh0]}
message={error}
isAlertVisible={!_.isEmpty(error)}
/>
</View>
</SignInPageLayout>
</SafeAreaView>
);
}
}
SetPasswordPage.propTypes = propTypes;
SetPasswordPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
credentials: {key: ONYXKEYS.CREDENTIALS},
account: {key: ONYXKEYS.ACCOUNT},
session: {
key: ONYXKEYS.SESSION,
initWithStoredValues: false,
},
userSignUp: {key: ONYXKEYS.USER_SIGN_UP},
}),
)(SetPasswordPage);