2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { Route, Switch, Redirect } from 'react-router-dom';
|
2017-09-09 19:52:19 +05:30
|
|
|
import { connect } from 'react-redux';
|
2019-12-29 17:27:44 +05:30
|
|
|
import { refreshUserData } from 'app/components/accounts/actions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { create as createPopup } from 'app/components/ui/popup/actions';
|
|
|
|
import PasswordRequestForm from 'app/components/profile/passwordRequestForm/PasswordRequestForm';
|
|
|
|
import logger from 'app/services/logger';
|
|
|
|
import { browserHistory } from 'app/services/history';
|
|
|
|
import { FooterMenu } from 'app/components/footerMenu';
|
|
|
|
import { FormModel } from 'app/components/ui/form';
|
2020-01-18 02:07:52 +05:30
|
|
|
import { Dispatch, RootState } from 'app/reducers';
|
2019-12-12 12:56:23 +05:30
|
|
|
import { Provider } from 'app/components/profile/Context';
|
2019-12-30 13:45:40 +05:30
|
|
|
import { ComponentLoader } from 'app/components/ui/loader';
|
2017-05-26 00:41:57 +05:30
|
|
|
|
2016-04-17 15:05:04 +05:30
|
|
|
import styles from './profile.scss';
|
|
|
|
|
2019-12-30 13:45:40 +05:30
|
|
|
const Profile = React.lazy(() =>
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "page-profile-index" */ 'app/components/profile/Profile'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const ChangePasswordPage = React.lazy(() =>
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "page-profile-change-password" */ 'app/pages/profile/ChangePasswordPage'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const ChangeUsernamePage = React.lazy(() =>
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "page-profile-change-username" */ 'app/pages/profile/ChangeUsernamePage'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const ChangeEmailPage = React.lazy(() =>
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "page-profile-change-email" */ 'app/pages/profile/ChangeEmailPage'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
const MultiFactorAuthPage = React.lazy(() =>
|
|
|
|
import(
|
|
|
|
/* webpackChunkName: "page-profile-mfa" */ 'app/pages/profile/MultiFactorAuthPage'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface Props {
|
|
|
|
userId: number;
|
|
|
|
onSubmit: (options: {
|
|
|
|
form: FormModel;
|
|
|
|
sendData: () => Promise<any>;
|
2019-12-12 12:56:23 +05:30
|
|
|
}) => Promise<void>;
|
2019-12-29 17:27:44 +05:30
|
|
|
refreshUserData: () => Promise<any>;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
2019-01-28 00:42:58 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
class ProfilePage extends React.Component<Props> {
|
2019-11-27 14:33:32 +05:30
|
|
|
render() {
|
2019-12-12 12:56:23 +05:30
|
|
|
const { userId, onSubmit } = this.props;
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2019-12-12 12:56:23 +05:30
|
|
|
<Provider
|
|
|
|
value={{
|
|
|
|
userId,
|
|
|
|
onSubmit,
|
|
|
|
goToProfile: this.goToProfile,
|
|
|
|
}}
|
|
|
|
>
|
2019-12-30 13:45:40 +05:30
|
|
|
<React.Suspense fallback={<ComponentLoader />}>
|
|
|
|
<Switch>
|
|
|
|
<Route
|
|
|
|
path="/profile/mfa/step:step([1-3])"
|
|
|
|
component={MultiFactorAuthPage}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/profile/mfa"
|
|
|
|
exact
|
|
|
|
component={MultiFactorAuthPage}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/profile/change-password"
|
|
|
|
exact
|
|
|
|
component={ChangePasswordPage}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/profile/change-username"
|
|
|
|
exact
|
|
|
|
component={ChangeUsernamePage}
|
|
|
|
/>
|
|
|
|
<Route
|
|
|
|
path="/profile/change-email/:step?/:code?"
|
|
|
|
component={ChangeEmailPage}
|
|
|
|
/>
|
|
|
|
<Route path="/profile" exact component={Profile} />
|
|
|
|
<Route path="/" exact component={Profile} />
|
|
|
|
<Redirect to="/404" />
|
|
|
|
</Switch>
|
|
|
|
</React.Suspense>
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-12 12:56:23 +05:30
|
|
|
<div className={styles.footer}>
|
|
|
|
<FooterMenu />
|
|
|
|
</div>
|
|
|
|
</Provider>
|
2019-11-27 14:33:32 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-12 12:56:23 +05:30
|
|
|
goToProfile = async () => {
|
2019-12-29 17:27:44 +05:30
|
|
|
await this.props.refreshUserData();
|
2019-12-12 12:56:23 +05:30
|
|
|
|
|
|
|
browserHistory.push('/');
|
|
|
|
};
|
2016-04-17 15:05:04 +05:30
|
|
|
}
|
2016-05-12 10:00:10 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default connect(
|
|
|
|
(state: RootState) => ({
|
2020-01-18 02:07:52 +05:30
|
|
|
userId: state.user.id!,
|
2019-11-27 14:33:32 +05:30
|
|
|
}),
|
|
|
|
{
|
2019-12-29 17:27:44 +05:30
|
|
|
refreshUserData,
|
2019-11-27 14:33:32 +05:30
|
|
|
onSubmit: ({
|
|
|
|
form,
|
|
|
|
sendData,
|
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
form: FormModel;
|
|
|
|
sendData: () => Promise<any>;
|
2020-01-18 02:07:52 +05:30
|
|
|
}) => (dispatch: Dispatch) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
form.beginLoading();
|
|
|
|
|
|
|
|
return sendData()
|
2020-05-20 22:05:52 +05:30
|
|
|
.catch((resp) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const requirePassword = resp.errors && !!resp.errors.password;
|
|
|
|
|
|
|
|
// prevalidate user input, because requestPassword popup will block the
|
|
|
|
// entire form from input, so it must be valid
|
|
|
|
if (resp.errors) {
|
|
|
|
delete resp.errors.password;
|
|
|
|
|
|
|
|
if (resp.errors.email && resp.data && resp.data.canRepeatIn) {
|
|
|
|
resp.errors.email = {
|
|
|
|
type: resp.errors.email,
|
|
|
|
payload: {
|
|
|
|
msLeft: resp.data.canRepeatIn * 1000,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(resp.errors).length) {
|
|
|
|
form.setErrors(resp.errors);
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requirePassword) {
|
|
|
|
return requestPassword(form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
})
|
2020-05-20 22:05:52 +05:30
|
|
|
.catch((resp) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!resp || !resp.errors) {
|
|
|
|
logger.warn('Unexpected profile editing error', {
|
|
|
|
resp,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject(resp);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => form.endLoading());
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
function requestPassword(form: FormModel) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
dispatch(
|
|
|
|
createPopup({
|
2019-12-07 16:58:52 +05:30
|
|
|
Popup(props: { onClose: () => Promise<any> }) {
|
2019-11-27 14:33:32 +05:30
|
|
|
const onSubmit = () => {
|
|
|
|
form.beginLoading();
|
|
|
|
|
|
|
|
sendData()
|
|
|
|
.then(resolve)
|
|
|
|
.then(props.onClose)
|
2020-05-20 22:05:52 +05:30
|
|
|
.catch((resp) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (resp.errors) {
|
2016-05-12 10:00:10 +05:30
|
|
|
form.setErrors(resp.errors);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
const parentFormHasErrors =
|
|
|
|
Object.keys(resp.errors).filter(
|
2020-05-20 22:05:52 +05:30
|
|
|
(name) => name !== 'password',
|
2019-11-27 14:33:32 +05:30
|
|
|
).length > 0;
|
|
|
|
|
|
|
|
if (parentFormHasErrors) {
|
2019-12-29 17:27:44 +05:30
|
|
|
// something wrong with parent form, hiding popup and show that form
|
2019-11-27 14:33:32 +05:30
|
|
|
props.onClose();
|
|
|
|
reject(resp);
|
|
|
|
logger.warn(
|
2019-12-29 17:27:44 +05:30
|
|
|
'Profile: can not submit password popup due to errors in source form',
|
2019-11-27 14:33:32 +05:30
|
|
|
{ resp },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-12 10:00:10 +05:30
|
|
|
return Promise.reject(resp);
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => form.endLoading());
|
|
|
|
};
|
|
|
|
|
|
|
|
return <PasswordRequestForm form={form} onSubmit={onSubmit} />;
|
|
|
|
},
|
|
|
|
disableOverlayClose: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)(ProfilePage);
|