2019-01-28 00:42:58 +05:30
|
|
|
// @flow
|
2019-06-30 19:02:50 +05:30
|
|
|
import type { RouterHistory, Match } from 'react-router-dom';
|
|
|
|
import type FormModel from 'components/ui/form/FormModel';
|
2017-08-23 00:19:50 +05:30
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2019-06-30 19:02:50 +05:30
|
|
|
import { connect } from 'react-redux';
|
2016-05-02 23:02:03 +05:30
|
|
|
import ChangeEmail from 'components/profile/changeEmail/ChangeEmail';
|
2019-01-28 00:42:58 +05:30
|
|
|
import { requestEmailChange, setNewEmail, confirmNewEmail } from 'services/api/accounts';
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type OwnProps = {|
|
2019-01-28 00:42:58 +05:30
|
|
|
history: RouterHistory;
|
|
|
|
match: {
|
|
|
|
...Match;
|
|
|
|
params: {
|
|
|
|
step: 'step1' | 'step2' | 'step3';
|
|
|
|
code: string;
|
|
|
|
};
|
|
|
|
};
|
2019-06-30 19:02:50 +05:30
|
|
|
|};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
...OwnProps,
|
|
|
|
lang: string;
|
|
|
|
email: string;
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
2016-05-22 13:23:40 +05:30
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
class ChangeEmailPage extends Component<Props> {
|
2016-05-22 20:26:39 +05:30
|
|
|
static displayName = 'ChangeEmailPage';
|
2016-05-02 23:02:03 +05:30
|
|
|
|
2016-05-20 10:44:14 +05:30
|
|
|
static contextTypes = {
|
2019-01-28 00:42:58 +05:30
|
|
|
userId: PropTypes.number.isRequired,
|
2016-05-22 13:23:40 +05:30
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2019-01-28 00:42:58 +05:30
|
|
|
goToProfile: PropTypes.func.isRequired,
|
2016-05-02 23:02:03 +05:30
|
|
|
};
|
|
|
|
|
2016-05-20 10:44:14 +05:30
|
|
|
componentWillMount() {
|
2019-01-28 00:42:58 +05:30
|
|
|
const { step } = this.props.match.params;
|
2016-05-20 10:44:14 +05:30
|
|
|
|
2017-05-26 00:41:57 +05:30
|
|
|
if (step && !/^step[123]$/.test(step)) {
|
2016-05-20 10:44:14 +05:30
|
|
|
// wrong param value
|
2017-05-26 00:41:57 +05:30
|
|
|
this.props.history.push('/404');
|
2016-05-20 10:44:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-02 23:02:03 +05:30
|
|
|
render() {
|
2019-01-28 00:42:58 +05:30
|
|
|
const { step = 'step1', code } = this.props.match.params;
|
2016-05-20 10:44:14 +05:30
|
|
|
|
2016-05-02 23:02:03 +05:30
|
|
|
return (
|
2016-05-22 13:23:40 +05:30
|
|
|
<ChangeEmail
|
2016-05-02 23:02:03 +05:30
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
email={this.props.email}
|
2016-05-28 03:06:10 +05:30
|
|
|
lang={this.props.lang}
|
2019-01-28 00:42:58 +05:30
|
|
|
step={((step.slice(-1): any): number) * 1 - 1}
|
2016-05-20 10:44:14 +05:30
|
|
|
onChangeStep={this.onChangeStep}
|
|
|
|
code={code}
|
2016-05-02 23:02:03 +05:30
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onChangeStep = (step: number) => {
|
2017-05-26 00:41:57 +05:30
|
|
|
this.props.history.push(`/profile/change-email/step${++step}`);
|
2016-05-20 10:44:14 +05:30
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onSubmit = (step: number, form: FormModel) => {
|
2016-05-22 13:23:40 +05:30
|
|
|
return this.context.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData: () => {
|
2019-01-28 00:42:58 +05:30
|
|
|
const { userId } = this.context;
|
2016-05-22 13:23:40 +05:30
|
|
|
const data = form.serialize();
|
|
|
|
|
|
|
|
switch (step) {
|
|
|
|
case 0:
|
2019-01-28 00:42:58 +05:30
|
|
|
return requestEmailChange(userId, data.password).catch(handleErrors());
|
2016-05-22 13:23:40 +05:30
|
|
|
case 1:
|
2019-01-28 00:42:58 +05:30
|
|
|
return setNewEmail(userId, data.email, data.key).catch(handleErrors('/profile/change-email'));
|
2016-05-22 13:23:40 +05:30
|
|
|
case 2:
|
2019-01-28 00:42:58 +05:30
|
|
|
return confirmNewEmail(userId, data.key).catch(handleErrors('/profile/change-email'));
|
2016-05-22 13:23:40 +05:30
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported step ${step}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
step > 1 && this.context.goToProfile();
|
|
|
|
});
|
2016-05-02 23:02:03 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-22 20:46:51 +05:30
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
export default connect<Props, OwnProps, _, _, _, _>((state) => ({
|
2016-05-28 03:06:10 +05:30
|
|
|
email: state.user.email,
|
|
|
|
lang: state.user.lang
|
2019-06-30 19:02:50 +05:30
|
|
|
}))(ChangeEmailPage);
|