2019-06-09 13:59:54 +05:30
|
|
|
// @flow
|
|
|
|
import type { User } from 'components/user';
|
|
|
|
import type { AccountsState } from 'components/accounts';
|
2019-06-30 19:02:50 +05:30
|
|
|
import type { Element } from 'react';
|
2017-08-23 00:09:08 +05:30
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
import { connect } from 'react-redux';
|
2016-01-31 18:29:38 +05:30
|
|
|
import { TransitionMotion, spring } from 'react-motion';
|
|
|
|
|
|
|
|
import { Panel, PanelBody, PanelFooter, PanelHeader } from 'components/ui/Panel';
|
2017-08-23 00:09:08 +05:30
|
|
|
import { getLogin } from 'components/auth/reducer';
|
2016-05-02 12:45:42 +05:30
|
|
|
import { Form } from 'components/ui/form';
|
2016-05-02 22:55:14 +05:30
|
|
|
import MeasureHeight from 'components/MeasureHeight';
|
2016-05-15 03:21:28 +05:30
|
|
|
import { helpLinks as helpLinksStyles } from 'components/auth/helpLinks.scss';
|
2016-01-31 18:29:38 +05:30
|
|
|
import panelStyles from 'components/ui/panel.scss';
|
|
|
|
import icons from 'components/ui/icons.scss';
|
2016-03-02 02:06:14 +05:30
|
|
|
import authFlow from 'services/authFlow';
|
2016-03-13 14:06:31 +05:30
|
|
|
import { userShape } from 'components/user/User';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
import * as actions from './actions';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const opacitySpringConfig = {stiffness: 300, damping: 20};
|
2016-03-28 09:53:45 +05:30
|
|
|
const transformSpringConfig = {stiffness: 500, damping: 50, precision: 0.5};
|
|
|
|
const changeContextSpringConfig = {stiffness: 500, damping: 20, precision: 0.5};
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type PanelId = string;
|
|
|
|
|
2016-05-15 03:21:28 +05:30
|
|
|
/**
|
|
|
|
* Definition of relation between contexts and panels
|
|
|
|
*
|
|
|
|
* Each sub-array is context. Each sub-array item is panel
|
|
|
|
*
|
|
|
|
* This definition declares animations between panels:
|
|
|
|
* - The animation between panels from different contexts will be along Y axe (height toggling)
|
|
|
|
* - The animation between panels from the same context will be along X axe (sliding)
|
|
|
|
* - Panel index defines the direction of X transition of both panels
|
|
|
|
* (e.g. the panel with lower index will slide from left side, and with greater from right side)
|
|
|
|
*/
|
2019-06-30 19:02:50 +05:30
|
|
|
const contexts: Array<PanelId[]> = [
|
2017-10-17 22:16:00 +05:30
|
|
|
['login', 'password', 'forgotPassword', 'mfa', 'recoverPassword'],
|
2016-05-23 00:28:43 +05:30
|
|
|
['register', 'activation', 'resendActivation'],
|
2016-08-03 00:29:29 +05:30
|
|
|
['acceptRules'],
|
2016-11-06 01:53:56 +05:30
|
|
|
['chooseAccount', 'permissions']
|
2016-05-15 03:21:28 +05:30
|
|
|
];
|
|
|
|
|
2017-06-13 01:02:59 +05:30
|
|
|
// eslint-disable-next-line
|
2016-05-15 03:21:28 +05:30
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
// test panel uniquenes between contexts
|
|
|
|
// TODO: it may be moved to tests in future
|
|
|
|
|
|
|
|
contexts.reduce((acc, context) => {
|
|
|
|
context.forEach((panel) => {
|
|
|
|
if (acc[panel]) {
|
|
|
|
throw new Error(`Panel ${panel} is already exists in context ${JSON.stringify(acc[panel])}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[panel] = context;
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
type ValidationError = string | {
|
|
|
|
type: string,
|
|
|
|
payload: { [key: string]: any },
|
|
|
|
};
|
|
|
|
|
|
|
|
type AnimationProps = {|
|
|
|
|
opacitySpring: number,
|
|
|
|
transformSpring: number,
|
|
|
|
|};
|
|
|
|
|
|
|
|
type AnimationContext = {
|
2019-06-30 19:02:50 +05:30
|
|
|
key: PanelId,
|
2019-06-09 13:59:54 +05:30
|
|
|
style: AnimationProps,
|
|
|
|
data: {
|
2019-06-30 19:02:50 +05:30
|
|
|
Title: Element<any>,
|
2019-06-09 13:59:54 +05:30
|
|
|
Body: Element<any>,
|
2019-06-30 19:02:50 +05:30
|
|
|
Footer: Element<any>,
|
|
|
|
Links: Element<any>,
|
2019-06-09 13:59:54 +05:30
|
|
|
hasBackButton: bool,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type OwnProps = {|
|
|
|
|
Title: Element<any>,
|
|
|
|
Body: Element<any>,
|
|
|
|
Footer: Element<any>,
|
|
|
|
Links: Element<any>,
|
|
|
|
children?: Element<any>
|
|
|
|
|};
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
type Props = {
|
2019-06-30 19:02:50 +05:30
|
|
|
...OwnProps,
|
2019-06-09 13:59:54 +05:30
|
|
|
// context props
|
|
|
|
auth: {
|
|
|
|
error: string | {
|
|
|
|
type: string,
|
|
|
|
payload: {[key: string]: any},
|
|
|
|
},
|
|
|
|
isLoading: bool,
|
|
|
|
login: string,
|
|
|
|
},
|
|
|
|
user: User,
|
|
|
|
accounts: AccountsState,
|
|
|
|
setErrors: ({ [key: string]: ValidationError }) => void,
|
|
|
|
clearErrors: () => void,
|
|
|
|
resolve: () => void,
|
|
|
|
reject: () => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
contextHeight: number,
|
2019-06-30 19:02:50 +05:30
|
|
|
panelId: PanelId | void,
|
|
|
|
prevPanelId: PanelId | void,
|
2019-06-09 13:59:54 +05:30
|
|
|
isHeightDirty: bool,
|
|
|
|
forceHeight: 1 | 0,
|
|
|
|
direction: 'X' | 'Y',
|
|
|
|
};
|
|
|
|
|
|
|
|
class PanelTransition extends Component<Props, State> {
|
2016-02-13 20:58:47 +05:30
|
|
|
static displayName = 'PanelTransition';
|
|
|
|
|
|
|
|
static propTypes = {
|
2016-03-13 14:06:31 +05:30
|
|
|
// context props
|
2016-02-13 20:58:47 +05:30
|
|
|
auth: PropTypes.shape({
|
2016-05-22 19:31:31 +05:30
|
|
|
error: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
payload: PropTypes.object
|
|
|
|
})]),
|
2016-04-02 16:28:54 +05:30
|
|
|
isLoading: PropTypes.bool,
|
2016-11-13 20:17:56 +05:30
|
|
|
login: PropTypes.string
|
2016-02-13 20:58:47 +05:30
|
|
|
}).isRequired,
|
2016-03-13 14:06:31 +05:30
|
|
|
user: userShape.isRequired,
|
2017-01-27 11:58:15 +05:30
|
|
|
accounts: PropTypes.shape({
|
|
|
|
available: PropTypes.array
|
|
|
|
}),
|
2016-08-23 00:48:11 +05:30
|
|
|
setErrors: PropTypes.func.isRequired,
|
2016-05-12 10:00:10 +05:30
|
|
|
clearErrors: PropTypes.func.isRequired,
|
|
|
|
resolve: PropTypes.func.isRequired,
|
|
|
|
reject: PropTypes.func.isRequired,
|
2016-03-13 14:06:31 +05:30
|
|
|
|
|
|
|
// local props
|
2016-03-15 11:10:18 +05:30
|
|
|
Title: PropTypes.element,
|
|
|
|
Body: PropTypes.element,
|
|
|
|
Footer: PropTypes.element,
|
|
|
|
Links: PropTypes.element,
|
|
|
|
children: PropTypes.element
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
2016-03-13 14:06:31 +05:30
|
|
|
static childContextTypes = {
|
|
|
|
auth: PropTypes.shape({
|
2016-05-22 19:31:31 +05:30
|
|
|
error: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
|
|
|
|
type: PropTypes.string,
|
|
|
|
payload: PropTypes.object
|
|
|
|
})]),
|
2016-11-13 20:17:56 +05:30
|
|
|
login: PropTypes.string
|
2016-03-13 14:06:31 +05:30
|
|
|
}),
|
|
|
|
user: userShape,
|
2016-11-19 18:07:17 +05:30
|
|
|
accounts: PropTypes.shape({
|
|
|
|
available: PropTypes.array
|
|
|
|
}),
|
2016-05-22 18:37:51 +05:30
|
|
|
requestRedraw: PropTypes.func,
|
2016-05-12 10:00:10 +05:30
|
|
|
clearErrors: PropTypes.func,
|
2016-03-13 14:06:31 +05:30
|
|
|
resolve: PropTypes.func,
|
|
|
|
reject: PropTypes.func
|
|
|
|
};
|
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
state = {
|
2016-05-14 18:08:38 +05:30
|
|
|
contextHeight: 0,
|
2019-06-09 13:59:54 +05:30
|
|
|
panelId: this.props.Body && (this.props.Body: any).type.panelId,
|
|
|
|
isHeightDirty: false,
|
|
|
|
forceHeight: 0,
|
|
|
|
direction: 'X',
|
|
|
|
prevPanelId: undefined,
|
2016-03-28 09:16:51 +05:30
|
|
|
};
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
isHeightMeasured: bool = false;
|
|
|
|
wasAutoFocused: bool = false;
|
|
|
|
body: null | {
|
|
|
|
autoFocus: () => void,
|
|
|
|
onFormSubmit: () => void,
|
|
|
|
} = null;
|
|
|
|
|
2018-02-13 01:50:53 +05:30
|
|
|
timerIds = []; // this is a list of a probably running timeouts to clean on unmount
|
|
|
|
|
2016-03-13 14:06:31 +05:30
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
auth: this.props.auth,
|
|
|
|
user: this.props.user,
|
2019-06-30 19:02:50 +05:30
|
|
|
requestRedraw: (): Promise<void> =>
|
2018-01-01 22:30:38 +05:30
|
|
|
new Promise((resolve) =>
|
|
|
|
this.setState(
|
|
|
|
{isHeightDirty: true},
|
|
|
|
() => {
|
|
|
|
this.setState({isHeightDirty: false});
|
|
|
|
|
|
|
|
// wait till transition end
|
2018-02-13 01:50:53 +05:30
|
|
|
this.timerIds.push(
|
|
|
|
setTimeout(resolve, 200)
|
|
|
|
);
|
2018-01-01 22:30:38 +05:30
|
|
|
}
|
|
|
|
)
|
|
|
|
),
|
2016-03-13 14:06:31 +05:30
|
|
|
clearErrors: this.props.clearErrors,
|
|
|
|
resolve: this.props.resolve,
|
|
|
|
reject: this.props.reject
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
componentWillReceiveProps(nextProps: Props) {
|
|
|
|
const nextPanel: PanelId = nextProps.Body && (nextProps.Body: any).type.panelId;
|
|
|
|
const prevPanel: PanelId = this.props.Body && (this.props.Body: any).type.panelId;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-05-14 18:08:38 +05:30
|
|
|
if (nextPanel !== prevPanel) {
|
|
|
|
const direction = this.getDirection(nextPanel, prevPanel);
|
|
|
|
const forceHeight = direction === 'Y' && nextPanel !== prevPanel ? 1 : 0;
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
this.props.clearErrors();
|
|
|
|
this.setState({
|
|
|
|
direction,
|
2016-05-14 18:08:38 +05:30
|
|
|
panelId: nextPanel,
|
|
|
|
prevPanelId: prevPanel,
|
2016-03-28 09:16:51 +05:30
|
|
|
forceHeight
|
2016-02-13 20:58:47 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
if (forceHeight) {
|
2018-02-13 01:50:53 +05:30
|
|
|
this.timerIds.push(
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({forceHeight: 0});
|
|
|
|
}, 100)
|
|
|
|
);
|
2016-02-13 20:58:47 +05:30
|
|
|
}
|
2016-02-03 11:06:00 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2018-02-13 01:50:53 +05:30
|
|
|
componentWillUnmount() {
|
|
|
|
this.timerIds.forEach((id) => clearTimeout(id));
|
|
|
|
this.timerIds = [];
|
|
|
|
}
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
render() {
|
2016-05-02 22:59:50 +05:30
|
|
|
const {contextHeight, forceHeight} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-28 09:16:51 +05:30
|
|
|
const {Title, Body, Footer, Links} = this.props;
|
2016-03-27 13:22:00 +05:30
|
|
|
|
2016-03-15 11:10:18 +05:30
|
|
|
if (this.props.children) {
|
|
|
|
return this.props.children;
|
|
|
|
} else if (!Title || !Body || !Footer || !Links) {
|
|
|
|
throw new Error('Title, Body, Footer and Links are required');
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
const {panelId, hasGoBack}: {
|
2019-06-30 19:02:50 +05:30
|
|
|
panelId: PanelId,
|
2019-06-09 13:59:54 +05:30
|
|
|
hasGoBack: bool,
|
|
|
|
} = (Body: any).type;
|
2016-03-28 09:16:51 +05:30
|
|
|
|
|
|
|
const formHeight = this.state[`formHeight${panelId}`] || 0;
|
|
|
|
|
2016-05-02 22:59:50 +05:30
|
|
|
// a hack to disable height animation on first render
|
|
|
|
const isHeightMeasured = this.isHeightMeasured;
|
2016-05-02 23:22:37 +05:30
|
|
|
this.isHeightMeasured = isHeightMeasured || formHeight > 0;
|
2016-05-02 22:59:50 +05:30
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
return (
|
|
|
|
<TransitionMotion
|
2016-03-03 10:53:17 +05:30
|
|
|
styles={[
|
2016-03-28 09:16:51 +05:30
|
|
|
{key: panelId, data: {Title, Body, Footer, Links, hasBackButton: hasGoBack}, style: {
|
2016-02-03 11:06:00 +05:30
|
|
|
transformSpring: spring(0, transformSpringConfig),
|
|
|
|
opacitySpring: spring(1, opacitySpringConfig)
|
2016-03-03 10:53:17 +05:30
|
|
|
}},
|
|
|
|
{key: 'common', style: {
|
2016-05-02 22:59:50 +05:30
|
|
|
heightSpring: isHeightMeasured ? spring(forceHeight || formHeight, transformSpringConfig) : formHeight,
|
2016-02-06 15:32:23 +05:30
|
|
|
switchContextHeightSpring: spring(forceHeight || contextHeight, changeContextSpringConfig)
|
2016-03-03 10:53:17 +05:30
|
|
|
}}
|
|
|
|
]}
|
2016-01-31 18:29:38 +05:30
|
|
|
willEnter={this.willEnter}
|
|
|
|
willLeave={this.willLeave}
|
|
|
|
>
|
|
|
|
{(items) => {
|
2016-03-03 10:53:17 +05:30
|
|
|
const panels = items.filter(({key}) => key !== 'common');
|
|
|
|
const common = items.filter(({key}) => key === 'common')[0];
|
2016-02-13 20:58:47 +05:30
|
|
|
|
|
|
|
const contentHeight = {
|
|
|
|
overflow: 'hidden',
|
2016-03-12 19:53:55 +05:30
|
|
|
height: forceHeight ? common.style.switchContextHeightSpring : 'auto'
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
|
|
|
|
2016-03-28 10:35:18 +05:30
|
|
|
this.tryToAutoFocus(panels.length);
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
const bodyHeight = {
|
|
|
|
position: 'relative',
|
2016-05-02 22:59:50 +05:30
|
|
|
height: `${common.style.heightSpring}px`
|
2016-02-13 20:58:47 +05:30
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
return (
|
2016-04-02 16:28:54 +05:30
|
|
|
<Form
|
|
|
|
id={panelId}
|
|
|
|
onSubmit={this.onFormSubmit}
|
|
|
|
onInvalid={this.onFormInvalid}
|
|
|
|
isLoading={this.props.auth.isLoading}
|
|
|
|
>
|
2016-01-31 18:29:38 +05:30
|
|
|
<Panel>
|
|
|
|
<PanelHeader>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getHeader(config))}
|
2016-01-31 18:29:38 +05:30
|
|
|
</PanelHeader>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={contentHeight}>
|
2016-03-27 13:22:00 +05:30
|
|
|
<MeasureHeight
|
2016-05-22 18:37:51 +05:30
|
|
|
state={this.shouldMeasureHeight()}
|
2016-03-27 13:22:00 +05:30
|
|
|
onMeasure={this.onUpdateContextHeight}
|
|
|
|
>
|
2016-02-06 15:32:23 +05:30
|
|
|
<PanelBody>
|
2016-02-13 20:58:47 +05:30
|
|
|
<div style={bodyHeight}>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getBody(config))}
|
2016-02-06 14:33:51 +05:30
|
|
|
</div>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelFooter>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getFooter(config))}
|
2016-02-06 14:33:51 +05:30
|
|
|
</PanelFooter>
|
2016-03-27 13:22:00 +05:30
|
|
|
</MeasureHeight>
|
2016-02-06 14:33:51 +05:30
|
|
|
</div>
|
2016-01-31 18:29:38 +05:30
|
|
|
</Panel>
|
2016-02-06 15:32:23 +05:30
|
|
|
<div className={helpLinksStyles}>
|
2016-03-03 10:53:17 +05:30
|
|
|
{panels.map((config) => this.getLinks(config))}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
2016-02-13 20:58:47 +05:30
|
|
|
</Form>
|
2016-01-31 18:29:38 +05:30
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TransitionMotion>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:58:47 +05:30
|
|
|
onFormSubmit = () => {
|
2016-05-01 12:10:35 +05:30
|
|
|
this.props.clearErrors();
|
2019-06-09 13:59:54 +05:30
|
|
|
|
|
|
|
if (this.body) {
|
|
|
|
this.body.onFormSubmit();
|
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onFormInvalid = (errors: { [key: string]: ValidationError }) => this.props.setErrors(errors);
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
willEnter = (config: AnimationContext) => this.getTransitionStyles(config);
|
|
|
|
willLeave = (config: AnimationContext) => this.getTransitionStyles(config, {isLeave: true});
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2016-02-06 15:32:23 +05:30
|
|
|
/**
|
2016-08-14 15:40:59 +05:30
|
|
|
* @param {object} config
|
|
|
|
* @param {string} config.key
|
|
|
|
* @param {object} [options]
|
|
|
|
* @param {object} [options.isLeave=false] - true, if this is a leave transition
|
2016-02-06 15:32:23 +05:30
|
|
|
*
|
2016-08-14 15:40:59 +05:30
|
|
|
* @return {object}
|
2016-02-06 15:32:23 +05:30
|
|
|
*/
|
2019-06-30 19:02:50 +05:30
|
|
|
getTransitionStyles({key}: AnimationContext, options: { isLeave?: bool } = {}): {|
|
2019-06-09 13:59:54 +05:30
|
|
|
transformSpring: number,
|
|
|
|
opacitySpring: number,
|
|
|
|
|} {
|
2016-03-03 10:53:17 +05:30
|
|
|
const {isLeave = false} = options;
|
2016-05-14 18:08:38 +05:30
|
|
|
const {panelId, prevPanelId} = this.state;
|
|
|
|
|
|
|
|
const fromLeft = -1;
|
|
|
|
const fromRight = 1;
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2016-05-15 03:21:28 +05:30
|
|
|
const currentContext = contexts.find((context) => context.includes(key));
|
2019-06-09 13:59:54 +05:30
|
|
|
|
|
|
|
if (!currentContext) {
|
|
|
|
throw new Error(`Can not find settings for ${key} panel`);
|
|
|
|
}
|
|
|
|
|
2016-05-15 03:21:28 +05:30
|
|
|
let sign = currentContext.indexOf(panelId) > currentContext.indexOf(prevPanelId)
|
2017-08-23 02:30:10 +05:30
|
|
|
? fromRight
|
|
|
|
: fromLeft;
|
2016-05-15 03:21:28 +05:30
|
|
|
if (prevPanelId === key) {
|
|
|
|
sign *= -1;
|
|
|
|
}
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const transform = sign * 100;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return {
|
2016-03-03 10:53:17 +05:30
|
|
|
transformSpring: isLeave ? spring(transform, transformSpringConfig) : transform,
|
|
|
|
opacitySpring: isLeave ? spring(0, opacitySpringConfig) : 1
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
getDirection(next: PanelId, prev: PanelId): 'X' | 'Y' {
|
2019-06-09 13:59:54 +05:30
|
|
|
const context = contexts.find((context) => context.includes(prev));
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
throw new Error(`Can not find context for transition ${prev} -> ${next}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.includes(next) ? 'X' : 'Y';
|
2016-03-01 00:06:23 +05:30
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onUpdateHeight = (height: number, key: PanelId) => {
|
2016-03-27 13:22:00 +05:30
|
|
|
const heightKey = `formHeight${key}`;
|
|
|
|
|
2016-01-31 18:29:38 +05:30
|
|
|
this.setState({
|
2016-03-27 13:22:00 +05:30
|
|
|
[heightKey]: height
|
2016-01-31 18:29:38 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onUpdateContextHeight = (height: number) => {
|
2016-02-06 14:33:51 +05:30
|
|
|
this.setState({
|
|
|
|
contextHeight: height
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
onGoBack = (event: SyntheticEvent<HTMLElement>) => {
|
2016-01-31 18:29:38 +05:30
|
|
|
event.preventDefault();
|
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
authFlow.goBack();
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-03-28 10:35:18 +05:30
|
|
|
/**
|
|
|
|
* Tries to auto focus form fields after transition end
|
|
|
|
*
|
2016-08-14 15:40:59 +05:30
|
|
|
* @param {number} length number of panels transitioned
|
2016-03-28 10:35:18 +05:30
|
|
|
*/
|
2019-06-30 19:02:50 +05:30
|
|
|
tryToAutoFocus(length: number) {
|
2016-03-28 10:35:18 +05:30
|
|
|
if (!this.body) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length === 1) {
|
|
|
|
if (!this.wasAutoFocused) {
|
|
|
|
this.body.autoFocus();
|
|
|
|
}
|
|
|
|
this.wasAutoFocused = true;
|
|
|
|
} else if (this.wasAutoFocused) {
|
|
|
|
this.wasAutoFocused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 18:37:51 +05:30
|
|
|
shouldMeasureHeight() {
|
2019-06-09 13:24:03 +05:30
|
|
|
const errorString = Object.values(this.props.auth.error || {})
|
2019-06-09 13:59:54 +05:30
|
|
|
.reduce(
|
|
|
|
// $FlowFixMe
|
|
|
|
(acc, item: ValidationError) => {
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
return acc + item;
|
|
|
|
}
|
2019-06-09 13:24:03 +05:30
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
return acc + item.type;
|
|
|
|
},
|
|
|
|
''
|
|
|
|
);
|
2019-06-09 13:24:03 +05:30
|
|
|
|
2016-11-19 18:07:17 +05:30
|
|
|
return [
|
2019-06-09 13:24:03 +05:30
|
|
|
errorString,
|
2016-11-19 18:07:17 +05:30
|
|
|
this.state.isHeightDirty,
|
|
|
|
this.props.user.lang,
|
|
|
|
this.props.accounts.available.length
|
|
|
|
].join('');
|
2016-05-22 18:37:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
getHeader({key, style, data}: AnimationContext) {
|
2017-01-29 21:06:21 +05:30
|
|
|
const {Title} = data;
|
2016-03-03 10:53:17 +05:30
|
|
|
const {transformSpring} = style;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2017-01-29 21:06:21 +05:30
|
|
|
let {hasBackButton} = data;
|
|
|
|
|
|
|
|
if (typeof hasBackButton === 'function') {
|
|
|
|
hasBackButton = hasBackButton(this.props);
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
const transitionStyle = {
|
2016-03-03 10:53:17 +05:30
|
|
|
...this.getDefaultTransitionStyles(key, style),
|
2016-02-06 15:32:23 +05:30
|
|
|
opacity: 1 // reset default
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const scrollStyle = this.translate(transformSpring, 'Y');
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const sideScrollStyle = {
|
2016-01-31 18:29:38 +05:30
|
|
|
position: 'relative',
|
|
|
|
zIndex: 2,
|
2016-02-06 15:32:23 +05:30
|
|
|
...this.translate(-Math.abs(transformSpring))
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
const backButton = (
|
2016-08-14 15:40:59 +05:30
|
|
|
<button style={sideScrollStyle}
|
|
|
|
className={panelStyles.headerControl}
|
2018-02-18 23:39:32 +05:30
|
|
|
data-e2e-go-back
|
2016-08-14 15:40:59 +05:30
|
|
|
type="button"
|
|
|
|
onClick={this.onGoBack}
|
|
|
|
>
|
2016-01-31 18:29:38 +05:30
|
|
|
<span className={icons.arrowLeft} />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2019-06-09 13:59:54 +05:30
|
|
|
<div key={`header/${key}`} style={transitionStyle}>
|
2016-01-31 18:29:38 +05:30
|
|
|
{hasBackButton ? backButton : null}
|
|
|
|
<div style={scrollStyle}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Title}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
getBody({key, style, data}: AnimationContext) {
|
2016-03-03 10:53:17 +05:30
|
|
|
const {Body} = data;
|
|
|
|
const {transformSpring} = style;
|
|
|
|
const {direction} = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2016-03-03 10:53:17 +05:30
|
|
|
let transform = this.translate(transformSpring, direction);
|
|
|
|
let verticalOrigin = 'top';
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2016-02-06 14:33:51 +05:30
|
|
|
if (direction === 'Y') {
|
2016-02-02 23:32:00 +05:30
|
|
|
verticalOrigin = 'bottom';
|
2016-02-06 14:33:51 +05:30
|
|
|
transform = {};
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
const transitionStyle = {
|
2016-03-03 10:53:17 +05:30
|
|
|
...this.getDefaultTransitionStyles(key, style),
|
2016-02-06 15:32:23 +05:30
|
|
|
top: 'auto', // reset default
|
2016-02-02 23:32:00 +05:30
|
|
|
[verticalOrigin]: 0,
|
2016-02-06 14:33:51 +05:30
|
|
|
...transform
|
2016-01-31 18:29:38 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2016-03-27 13:22:00 +05:30
|
|
|
<MeasureHeight
|
2016-03-28 09:16:51 +05:30
|
|
|
key={`body/${key}`}
|
2019-06-09 13:59:54 +05:30
|
|
|
style={transitionStyle}
|
2016-05-22 18:37:51 +05:30
|
|
|
state={this.shouldMeasureHeight()}
|
2016-03-27 13:22:00 +05:30
|
|
|
onMeasure={(height) => this.onUpdateHeight(height, key)}
|
|
|
|
>
|
2016-02-13 20:58:47 +05:30
|
|
|
{React.cloneElement(Body, {
|
|
|
|
ref: (body) => {
|
|
|
|
this.body = body;
|
|
|
|
}
|
|
|
|
})}
|
2016-03-27 13:22:00 +05:30
|
|
|
</MeasureHeight>
|
2016-01-31 18:29:38 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
getFooter({key, style, data}: AnimationContext) {
|
2016-03-03 10:53:17 +05:30
|
|
|
const {Footer} = data;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
const transitionStyle = this.getDefaultTransitionStyles(key, style);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
2019-06-09 13:59:54 +05:30
|
|
|
<div key={`footer/${key}`} style={transitionStyle}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Footer}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
getLinks({key, style, data}: AnimationContext) {
|
2016-03-03 10:53:17 +05:30
|
|
|
const {Links} = data;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-06-09 13:59:54 +05:30
|
|
|
const transitionStyle = this.getDefaultTransitionStyles(key, style);
|
2016-01-31 18:29:38 +05:30
|
|
|
|
|
|
|
return (
|
2019-06-09 13:59:54 +05:30
|
|
|
<div key={`links/${key}`} style={transitionStyle}>
|
2016-03-13 14:06:31 +05:30
|
|
|
{Links}
|
2016-01-31 18:29:38 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-02-06 15:32:23 +05:30
|
|
|
|
|
|
|
/**
|
2016-08-14 15:40:59 +05:30
|
|
|
* @param {string} key
|
|
|
|
* @param {object} style
|
|
|
|
* @param {number} style.opacitySpring
|
2016-02-06 15:32:23 +05:30
|
|
|
*
|
2016-08-14 15:40:59 +05:30
|
|
|
* @return {object}
|
2016-02-06 15:32:23 +05:30
|
|
|
*/
|
2019-06-09 13:59:54 +05:30
|
|
|
getDefaultTransitionStyles(key: string, {opacitySpring}: $ReadOnly<AnimationProps>): {|
|
|
|
|
position: string,
|
|
|
|
top: number,
|
|
|
|
left: number,
|
|
|
|
width: string,
|
|
|
|
opacity: number,
|
|
|
|
pointerEvents: string,
|
|
|
|
|} {
|
2016-02-06 15:32:23 +05:30
|
|
|
return {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
opacity: opacitySpring,
|
2016-05-14 18:08:38 +05:30
|
|
|
pointerEvents: key === this.state.panelId ? 'auto' : 'none'
|
2016-02-06 15:32:23 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-14 15:40:59 +05:30
|
|
|
* @param {number} value
|
|
|
|
* @param {string} direction='X' - X|Y
|
|
|
|
* @param {string} unit='%' - %|px etc
|
2016-02-06 15:32:23 +05:30
|
|
|
*
|
2016-08-14 15:40:59 +05:30
|
|
|
* @return {object}
|
2016-02-06 15:32:23 +05:30
|
|
|
*/
|
2019-06-30 19:02:50 +05:30
|
|
|
translate(value: number, direction: 'X' | 'Y' = 'X', unit: '%' | 'px' = '%') {
|
2016-02-06 15:32:23 +05:30
|
|
|
return {
|
|
|
|
WebkitTransform: `translate${direction}(${value}${unit})`,
|
|
|
|
transform: `translate${direction}(${value}${unit})`
|
|
|
|
};
|
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
export default connect<Props, OwnProps, _, _, _, _>((state) => {
|
2017-08-23 00:09:08 +05:30
|
|
|
const login = getLogin(state);
|
2016-11-19 20:11:15 +05:30
|
|
|
let user = {
|
|
|
|
...state.user
|
2016-11-13 02:01:44 +05:30
|
|
|
};
|
|
|
|
|
2016-11-19 20:11:15 +05:30
|
|
|
if (login) {
|
|
|
|
user = {
|
|
|
|
...user,
|
|
|
|
isGuest: true,
|
|
|
|
email: '',
|
|
|
|
username: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
if (/[@.]/.test(login)) {
|
|
|
|
user.email = login;
|
|
|
|
} else {
|
|
|
|
user.username = login;
|
|
|
|
}
|
2016-11-13 02:01:44 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
user,
|
2016-11-19 18:07:17 +05:30
|
|
|
accounts: state.accounts, // need this, to re-render height
|
2016-11-13 02:01:44 +05:30
|
|
|
auth: state.auth,
|
|
|
|
resolve: authFlow.resolve.bind(authFlow),
|
|
|
|
reject: authFlow.reject.bind(authFlow)
|
|
|
|
};
|
|
|
|
}, {
|
2016-02-13 20:58:47 +05:30
|
|
|
clearErrors: actions.clearErrors,
|
2016-08-23 00:48:11 +05:30
|
|
|
setErrors: actions.setErrors
|
2016-02-13 20:58:47 +05:30
|
|
|
})(PanelTransition);
|