2020-05-20 22:05:52 +05:30
|
|
|
import React, {
|
|
|
|
CSSProperties,
|
|
|
|
MouseEventHandler,
|
|
|
|
ReactElement,
|
|
|
|
ReactNode,
|
|
|
|
} from 'react';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { AccountsState } from 'app/components/accounts';
|
|
|
|
import { User } from 'app/components/user';
|
2016-02-13 20:58:47 +05:30
|
|
|
import { connect } from 'react-redux';
|
2020-01-18 02:07:52 +05:30
|
|
|
import {
|
|
|
|
TransitionMotion,
|
|
|
|
spring,
|
|
|
|
PlainStyle,
|
|
|
|
Style,
|
|
|
|
TransitionStyle,
|
|
|
|
TransitionPlainStyle,
|
|
|
|
} from 'react-motion';
|
2019-11-27 14:33:32 +05:30
|
|
|
import {
|
|
|
|
Panel,
|
|
|
|
PanelBody,
|
|
|
|
PanelFooter,
|
|
|
|
PanelHeader,
|
2019-12-08 00:32:00 +05:30
|
|
|
} from 'app/components/ui/Panel';
|
|
|
|
import { Form } from 'app/components/ui/form';
|
|
|
|
import MeasureHeight from 'app/components/MeasureHeight';
|
|
|
|
import panelStyles from 'app/components/ui/panel.scss';
|
|
|
|
import icons from 'app/components/ui/icons.scss';
|
|
|
|
import authFlow from 'app/services/authFlow';
|
2019-12-08 03:23:22 +05:30
|
|
|
import { RootState } from 'app/reducers';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
import { Provider as AuthContextProvider } from './Context';
|
2019-12-08 03:23:22 +05:30
|
|
|
import { getLogin, State as AuthState } from './reducer';
|
2016-02-13 20:58:47 +05:30
|
|
|
import * as actions from './actions';
|
2019-12-08 03:23:22 +05:30
|
|
|
import helpLinks from './helpLinks.scss';
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +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,
|
|
|
|
};
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-12-08 03:23:22 +05:30
|
|
|
const { helpLinks: helpLinksStyles } = helpLinks;
|
2019-12-07 16:58:52 +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)
|
|
|
|
*/
|
2020-01-18 02:07:52 +05:30
|
|
|
const contexts: Array<Array<PanelId>> = [
|
2019-11-27 14:33:32 +05:30
|
|
|
['login', 'password', 'forgotPassword', 'mfa', 'recoverPassword'],
|
|
|
|
['register', 'activation', 'resendActivation'],
|
|
|
|
['acceptRules'],
|
|
|
|
['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') {
|
2019-11-27 14:33:32 +05:30
|
|
|
// test panel uniquenes between contexts
|
|
|
|
// TODO: it may be moved to tests in future
|
|
|
|
|
|
|
|
contexts.reduce((acc, context) => {
|
2020-05-20 22:05:52 +05:30
|
|
|
context.forEach((panel) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (acc[panel]) {
|
|
|
|
throw new Error(
|
|
|
|
`Panel ${panel} is already exists in context ${JSON.stringify(
|
|
|
|
acc[panel],
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
}
|
2016-05-15 03:21:28 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
acc[panel] = context;
|
|
|
|
});
|
2016-05-15 03:21:28 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return acc;
|
2020-01-18 02:07:52 +05:30
|
|
|
}, {} as Record<string, Array<PanelId>>);
|
2016-05-15 03:21:28 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
type ValidationError =
|
|
|
|
| string
|
|
|
|
| {
|
2019-12-07 16:58:52 +05:30
|
|
|
type: string;
|
2020-01-18 02:07:52 +05:30
|
|
|
payload: Record<string, any>;
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface AnimationStyle extends PlainStyle {
|
2019-12-07 16:58:52 +05:30
|
|
|
opacitySpring: number;
|
|
|
|
transformSpring: number;
|
2020-01-18 02:07:52 +05:30
|
|
|
}
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface AnimationData {
|
|
|
|
Title: ReactElement;
|
|
|
|
Body: ReactElement;
|
|
|
|
Footer: ReactElement;
|
|
|
|
Links: ReactNode;
|
|
|
|
hasBackButton: boolean | ((props: Props) => boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AnimationContext extends TransitionPlainStyle {
|
2019-12-07 16:58:52 +05:30
|
|
|
key: PanelId;
|
2020-01-18 02:07:52 +05:30
|
|
|
style: AnimationStyle;
|
|
|
|
data: AnimationData;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface OwnProps {
|
|
|
|
Title: ReactElement;
|
|
|
|
Body: ReactElement;
|
|
|
|
Footer: ReactElement;
|
|
|
|
Links: ReactNode;
|
|
|
|
}
|
2019-06-30 19:02:50 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface Props extends OwnProps {
|
2019-11-27 14:33:32 +05:30
|
|
|
// context props
|
2019-12-07 16:58:52 +05:30
|
|
|
auth: AuthState;
|
|
|
|
user: User;
|
|
|
|
accounts: AccountsState;
|
|
|
|
clearErrors: () => void;
|
|
|
|
resolve: () => void;
|
|
|
|
reject: () => void;
|
2019-12-13 12:56:29 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
setErrors: (errors: Record<string, ValidationError>) => void;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
interface State {
|
2019-12-07 16:58:52 +05:30
|
|
|
contextHeight: number;
|
|
|
|
panelId: PanelId | void;
|
|
|
|
prevPanelId: PanelId | void;
|
|
|
|
isHeightDirty: boolean;
|
|
|
|
forceHeight: 1 | 0;
|
|
|
|
direction: 'X' | 'Y';
|
2020-01-18 02:07:52 +05:30
|
|
|
formsHeights: Record<PanelId, number>;
|
|
|
|
}
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
class PanelTransition extends React.PureComponent<Props, State> {
|
2019-12-07 16:58:52 +05:30
|
|
|
state: State = {
|
2019-11-27 14:33:32 +05:30
|
|
|
contextHeight: 0,
|
2019-12-07 16:58:52 +05:30
|
|
|
panelId: this.props.Body && (this.props.Body.type as any).panelId,
|
2019-11-27 14:33:32 +05:30
|
|
|
isHeightDirty: false,
|
2019-12-07 16:58:52 +05:30
|
|
|
forceHeight: 0 as const,
|
|
|
|
direction: 'X' as const,
|
2019-11-27 14:33:32 +05:30
|
|
|
prevPanelId: undefined,
|
2020-01-18 02:07:52 +05:30
|
|
|
formsHeights: {},
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
isHeightMeasured: boolean = false;
|
|
|
|
wasAutoFocused: boolean = false;
|
2020-01-18 02:07:52 +05:30
|
|
|
body: {
|
2019-12-07 16:58:52 +05:30
|
|
|
autoFocus: () => void;
|
|
|
|
onFormSubmit: () => void;
|
2020-01-18 02:07:52 +05:30
|
|
|
} | null = null;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
timerIds: Array<number> = []; // this is a list of a probably running timeouts to clean on unmount
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-11-27 14:33:32 +05:30
|
|
|
const nextPanel: PanelId =
|
2019-12-07 16:58:52 +05:30
|
|
|
this.props.Body && (this.props.Body.type as any).panelId;
|
2019-12-10 13:17:32 +05:30
|
|
|
const prevPanel: PanelId =
|
|
|
|
prevProps.Body && (prevProps.Body.type as any).panelId;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
if (nextPanel !== prevPanel) {
|
|
|
|
const direction = this.getDirection(nextPanel, prevPanel);
|
|
|
|
const forceHeight = direction === 'Y' && nextPanel !== prevPanel ? 1 : 0;
|
|
|
|
|
|
|
|
this.props.clearErrors();
|
2019-12-10 13:17:32 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
|
|
|
direction,
|
|
|
|
panelId: nextPanel,
|
|
|
|
prevPanelId: prevPanel,
|
|
|
|
forceHeight,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (forceHeight) {
|
|
|
|
this.timerIds.push(
|
2020-01-18 02:07:52 +05:30
|
|
|
// https://stackoverflow.com/a/51040768/5184751
|
|
|
|
window.setTimeout(() => {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({ forceHeight: 0 });
|
|
|
|
}, 100),
|
|
|
|
);
|
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
componentWillUnmount() {
|
2020-05-20 22:05:52 +05:30
|
|
|
this.timerIds.forEach((id) => clearTimeout(id));
|
2019-11-27 14:33:32 +05:30
|
|
|
this.timerIds = [];
|
|
|
|
}
|
2018-02-13 01:50:53 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
render() {
|
|
|
|
const { contextHeight, forceHeight } = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-12-13 12:56:29 +05:30
|
|
|
const {
|
|
|
|
Title,
|
|
|
|
Body,
|
|
|
|
Footer,
|
|
|
|
Links,
|
|
|
|
auth,
|
|
|
|
user,
|
|
|
|
clearErrors,
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
} = this.props;
|
2016-03-27 13:22:00 +05:30
|
|
|
|
2019-11-27 14:33:32 +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');
|
|
|
|
}
|
2016-03-15 11:10:18 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const {
|
|
|
|
panelId,
|
|
|
|
hasGoBack,
|
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
panelId: PanelId;
|
|
|
|
hasGoBack: boolean;
|
|
|
|
} = Body.type as any;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const formHeight = this.state.formsHeights[panelId] || 0;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
// a hack to disable height animation on first render
|
|
|
|
const { isHeightMeasured } = this;
|
|
|
|
this.isHeightMeasured = isHeightMeasured || formHeight > 0;
|
|
|
|
|
|
|
|
return (
|
2019-12-13 12:56:29 +05:30
|
|
|
<AuthContextProvider
|
|
|
|
value={{
|
|
|
|
auth,
|
|
|
|
user,
|
|
|
|
requestRedraw: this.requestRedraw,
|
|
|
|
clearErrors,
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<TransitionMotion
|
|
|
|
styles={[
|
|
|
|
{
|
|
|
|
key: panelId,
|
|
|
|
data: { Title, Body, Footer, Links, hasBackButton: hasGoBack },
|
|
|
|
style: {
|
|
|
|
transformSpring: spring(0, transformSpringConfig),
|
|
|
|
opacitySpring: spring(1, opacitySpringConfig),
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
2019-12-13 12:56:29 +05:30
|
|
|
{
|
|
|
|
key: 'common',
|
|
|
|
style: {
|
|
|
|
heightSpring: isHeightMeasured
|
|
|
|
? spring(forceHeight || formHeight, transformSpringConfig)
|
|
|
|
: formHeight,
|
|
|
|
switchContextHeightSpring: spring(
|
|
|
|
forceHeight || contextHeight,
|
|
|
|
changeContextSpringConfig,
|
|
|
|
),
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
2019-12-13 12:56:29 +05:30
|
|
|
]}
|
|
|
|
willEnter={this.willEnter}
|
|
|
|
willLeave={this.willLeave}
|
|
|
|
>
|
2020-05-20 22:05:52 +05:30
|
|
|
{(items) => {
|
2019-12-13 12:56:29 +05:30
|
|
|
const panels = items.filter(({ key }) => key !== 'common');
|
|
|
|
const [common] = items.filter(({ key }) => key === 'common');
|
|
|
|
|
|
|
|
const contentHeight = {
|
|
|
|
overflow: 'hidden',
|
|
|
|
height: forceHeight
|
|
|
|
? common.style.switchContextHeightSpring
|
|
|
|
: 'auto',
|
|
|
|
};
|
|
|
|
|
|
|
|
this.tryToAutoFocus(panels.length);
|
|
|
|
|
|
|
|
const bodyHeight = {
|
|
|
|
position: 'relative' as const,
|
|
|
|
height: `${common.style.heightSpring}px`,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
id={panelId}
|
|
|
|
onSubmit={this.onFormSubmit}
|
|
|
|
onInvalid={this.onFormInvalid}
|
|
|
|
isLoading={this.props.auth.isLoading}
|
|
|
|
>
|
|
|
|
<Panel>
|
|
|
|
<PanelHeader>
|
2020-05-20 22:05:52 +05:30
|
|
|
{panels.map((config) => this.getHeader(config))}
|
2019-12-13 12:56:29 +05:30
|
|
|
</PanelHeader>
|
|
|
|
<div style={contentHeight}>
|
|
|
|
<MeasureHeight
|
|
|
|
state={this.shouldMeasureHeight()}
|
|
|
|
onMeasure={this.onUpdateContextHeight}
|
|
|
|
>
|
|
|
|
<PanelBody>
|
|
|
|
<div style={bodyHeight}>
|
2020-05-20 22:05:52 +05:30
|
|
|
{panels.map((config) => this.getBody(config))}
|
2019-12-13 12:56:29 +05:30
|
|
|
</div>
|
|
|
|
</PanelBody>
|
|
|
|
<PanelFooter>
|
2020-05-20 22:05:52 +05:30
|
|
|
{panels.map((config) => this.getFooter(config))}
|
2019-12-13 12:56:29 +05:30
|
|
|
</PanelFooter>
|
|
|
|
</MeasureHeight>
|
|
|
|
</div>
|
|
|
|
</Panel>
|
2019-12-26 17:48:58 +05:30
|
|
|
<div
|
|
|
|
className={helpLinksStyles}
|
2019-12-28 02:10:26 +05:30
|
|
|
data-testid="auth-controls-secondary"
|
2019-12-26 17:48:58 +05:30
|
|
|
>
|
2020-05-20 22:05:52 +05:30
|
|
|
{panels.map((config) => this.getLinks(config))}
|
2019-11-27 14:33:32 +05:30
|
|
|
</div>
|
2019-12-13 12:56:29 +05:30
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TransitionMotion>
|
|
|
|
</AuthContextProvider>
|
2019-11-27 14:33:32 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
onFormSubmit = (): void => {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.props.clearErrors();
|
|
|
|
|
|
|
|
if (this.body) {
|
|
|
|
this.body.onFormSubmit();
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
onFormInvalid = (errors: Record<string, ValidationError>): void =>
|
2019-11-27 14:33:32 +05:30
|
|
|
this.props.setErrors(errors);
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
willEnter = (config: TransitionStyle): PlainStyle => {
|
|
|
|
const transform = this.getTransformForPanel(config.key);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
return {
|
|
|
|
transformSpring: transform,
|
|
|
|
opacitySpring: 1,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
willLeave = (config: TransitionStyle): Style => {
|
|
|
|
const transform = this.getTransformForPanel(config.key);
|
|
|
|
|
|
|
|
return {
|
|
|
|
transformSpring: spring(transform, transformSpringConfig),
|
|
|
|
opacitySpring: spring(0, opacitySpringConfig),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
getTransformForPanel(key: PanelId): number {
|
2019-11-27 14:33:32 +05:30
|
|
|
const { panelId, prevPanelId } = this.state;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const fromLeft = -1;
|
|
|
|
const fromRight = 1;
|
2016-05-15 03:21:28 +05:30
|
|
|
|
2020-05-20 22:05:52 +05:30
|
|
|
const currentContext = contexts.find((context) => context.includes(key));
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!currentContext) {
|
|
|
|
throw new Error(`Can not find settings for ${key} panel`);
|
2016-02-06 15:32:23 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
let sign =
|
2019-12-07 16:58:52 +05:30
|
|
|
prevPanelId &&
|
|
|
|
panelId &&
|
2019-11-27 14:33:32 +05:30
|
|
|
currentContext.indexOf(panelId) > currentContext.indexOf(prevPanelId)
|
|
|
|
? fromRight
|
|
|
|
: fromLeft;
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (prevPanelId === key) {
|
|
|
|
sign *= -1;
|
2016-03-01 00:06:23 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
return sign * 100;
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
getDirection(next: PanelId, prev: PanelId): 'X' | 'Y' {
|
2020-05-20 22:05:52 +05:30
|
|
|
const context = contexts.find((item) => item.includes(prev));
|
2016-03-28 10:35:18 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!context) {
|
|
|
|
throw new Error(`Can not find context for transition ${prev} -> ${next}`);
|
2016-03-28 10:35:18 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return context.includes(next) ? 'X' : 'Y';
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
onUpdateHeight = (height: number, key: PanelId): void => {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
2020-01-18 02:07:52 +05:30
|
|
|
formsHeights: {
|
|
|
|
...this.state.formsHeights,
|
|
|
|
[key]: height,
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
onUpdateContextHeight = (height: number): void => {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.setState({
|
|
|
|
contextHeight: height,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-05-20 22:29:06 +05:30
|
|
|
onGoBack: MouseEventHandler<HTMLButtonElement> = (event): void => {
|
2019-11-27 14:33:32 +05:30
|
|
|
event.preventDefault();
|
|
|
|
authFlow.goBack();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to auto focus form fields after transition end
|
|
|
|
*
|
|
|
|
* @param {number} length number of panels transitioned
|
|
|
|
*/
|
2020-01-18 02:07:52 +05:30
|
|
|
tryToAutoFocus(length: number): void {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!this.body) {
|
|
|
|
return;
|
2016-05-22 18:37:51 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (length === 1) {
|
|
|
|
if (!this.wasAutoFocused) {
|
|
|
|
this.body.autoFocus();
|
|
|
|
}
|
2017-01-29 21:06:21 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.wasAutoFocused = true;
|
|
|
|
} else if (this.wasAutoFocused) {
|
|
|
|
this.wasAutoFocused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
shouldMeasureHeight(): string {
|
2019-12-13 12:56:29 +05:30
|
|
|
const { user, accounts, auth } = this.props;
|
|
|
|
const { isHeightDirty } = this.state;
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const errorString = Object.values(auth.error || {}).reduce((acc, item) => {
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
return acc + item;
|
|
|
|
}
|
2017-01-29 21:06:21 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
return acc + item.type;
|
|
|
|
}, '') as string;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return [
|
|
|
|
errorString,
|
2019-12-13 12:56:29 +05:30
|
|
|
isHeightDirty,
|
|
|
|
user.lang,
|
|
|
|
accounts.available.length,
|
2019-11-27 14:33:32 +05:30
|
|
|
].join('');
|
|
|
|
}
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
getHeader({ key, style, data }: TransitionPlainStyle): ReactElement {
|
|
|
|
const { Title } = data as AnimationData;
|
|
|
|
const { transformSpring } = (style as unknown) as AnimationStyle;
|
2019-06-09 13:59:54 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
let { hasBackButton } = data;
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof hasBackButton === 'function') {
|
|
|
|
hasBackButton = hasBackButton(this.props);
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const transitionStyle = {
|
2020-01-18 02:07:52 +05:30
|
|
|
...this.getDefaultTransitionStyles(
|
|
|
|
key,
|
|
|
|
(style as unknown) as AnimationStyle,
|
|
|
|
),
|
2019-11-27 14:33:32 +05:30
|
|
|
opacity: 1, // reset default
|
|
|
|
};
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const scrollStyle = this.translate(transformSpring, 'Y');
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const sideScrollStyle = {
|
2019-12-07 16:58:52 +05:30
|
|
|
position: 'relative' as const,
|
2019-11-27 14:33:32 +05:30
|
|
|
zIndex: 2,
|
|
|
|
...this.translate(-Math.abs(transformSpring)),
|
|
|
|
};
|
2016-01-31 18:29:38 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
const backButton = (
|
|
|
|
<button
|
|
|
|
style={sideScrollStyle}
|
|
|
|
className={panelStyles.headerControl}
|
|
|
|
data-e2e-go-back
|
|
|
|
type="button"
|
|
|
|
onClick={this.onGoBack}
|
|
|
|
>
|
|
|
|
<span className={icons.arrowLeft} />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`header/${key}`} style={transitionStyle}>
|
|
|
|
{hasBackButton ? backButton : null}
|
|
|
|
<div style={scrollStyle}>{Title}</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
getBody({ key, style, data }: TransitionPlainStyle): ReactElement {
|
|
|
|
const { Body } = data as AnimationData;
|
|
|
|
const { transformSpring } = (style as unknown) as AnimationStyle;
|
2019-11-27 14:33:32 +05:30
|
|
|
const { direction } = this.state;
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
let transform = this.translate(transformSpring, direction);
|
2019-11-27 14:33:32 +05:30
|
|
|
let verticalOrigin = 'top';
|
|
|
|
|
|
|
|
if (direction === 'Y') {
|
|
|
|
verticalOrigin = 'bottom';
|
|
|
|
transform = {};
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const transitionStyle: CSSProperties = {
|
|
|
|
...this.getDefaultTransitionStyles(
|
|
|
|
key,
|
|
|
|
(style as unknown) as AnimationStyle,
|
|
|
|
),
|
2019-11-27 14:33:32 +05:30
|
|
|
top: 'auto', // reset default
|
|
|
|
[verticalOrigin]: 0,
|
|
|
|
...transform,
|
|
|
|
};
|
2016-02-06 15:32:23 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return (
|
|
|
|
<MeasureHeight
|
|
|
|
key={`body/${key}`}
|
|
|
|
style={transitionStyle}
|
|
|
|
state={this.shouldMeasureHeight()}
|
2020-05-20 22:05:52 +05:30
|
|
|
onMeasure={(height) => this.onUpdateHeight(height, key)}
|
2019-11-27 14:33:32 +05:30
|
|
|
>
|
|
|
|
{React.cloneElement(Body, {
|
2020-01-18 02:07:52 +05:30
|
|
|
// @ts-ignore
|
2020-05-20 22:05:52 +05:30
|
|
|
ref: (body) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.body = body;
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
</MeasureHeight>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
getFooter({ key, style, data }: TransitionPlainStyle): ReactElement {
|
|
|
|
const { Footer } = data as AnimationData;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const transitionStyle = this.getDefaultTransitionStyles(
|
|
|
|
key,
|
|
|
|
(style as unknown) as AnimationStyle,
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`footer/${key}`} style={transitionStyle}>
|
|
|
|
{Footer}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
getLinks({ key, style, data }: TransitionPlainStyle): ReactElement {
|
|
|
|
const { Links } = data as AnimationData;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
const transitionStyle = this.getDefaultTransitionStyles(
|
|
|
|
key,
|
|
|
|
(style as unknown) as AnimationStyle,
|
|
|
|
);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return (
|
|
|
|
<div key={`links/${key}`} style={transitionStyle}>
|
|
|
|
{Links}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultTransitionStyles(
|
|
|
|
key: string,
|
2020-01-18 02:07:52 +05:30
|
|
|
{ opacitySpring }: Readonly<AnimationStyle>,
|
2019-12-07 16:58:52 +05:30
|
|
|
): {
|
|
|
|
position: 'absolute';
|
|
|
|
top: number;
|
|
|
|
left: number;
|
|
|
|
width: string;
|
|
|
|
opacity: number;
|
|
|
|
pointerEvents: 'none' | 'auto';
|
|
|
|
} {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
opacity: opacitySpring,
|
|
|
|
pointerEvents: key === this.state.panelId ? 'auto' : 'none',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
translate(
|
|
|
|
value: number,
|
|
|
|
direction: 'X' | 'Y' = 'X',
|
|
|
|
unit: '%' | 'px' = '%',
|
|
|
|
): CSSProperties {
|
2019-11-27 14:33:32 +05:30
|
|
|
return {
|
|
|
|
WebkitTransform: `translate${direction}(${value}${unit})`,
|
|
|
|
transform: `translate${direction}(${value}${unit})`,
|
|
|
|
};
|
|
|
|
}
|
2019-12-13 12:56:29 +05:30
|
|
|
|
|
|
|
requestRedraw = (): Promise<void> =>
|
2020-05-20 22:05:52 +05:30
|
|
|
new Promise((resolve) =>
|
2019-12-13 12:56:29 +05:30
|
|
|
this.setState({ isHeightDirty: true }, () => {
|
|
|
|
this.setState({ isHeightDirty: false });
|
|
|
|
|
|
|
|
// wait till transition end
|
|
|
|
this.timerIds.push(setTimeout(resolve, 200));
|
|
|
|
}),
|
|
|
|
);
|
2016-01-31 18:29:38 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default connect(
|
|
|
|
(state: RootState) => {
|
2017-08-23 00:09:08 +05:30
|
|
|
const login = getLogin(state);
|
2016-11-19 20:11:15 +05:30
|
|
|
let user = {
|
2019-11-27 14:33:32 +05:30
|
|
|
...state.user,
|
2016-11-13 02:01:44 +05:30
|
|
|
};
|
|
|
|
|
2016-11-19 20:11:15 +05:30
|
|
|
if (login) {
|
2019-11-27 14:33:32 +05:30
|
|
|
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 {
|
2019-11-27 14:33:32 +05:30
|
|
|
user,
|
|
|
|
accounts: state.accounts, // need this, to re-render height
|
|
|
|
auth: state.auth,
|
|
|
|
resolve: authFlow.resolve.bind(authFlow),
|
|
|
|
reject: authFlow.reject.bind(authFlow),
|
2016-11-13 02:01:44 +05:30
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
{
|
2016-02-13 20:58:47 +05:30
|
|
|
clearErrors: actions.clearErrors,
|
2019-11-27 14:33:32 +05:30
|
|
|
setErrors: actions.setErrors,
|
|
|
|
},
|
|
|
|
)(PanelTransition);
|