PromucFlow_constructor/app/client/src/pages/Editor/routes.tsx

102 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-10-18 08:16:26 +00:00
import React from "react";
import {
Route,
Switch,
withRouter,
RouteComponentProps,
} from "react-router-dom";
2019-11-25 09:15:11 +00:00
import ApiEditor from "./APIEditor";
2019-10-21 15:12:45 +00:00
import {
API_EDITOR_ID_URL,
API_EDITOR_URL,
BUILDER_PAGE_URL,
BUILDER_BASE_URL,
BuilderRouteParams,
APIEditorRouteParams,
2019-11-25 05:07:27 +00:00
} from "constants/routes";
2019-10-18 08:16:26 +00:00
import styled from "styled-components";
const Wrapper = styled.div<{ isVisible: boolean }>`
2019-10-18 08:16:26 +00:00
position: absolute;
top: 1px;
left: 1px;
width: 100%;
2019-10-18 08:16:26 +00:00
height: calc(100vh - ${props => props.theme.headerHeight});
background-color: ${props =>
props.isVisible ? "rgba(0, 0, 0, 0.26)" : "transparent"};
z-index: ${props => (props.isVisible ? 10 : -1)};
transition-property: z-index;
transition-delay: ${props => (props.isVisible ? "0" : "0.25s")};
`;
const DrawerWrapper = styled.div<{ isVisible: boolean }>`
background-color: white;
2019-11-13 11:34:11 +00:00
width: 75%;
height: 100%;
box-shadow: -1px 2px 3px 0px ${props => props.theme.colors.paneBG};
transform: translateX(${props => (props.isVisible ? `0` : `-80vw`)});
transition: 0.25s;
2019-10-18 08:16:26 +00:00
`;
interface RouterState {
isVisible: boolean;
2019-10-18 08:16:26 +00:00
}
class EditorsRouter extends React.Component<
RouteComponentProps<BuilderRouteParams>,
RouterState
> {
constructor(props: RouteComponentProps<APIEditorRouteParams>) {
2019-10-18 08:16:26 +00:00
super(props);
const { applicationId, pageId } = this.props.match.params;
2019-10-18 08:16:26 +00:00
this.state = {
isVisible:
this.props.location.pathname !== BUILDER_BASE_URL(applicationId) &&
this.props.location.pathname !==
BUILDER_PAGE_URL(applicationId, pageId),
2019-10-18 08:16:26 +00:00
};
}
componentDidUpdate(prevProps: Readonly<RouteComponentProps>): void {
if (this.props.location.pathname !== prevProps.location.pathname) {
const { applicationId, pageId } = this.props.match.params;
2019-10-18 08:16:26 +00:00
this.setState({
isVisible:
this.props.location.pathname !== BUILDER_BASE_URL(applicationId) &&
this.props.location.pathname !==
BUILDER_PAGE_URL(applicationId, pageId),
2019-10-18 08:16:26 +00:00
});
}
}
handleClose = (e: React.MouseEvent) => {
e.stopPropagation();
const { applicationId, pageId } = this.props.match.params;
this.setState({
isVisible: false,
});
this.props.history.replace(BUILDER_PAGE_URL(applicationId, pageId));
};
preventClose = (e: React.MouseEvent) => {
e.stopPropagation();
};
2019-10-18 08:16:26 +00:00
render(): React.ReactNode {
return (
<Wrapper isVisible={this.state.isVisible} onClick={this.handleClose}>
<DrawerWrapper
isVisible={this.state.isVisible}
onClick={this.preventClose}
2019-10-18 08:16:26 +00:00
>
<Switch>
<Route exact path={API_EDITOR_URL()} component={ApiEditor} />
<Route exact path={API_EDITOR_ID_URL()} component={ApiEditor} />
2019-10-18 08:16:26 +00:00
</Switch>
</DrawerWrapper>
</Wrapper>
2019-10-18 08:16:26 +00:00
);
}
}
export default withRouter(EditorsRouter);