accounts-frontend/src/components/auth/PanelTransition.js

492 lines
15 KiB
JavaScript
Raw Normal View History

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { TransitionMotion, spring } from 'react-motion';
import { Panel, PanelBody, PanelFooter, PanelHeader } from 'components/ui/Panel';
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';
import { helpLinks as helpLinksStyles } from 'components/auth/helpLinks.scss';
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';
import { userShape } from 'components/user/User';
import * as actions from './actions';
2016-03-03 10:53:17 +05:30
const opacitySpringConfig = {stiffness: 300, damping: 20};
const transformSpringConfig = {stiffness: 500, damping: 50, precision: 0.5};
const changeContextSpringConfig = {stiffness: 500, damping: 20, precision: 0.5};
/**
* 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)
*/
const contexts = [
['login', 'password', 'forgotPassword', 'recoverPassword'],
['register', 'activation', 'resendActivation'],
2016-08-03 00:29:29 +05:30
['acceptRules'],
2016-11-06 01:53:56 +05:30
['chooseAccount', 'permissions']
];
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;
}, {});
}
class PanelTransition extends Component {
static displayName = 'PanelTransition';
static propTypes = {
// context props
auth: PropTypes.shape({
error: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
type: PropTypes.string,
payload: PropTypes.object
})]),
isLoading: PropTypes.bool,
2016-11-13 20:17:56 +05:30
login: PropTypes.string
}).isRequired,
user: userShape.isRequired,
2017-01-27 11:58:15 +05:30
accounts: PropTypes.shape({
available: PropTypes.array
}),
setErrors: PropTypes.func.isRequired,
clearErrors: PropTypes.func.isRequired,
resolve: PropTypes.func.isRequired,
reject: PropTypes.func.isRequired,
// local props
Title: PropTypes.element,
Body: PropTypes.element,
Footer: PropTypes.element,
Links: PropTypes.element,
children: PropTypes.element
};
static childContextTypes = {
auth: PropTypes.shape({
error: PropTypes.oneOfType([PropTypes.string, PropTypes.shape({
type: PropTypes.string,
payload: PropTypes.object
})]),
2016-11-13 20:17:56 +05:30
login: PropTypes.string
}),
user: userShape,
accounts: PropTypes.shape({
available: PropTypes.array
}),
requestRedraw: PropTypes.func,
clearErrors: PropTypes.func,
resolve: PropTypes.func,
reject: PropTypes.func
};
state = {
contextHeight: 0,
panelId: this.props.Body && this.props.Body.type.panelId
};
getChildContext() {
return {
auth: this.props.auth,
user: this.props.user,
requestRedraw: () => this.setState({isHeightDirty: true}, () => this.setState({isHeightDirty: false})),
clearErrors: this.props.clearErrors,
resolve: this.props.resolve,
reject: this.props.reject
};
}
componentWillReceiveProps(nextProps) {
const nextPanel = nextProps.Body && nextProps.Body.type.panelId;
const prevPanel = this.props.Body && this.props.Body.type.panelId;
if (nextPanel !== prevPanel) {
const direction = this.getDirection(nextPanel, prevPanel);
const forceHeight = direction === 'Y' && nextPanel !== prevPanel ? 1 : 0;
this.props.clearErrors();
this.setState({
direction,
panelId: nextPanel,
prevPanelId: prevPanel,
forceHeight
});
if (forceHeight) {
setTimeout(() => {
this.setState({forceHeight: 0});
}, 100);
}
}
}
render() {
const {contextHeight, forceHeight} = this.state;
const {Title, Body, Footer, Links} = this.props;
if (this.props.children) {
return this.props.children;
} else if (!Title || !Body || !Footer || !Links) {
throw new Error('Title, Body, Footer and Links are required');
}
const {panelId, hasGoBack} = Body.type;
const formHeight = this.state[`formHeight${panelId}`] || 0;
// 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;
return (
<TransitionMotion
2016-03-03 10:53:17 +05:30
styles={[
{key: panelId, data: {Title, Body, Footer, Links, hasBackButton: hasGoBack}, style: {
transformSpring: spring(0, transformSpringConfig),
opacitySpring: spring(1, opacitySpringConfig)
2016-03-03 10:53:17 +05:30
}},
{key: 'common', style: {
heightSpring: isHeightMeasured ? spring(forceHeight || formHeight, transformSpringConfig) : formHeight,
switchContextHeightSpring: spring(forceHeight || contextHeight, changeContextSpringConfig)
2016-03-03 10:53:17 +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];
const contentHeight = {
overflow: 'hidden',
height: forceHeight ? common.style.switchContextHeightSpring : 'auto'
};
this.tryToAutoFocus(panels.length);
const bodyHeight = {
position: 'relative',
height: `${common.style.heightSpring}px`
};
return (
<Form
id={panelId}
onSubmit={this.onFormSubmit}
onInvalid={this.onFormInvalid}
isLoading={this.props.auth.isLoading}
>
<Panel>
<PanelHeader>
2016-03-03 10:53:17 +05:30
{panels.map((config) => this.getHeader(config))}
</PanelHeader>
<div style={contentHeight}>
<MeasureHeight
state={this.shouldMeasureHeight()}
onMeasure={this.onUpdateContextHeight}
>
<PanelBody>
<div style={bodyHeight}>
2016-03-03 10:53:17 +05:30
{panels.map((config) => this.getBody(config))}
</div>
</PanelBody>
<PanelFooter>
2016-03-03 10:53:17 +05:30
{panels.map((config) => this.getFooter(config))}
</PanelFooter>
</MeasureHeight>
</div>
</Panel>
<div className={helpLinksStyles}>
2016-03-03 10:53:17 +05:30
{panels.map((config) => this.getLinks(config))}
</div>
</Form>
);
}}
</TransitionMotion>
);
}
onFormSubmit = () => {
2016-05-01 12:10:35 +05:30
this.props.clearErrors();
this.body.onFormSubmit();
};
onFormInvalid = (errors) => this.props.setErrors(errors);
2016-03-03 10:53:17 +05:30
willEnter = (config) => this.getTransitionStyles(config);
willLeave = (config) => this.getTransitionStyles(config, {isLeave: true});
/**
* @param {object} config
* @param {string} config.key
* @param {object} [options]
* @param {object} [options.isLeave=false] - true, if this is a leave transition
*
* @return {object}
*/
2016-03-03 10:53:17 +05:30
getTransitionStyles({key}, options = {}) {
const {isLeave = false} = options;
const {panelId, prevPanelId} = this.state;
const fromLeft = -1;
const fromRight = 1;
const currentContext = contexts.find((context) => context.includes(key));
let sign = currentContext.indexOf(panelId) > currentContext.indexOf(prevPanelId)
? fromRight
: fromLeft;
if (prevPanelId === key) {
sign *= -1;
}
2016-03-03 10:53:17 +05:30
const transform = sign * 100;
return {
2016-03-03 10:53:17 +05:30
transformSpring: isLeave ? spring(transform, transformSpringConfig) : transform,
opacitySpring: isLeave ? spring(0, opacitySpringConfig) : 1
};
}
getDirection(next, prev) {
return contexts.find((context) => context.includes(prev)).includes(next) ? 'X' : 'Y';
}
onUpdateHeight = (height, key) => {
const heightKey = `formHeight${key}`;
this.setState({
[heightKey]: height
});
};
onUpdateContextHeight = (height) => {
this.setState({
contextHeight: height
});
};
onGoBack = (event) => {
event.preventDefault();
2016-03-02 02:06:14 +05:30
authFlow.goBack();
};
/**
* Tries to auto focus form fields after transition end
*
* @param {number} length number of panels transitioned
*/
tryToAutoFocus(length) {
if (!this.body) {
return;
}
if (length === 1) {
if (!this.wasAutoFocused) {
this.body.autoFocus();
}
this.wasAutoFocused = true;
} else if (this.wasAutoFocused) {
this.wasAutoFocused = false;
}
}
shouldMeasureHeight() {
return [
this.props.auth.error,
this.state.isHeightDirty,
this.props.user.lang,
this.props.accounts.available.length
].join('');
}
2016-03-03 10:53:17 +05:30
getHeader({key, style, data}) {
const {Title} = data;
2016-03-03 10:53:17 +05:30
const {transformSpring} = style;
let {hasBackButton} = data;
if (typeof hasBackButton === 'function') {
hasBackButton = hasBackButton(this.props);
}
2016-03-03 10:53:17 +05:30
style = {
...this.getDefaultTransitionStyles(key, style),
opacity: 1 // reset default
};
2016-03-03 10:53:17 +05:30
const scrollStyle = this.translate(transformSpring, 'Y');
2016-03-03 10:53:17 +05:30
const sideScrollStyle = {
position: 'relative',
zIndex: 2,
...this.translate(-Math.abs(transformSpring))
};
2016-03-03 10:53:17 +05:30
const backButton = (
<button style={sideScrollStyle}
className={panelStyles.headerControl}
type="button"
onClick={this.onGoBack}
>
<span className={icons.arrowLeft} />
</button>
);
return (
<div key={`header/${key}`} style={style}>
{hasBackButton ? backButton : null}
<div style={scrollStyle}>
{Title}
</div>
</div>
);
}
2016-03-03 10:53:17 +05:30
getBody({key, style, data}) {
const {Body} = data;
const {transformSpring} = style;
const {direction} = this.state;
2016-03-03 10:53:17 +05:30
let transform = this.translate(transformSpring, direction);
let verticalOrigin = 'top';
if (direction === 'Y') {
verticalOrigin = 'bottom';
transform = {};
}
2016-03-03 10:53:17 +05:30
style = {
...this.getDefaultTransitionStyles(key, style),
top: 'auto', // reset default
[verticalOrigin]: 0,
...transform
};
return (
<MeasureHeight
key={`body/${key}`}
style={style}
state={this.shouldMeasureHeight()}
onMeasure={(height) => this.onUpdateHeight(height, key)}
>
{React.cloneElement(Body, {
ref: (body) => {
this.body = body;
}
})}
</MeasureHeight>
);
}
2016-03-03 10:53:17 +05:30
getFooter({key, style, data}) {
const {Footer} = data;
2016-03-03 10:53:17 +05:30
style = this.getDefaultTransitionStyles(key, style);
return (
<div key={`footer/${key}`} style={style}>
{Footer}
</div>
);
}
2016-03-03 10:53:17 +05:30
getLinks({key, style, data}) {
const {Links} = data;
2016-03-03 10:53:17 +05:30
style = this.getDefaultTransitionStyles(key, style);
return (
<div key={`links/${key}`} style={style}>
{Links}
</div>
);
}
/**
* @param {string} key
* @param {object} style
* @param {number} style.opacitySpring
*
* @return {object}
*/
2016-03-03 10:53:17 +05:30
getDefaultTransitionStyles(key, {opacitySpring}) {
return {
position: 'absolute',
top: 0,
left: 0,
width: '100%',
opacity: opacitySpring,
pointerEvents: key === this.state.panelId ? 'auto' : 'none'
};
}
/**
* @param {number} value
* @param {string} direction='X' - X|Y
* @param {string} unit='%' - %|px etc
*
* @return {object}
*/
translate(value, direction = 'X', unit = '%') {
return {
WebkitTransform: `translate${direction}(${value}${unit})`,
transform: `translate${direction}(${value}${unit})`
};
}
}
export default connect((state) => {
const {login} = state.auth;
let user = {
...state.user
};
if (login) {
user = {
...user,
isGuest: true,
email: '',
username: ''
};
if (/[@.]/.test(login)) {
user.email = login;
} else {
user.username = login;
}
}
return {
user,
accounts: state.accounts, // need this, to re-render height
auth: state.auth,
resolve: authFlow.resolve.bind(authFlow),
reject: authFlow.reject.bind(authFlow)
};
}, {
clearErrors: actions.clearErrors,
setErrors: actions.setErrors
})(PanelTransition);