2016-05-02 20:32:03 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
|
|
|
|
|
|
|
import ChangeEmail from 'components/profile/changeEmail/ChangeEmail';
|
|
|
|
|
2016-05-22 10:53:40 +03:00
|
|
|
import accounts from 'services/api/accounts';
|
|
|
|
|
2016-05-22 17:56:39 +03:00
|
|
|
class ChangeEmailPage extends Component {
|
|
|
|
static displayName = 'ChangeEmailPage';
|
2016-05-02 20:32:03 +03:00
|
|
|
|
|
|
|
static propTypes = {
|
2016-05-20 08:14:14 +03:00
|
|
|
email: PropTypes.string.isRequired,
|
2016-05-28 00:36:10 +03:00
|
|
|
lang: PropTypes.string.isRequired,
|
2017-05-25 22:11:57 +03:00
|
|
|
history: PropTypes.shape({
|
|
|
|
push: PropTypes.func
|
|
|
|
}).isRequired,
|
|
|
|
match: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
step: PropTypes.oneOf(['step1', 'step2', 'step3']),
|
|
|
|
code: PropTypes.string
|
|
|
|
})
|
2016-05-20 08:14:14 +03:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
static contextTypes = {
|
2016-05-22 10:53:40 +03:00
|
|
|
onSubmit: PropTypes.func.isRequired,
|
|
|
|
goToProfile: PropTypes.func.isRequired
|
2016-05-02 20:32:03 +03:00
|
|
|
};
|
|
|
|
|
2016-05-20 08:14:14 +03:00
|
|
|
componentWillMount() {
|
2017-05-25 22:11:57 +03:00
|
|
|
const step = this.props.match.params.step;
|
2016-05-20 08:14:14 +03:00
|
|
|
|
2017-05-25 22:11:57 +03:00
|
|
|
if (step && !/^step[123]$/.test(step)) {
|
2016-05-20 08:14:14 +03:00
|
|
|
// wrong param value
|
2017-05-25 22:11:57 +03:00
|
|
|
this.props.history.push('/404');
|
2016-05-20 08:14:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:32:03 +03:00
|
|
|
render() {
|
2017-05-25 22:11:57 +03:00
|
|
|
const {step = 'step1', code} = this.props.match.params;
|
2016-05-20 08:14:14 +03:00
|
|
|
|
2016-05-02 20:32:03 +03:00
|
|
|
return (
|
2016-05-22 10:53:40 +03:00
|
|
|
<ChangeEmail
|
2016-05-02 20:32:03 +03:00
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
email={this.props.email}
|
2016-05-28 00:36:10 +03:00
|
|
|
lang={this.props.lang}
|
2016-05-22 10:53:40 +03:00
|
|
|
step={step.slice(-1) * 1 - 1}
|
2016-05-20 08:14:14 +03:00
|
|
|
onChangeStep={this.onChangeStep}
|
|
|
|
code={code}
|
2016-05-02 20:32:03 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-20 08:14:14 +03:00
|
|
|
onChangeStep = (step) => {
|
2017-05-25 22:11:57 +03:00
|
|
|
this.props.history.push(`/profile/change-email/step${++step}`);
|
2016-05-20 08:14:14 +03:00
|
|
|
};
|
|
|
|
|
2016-05-22 10:53:40 +03:00
|
|
|
onSubmit = (step, form) => {
|
|
|
|
return this.context.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData: () => {
|
|
|
|
const data = form.serialize();
|
|
|
|
|
|
|
|
switch (step) {
|
|
|
|
case 0:
|
2016-05-22 22:04:52 +03:00
|
|
|
return accounts.requestEmailChange(data).catch(handleErrors());
|
2016-05-22 10:53:40 +03:00
|
|
|
case 1:
|
2016-05-22 18:16:51 +03:00
|
|
|
return accounts.setNewEmail(data).catch(handleErrors('/profile/change-email'));
|
2016-05-22 10:53:40 +03:00
|
|
|
case 2:
|
2016-05-22 18:16:51 +03:00
|
|
|
return accounts.confirmNewEmail(data).catch(handleErrors('/profile/change-email'));
|
2016-05-22 10:53:40 +03:00
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported step ${step}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
step > 1 && this.context.goToProfile();
|
|
|
|
});
|
2016-05-02 20:32:03 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-22 18:16:51 +03:00
|
|
|
function handleErrors(repeatUrl) {
|
|
|
|
return (resp) => {
|
|
|
|
if (resp.errors) {
|
|
|
|
if (resp.errors.key) {
|
|
|
|
resp.errors.key = {
|
|
|
|
type: resp.errors.key,
|
|
|
|
payload: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (['error.key_not_exists', 'error.key_expire'].includes(resp.errors.key.type) && repeatUrl) {
|
|
|
|
Object.assign(resp.errors.key.payload, {
|
|
|
|
repeatUrl
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-02 20:32:03 +03:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
export default connect((state) => ({
|
2016-05-28 00:36:10 +03:00
|
|
|
email: state.user.email,
|
|
|
|
lang: state.user.lang
|
2016-05-02 20:32:03 +03:00
|
|
|
}), {
|
2016-05-22 17:56:39 +03:00
|
|
|
})(ChangeEmailPage);
|