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

175 lines
5.3 KiB
TypeScript
Raw Normal View History

import React, { Component } from "react";
import styled from "styled-components";
import { connect } from "react-redux";
import { withRouter, RouteComponentProps, Route } from "react-router";
2020-03-11 13:59:46 +00:00
import { Switch } 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 {
PageListPayload,
ReduxActionTypes,
} from "constants/ReduxActionConstants";
import { getIsInitialized } from "selectors/appViewSelectors";
2019-11-25 05:07:27 +00:00
import { executeAction } from "actions/widgetActions";
import { ExecuteActionPayload } from "constants/AppsmithActionConstants/ActionConstants";
import { updateWidgetPropertyRequest } from "actions/controlActions";
2019-11-25 05:07:27 +00:00
import { RenderModes } from "constants/WidgetConstants";
import { EditorContext } from "components/editorComponents/EditorContextProvider";
import AppViewerPageContainer from "./AppViewerPageContainer";
2020-03-06 09:45:21 +00:00
import {
resetChildrenMetaProperty,
updateWidgetMetaProperty,
} from "actions/metaActions";
2020-04-16 13:00:53 +00:00
import { editorInitializer } from "utils/EditorUtils";
import * as Sentry from "@sentry/react";
import log from "loglevel";
2021-02-24 13:47:37 +00:00
import { getViewModePageList } from "selectors/editorSelectors";
2021-06-09 10:35:10 +00:00
import AppComments from "comments/AppComments/AppComments";
import AddCommentTourComponent from "comments/tour/AddCommentTourComponent";
const SentryRoute = Sentry.withSentryRouting(Route);
const AppViewerBody = styled.section<{ hasPages: boolean }>`
display: flex;
flex-direction: row;
align-items: stretch;
justify-content: flex-start;
height: calc(
100vh -
${(props) => (!props.hasPages ? props.theme.smallHeaderHeight : "72px")}
);
`;
2019-11-01 05:28:56 +00:00
2021-06-09 10:35:10 +00:00
const ContainerWithComments = styled.div`
display: flex;
width: 100%;
`;
const AppViewerBodyContainer = styled.div<{ width?: string }>`
overflow: auto;
margin: 0 auto;
`;
export type AppViewerProps = {
initializeAppViewer: (applicationId: string, pageId?: string) => void;
2020-01-24 09:54:40 +00:00
isInitialized: boolean;
isInitializeError: 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-03-06 09:45:21 +00:00
resetChildrenMetaProperty: (widgetId: string) => void;
pages: PageListPayload;
2020-02-18 10:41:52 +00:00
} & RouteComponentProps<BuilderRouteParams>;
class AppViewer extends Component<
AppViewerProps & RouteComponentProps<AppViewerRouteParams>
> {
2020-04-16 13:00:53 +00:00
public state = {
registered: false,
isSideNavOpen: true,
2020-04-16 13:00:53 +00:00
};
componentDidMount() {
2020-04-16 13:00:53 +00:00
editorInitializer().then(() => {
this.setState({ registered: true });
});
const { applicationId, pageId } = this.props.match.params;
log.debug({ applicationId, pageId });
if (applicationId) {
this.props.initializeAppViewer(applicationId, pageId);
2019-11-06 06:21:56 +00:00
}
}
2020-02-18 10:41:52 +00:00
toggleCollapse = (open: boolean) => {
this.setState({ isSideNavOpen: open });
};
public render() {
const { isInitialized } = this.props;
return (
<EditorContext.Provider
2019-11-08 11:02:00 +00:00
value={{
executeAction: this.props.executeAction,
2020-02-07 02:32:52 +00:00
updateWidgetMetaProperty: this.props.updateWidgetMetaProperty,
2020-03-06 09:45:21 +00:00
resetChildrenMetaProperty: this.props.resetChildrenMetaProperty,
2019-11-08 11:02:00 +00:00
}}
>
2021-06-09 10:35:10 +00:00
<ContainerWithComments>
<AppComments isInline />
<AppViewerBodyContainer>
<AppViewerBody hasPages={this.props.pages.length > 1}>
{isInitialized && this.state.registered && (
<Switch>
<SentryRoute
component={AppViewerPageContainer}
exact
path={getApplicationViewerPageURL()}
/>
<SentryRoute
component={AppViewerPageContainer}
exact
path={`${getApplicationViewerPageURL()}/fork`}
/>
</Switch>
)}
</AppViewerBody>
</AppViewerBodyContainer>
</ContainerWithComments>
<AddCommentTourComponent />
</EditorContext.Provider>
);
}
}
const mapStateToProps = (state: AppState) => ({
2020-01-24 09:54:40 +00:00
isInitialized: getIsInitialized(state),
2021-02-24 13:47:37 +00:00
pages: getViewModePageList(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(
updateWidgetPropertyRequest(
2019-11-08 11:02:00 +00:00
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)),
2020-03-06 09:45:21 +00:00
resetChildrenMetaProperty: (widgetId: string) =>
dispatch(resetChildrenMetaProperty(widgetId)),
initializeAppViewer: (applicationId: string, pageId?: string) => {
dispatch({
type: ReduxActionTypes.INITIALIZE_PAGE_VIEWER,
payload: { applicationId, pageId },
});
},
});
export default withRouter(
connect(mapStateToProps, mapDispatchToProps)(Sentry.withProfiler(AppViewer)),
);