PromucFlow_constructor/app/client/src/pages/AppViewer/index.tsx

156 lines
4.6 KiB
TypeScript
Raw Normal View History

import React, { Component } from "react";
import styled from "styled-components";
import { connect } from "react-redux";
import { withRouter, RouteComponentProps } from "react-router";
import { Switch, Route } from "react-router-dom";
2019-11-25 05:07:27 +00:00
import { AppState } from "reducers";
import {
AppViewerRouteParams,
2020-02-18 10:41:52 +00:00
BuilderRouteParams,
getApplicationViewerPageURL,
} from "constants/routes";
import {
ReduxActionTypes,
PageListPayload,
2019-11-25 05:07:27 +00:00
} from "constants/ReduxActionConstants";
import {
getPageList,
2019-11-06 06:21:56 +00:00
getCurrentDSLPageId,
2020-01-24 09:54:40 +00:00
getIsInitialized,
2019-11-25 05:07:27 +00:00
} from "selectors/appViewSelectors";
import { executeAction } from "actions/widgetActions";
2020-02-18 10:41:52 +00:00
import { ExecuteActionPayload } from "constants/ActionConstants";
import SideNav from "./viewer/SideNav";
import { SideNavItemProps } from "./viewer/SideNavItem";
2019-11-04 10:57:19 +00:00
import AppViewerHeader from "./viewer/AppViewerHeader";
2019-11-25 05:07:27 +00:00
import { updateWidgetProperty } from "actions/controlActions";
import { RenderModes } from "constants/WidgetConstants";
import { EditorContext } from "components/editorComponents/EditorContextProvider";
import AppViewerPageContainer from "./AppViewerPageContainer";
import AppViewerSideNavWrapper from "./viewer/AppViewerSideNavWrapper";
2020-02-07 02:32:52 +00:00
import { updateWidgetMetaProperty } from "actions/metaActions";
const AppViewWrapper = styled.div`
margin-top: ${props => props.theme.headerHeight};
background: white;
`;
const AppViewerBody = styled.section`
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: flex-start;
2019-11-01 05:28:56 +00:00
height: calc(100vh - ${props => props.theme.headerHeight});
`;
2019-11-01 05:28:56 +00:00
export type AppViewerProps = {
2019-11-06 06:21:56 +00:00
currentDSLPageId?: string;
currentLayoutId?: string;
pages?: PageListPayload;
initializeAppViewer: Function;
2020-01-24 09:54:40 +00:00
isInitialized: boolean;
2020-02-18 10:41:52 +00:00
executeAction: (actionPayload: ExecuteActionPayload) => void;
2019-11-08 11:02:00 +00:00
updateWidgetProperty: (
widgetId: string,
propertyName: string,
propertyValue: any,
) => void;
2020-02-07 02:32:52 +00:00
updateWidgetMetaProperty: (
widgetId: string,
propertyName: string,
propertyValue: any,
) => void;
2020-02-18 10:41:52 +00:00
} & RouteComponentProps<BuilderRouteParams>;
class AppViewer extends Component<
AppViewerProps & RouteComponentProps<AppViewerRouteParams>
> {
componentDidMount() {
const { applicationId } = this.props.match.params;
if (this.props.match.params.applicationId) {
this.props.initializeAppViewer(applicationId);
2019-11-06 06:21:56 +00:00
}
}
2020-02-18 10:41:52 +00:00
public render() {
2020-01-24 09:54:40 +00:00
const { isInitialized } = this.props;
if (!isInitialized) return null;
const items: SideNavItemProps[] | undefined =
this.props.pages &&
this.props.pages.map(page => ({
text: page.pageName,
id: page.pageId,
2019-11-01 05:28:56 +00:00
icon: "page-layout", //TODO: get the icon from page.
path: getApplicationViewerPageURL(
this.props.match.params.applicationId,
page.pageId,
),
loading: false,
}));
return (
<EditorContext.Provider
2019-11-08 11:02:00 +00:00
value={{
executeAction: this.props.executeAction,
updateWidgetProperty: this.props.updateWidgetProperty,
2020-02-07 02:32:52 +00:00
updateWidgetMetaProperty: this.props.updateWidgetMetaProperty,
2019-11-08 11:02:00 +00:00
}}
>
<AppViewWrapper>
<AppViewerHeader />
<AppViewerBody>
<AppViewerSideNavWrapper>
<SideNav items={items} active={this.props.currentDSLPageId} />
</AppViewerSideNavWrapper>
<Switch>
<Route
path={getApplicationViewerPageURL()}
exact
component={AppViewerPageContainer}
/>
</Switch>
2019-11-08 11:02:00 +00:00
</AppViewerBody>
</AppViewWrapper>
</EditorContext.Provider>
);
}
}
const mapStateToProps = (state: AppState) => ({
2019-11-06 06:21:56 +00:00
currentDSLPageId: getCurrentDSLPageId(state),
pages: getPageList(state),
2020-01-24 09:54:40 +00:00
isInitialized: getIsInitialized(state),
});
const mapDispatchToProps = (dispatch: any) => ({
2020-02-18 10:41:52 +00:00
executeAction: (actionPayload: ExecuteActionPayload) =>
dispatch(executeAction(actionPayload)),
2019-11-08 11:02:00 +00:00
updateWidgetProperty: (
widgetId: string,
propertyName: string,
propertyValue: any,
) =>
dispatch(
updateWidgetProperty(
widgetId,
propertyName,
propertyValue,
RenderModes.PAGE,
),
),
2020-02-07 02:32:52 +00:00
updateWidgetMetaProperty: (
widgetId: string,
propertyName: string,
propertyValue: any,
) =>
dispatch(updateWidgetMetaProperty(widgetId, propertyName, propertyValue)),
initializeAppViewer: (applicationId: string) =>
dispatch({
type: ReduxActionTypes.INITIALIZE_PAGE_VIEWER,
payload: { applicationId },
}),
});
export default withRouter(
connect(mapStateToProps, mapDispatchToProps)(AppViewer),
);