2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-12-10 13:17:32 +05:30
|
|
|
import { RouteComponentProps, Redirect } from 'react-router-dom';
|
2020-07-22 15:31:12 +05:30
|
|
|
|
|
|
|
import { connect } from 'app/functions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import FormModel from 'app/components/ui/form/FormModel';
|
2020-05-24 04:38:24 +05:30
|
|
|
import ChangeEmail, { ChangeEmailStep } from 'app/components/profile/changeEmail/ChangeEmail';
|
|
|
|
import { requestEmailChange, setNewEmail, confirmNewEmail } from 'app/services/api/accounts';
|
2019-12-12 12:56:23 +05:30
|
|
|
import Context from 'app/components/profile/Context';
|
2019-01-28 00:42:58 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface RouteParams {
|
2020-05-24 04:38:24 +05:30
|
|
|
step: 'step1' | 'step2' | 'step3';
|
|
|
|
code: string;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
2016-05-22 13:23:40 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface Props extends RouteComponentProps<RouteParams> {
|
2020-05-24 04:38:24 +05:30
|
|
|
email: string;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
2016-05-02 23:02:03 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
class ChangeEmailPage extends React.Component<Props> {
|
2020-05-24 04:38:24 +05:30
|
|
|
static contextType = Context;
|
2020-10-11 22:03:55 +05:30
|
|
|
declare context: React.ContextType<typeof Context>;
|
2016-05-02 23:02:03 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
const { step = 'step1', code } = this.props.match.params;
|
2016-05-20 10:44:14 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (step && !/^step[123]$/.test(step)) {
|
|
|
|
// wrong param value
|
|
|
|
return <Redirect to="/404" />;
|
|
|
|
}
|
2016-05-20 10:44:14 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return (
|
|
|
|
<ChangeEmail
|
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
email={this.props.email}
|
|
|
|
step={(Number(step.slice(-1)) - 1) as ChangeEmailStep}
|
|
|
|
onChangeStep={this.onChangeStep}
|
|
|
|
code={code}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2016-05-02 23:02:03 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onChangeStep = (step: number) => {
|
|
|
|
this.props.history.push(`/profile/change-email/step${++step}`);
|
|
|
|
};
|
2016-05-20 10:44:14 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onSubmit = (step: number, form: FormModel): Promise<void> => {
|
|
|
|
return this.context
|
|
|
|
.onSubmit({
|
|
|
|
form,
|
|
|
|
sendData: () => {
|
|
|
|
const { userId } = this.context;
|
|
|
|
const data = form.serialize();
|
2016-05-22 13:23:40 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
switch (step) {
|
|
|
|
case 0:
|
|
|
|
return requestEmailChange(userId, data.password).catch(handleErrors());
|
|
|
|
case 1:
|
|
|
|
return setNewEmail(userId, data.email, data.key).catch(
|
|
|
|
handleErrors('/profile/change-email'),
|
|
|
|
);
|
|
|
|
case 2:
|
|
|
|
return confirmNewEmail(userId, data.key).catch(handleErrors('/profile/change-email'));
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported step ${step}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
step > 1 && this.context.goToProfile();
|
|
|
|
});
|
|
|
|
};
|
2016-05-02 23:02:03 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
function handleErrors(repeatUrl?: string): <T extends { errors: Record<string, any> }>(resp: T) => Promise<T> {
|
|
|
|
return (resp) => {
|
|
|
|
if (resp.errors) {
|
|
|
|
if (resp.errors.key) {
|
|
|
|
resp.errors.key = {
|
|
|
|
type: resp.errors.key,
|
|
|
|
payload: {},
|
|
|
|
};
|
2016-05-22 20:46:51 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (['error.key_not_exists', 'error.key_expire'].includes(resp.errors.key.type) && repeatUrl) {
|
|
|
|
Object.assign(resp.errors.key.payload, {
|
|
|
|
repeatUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-05-22 20:46:51 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.reject(resp);
|
|
|
|
};
|
2016-05-22 20:46:51 +05:30
|
|
|
}
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export default connect((state) => ({
|
2020-05-24 04:38:24 +05:30
|
|
|
email: state.user.email,
|
2019-06-30 19:02:50 +05:30
|
|
|
}))(ChangeEmailPage);
|