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

146 lines
4.2 KiB
TypeScript
Raw Normal View History

import React, { Component } from "react";
import { RouteComponentProps, Link } from "react-router-dom";
import { connect } from "react-redux";
import { getIsFetchingPage } from "selectors/appViewSelectors";
2020-01-16 11:46:21 +00:00
import styled from "styled-components";
import { ContainerWidgetProps } from "widgets/ContainerWidget";
import { WidgetProps } from "widgets/BaseWidget";
2020-05-14 06:06:20 +00:00
import { AppViewerRouteParams, BUILDER_PAGE_URL } from "constants/routes";
import { AppState } from "reducers";
import { theme } from "constants/DefaultTheme";
import { NonIdealState, Icon, Spinner } from "@blueprintjs/core";
import Centered from "components/designSystems/appsmith/CenteredWrapper";
import AppPage from "./AppPage";
2020-03-11 13:59:46 +00:00
import {
getCanvasWidgetDsl,
getCurrentPageName,
} from "selectors/editorSelectors";
2020-12-30 07:31:20 +00:00
import OnboardingCompletionDialog from "components/editorComponents/Onboarding/CompletionDialog";
import ConfirmRunModal from "pages/Editor/ConfirmRunModal";
import { getCurrentApplication } from "selectors/applicationSelectors";
import {
isPermitted,
PERMISSION_TYPE,
} from "../Applications/permissionHelpers";
import { fetchPublishedPage } from "actions/pageActions";
2020-01-16 11:46:21 +00:00
const Section = styled.section`
2020-12-24 04:32:25 +00:00
background: ${(props) => props.theme.colors.bodyBG};
2020-01-16 11:46:21 +00:00
height: 100%;
width: 100%;
position: relative;
overflow-x: auto;
overflow-y: auto;
`;
type AppViewerPageContainerProps = {
isFetchingPage: boolean;
widgets?: ContainerWidgetProps<WidgetProps>;
2020-03-11 13:59:46 +00:00
currentPageName?: string;
currentAppName?: string;
2020-05-05 12:16:51 +00:00
fetchPage: (pageId: string, bustCache?: boolean) => void;
currentAppPermissions?: string[];
} & RouteComponentProps<AppViewerRouteParams>;
class AppViewerPageContainer extends Component<AppViewerPageContainerProps> {
componentDidUpdate(previously: AppViewerPageContainerProps) {
const { pageId } = this.props.match.params;
if (
pageId &&
previously.location.pathname !== this.props.location.pathname
) {
this.props.fetchPage(pageId);
}
}
render() {
let appsmithEditorLink;
if (
this.props.currentAppPermissions &&
isPermitted(
this.props.currentAppPermissions,
PERMISSION_TYPE.MANAGE_APPLICATION,
)
) {
appsmithEditorLink = (
<p>
Please add widgets to this page in the&nbsp;
<Link
to={BUILDER_PAGE_URL(
this.props.match.params.applicationId,
this.props.match.params.pageId,
)}
>
Appsmith Editor
</Link>
</p>
);
}
const pageNotFound = (
<Centered>
<NonIdealState
icon={
<Icon
iconSize={theme.fontSizes[9]}
icon="page-layout"
color={theme.colors.primaryOld}
/>
}
title="This page seems to be blank"
description={appsmithEditorLink}
/>
</Centered>
);
const pageLoading = (
<Centered>
<Spinner />
</Centered>
);
if (this.props.isFetchingPage) {
return pageLoading;
2020-05-14 06:06:20 +00:00
} else if (
!this.props.isFetchingPage &&
!(
this.props.widgets &&
this.props.widgets.children &&
this.props.widgets.children.length > 0
)
) {
return pageNotFound;
} else if (!this.props.isFetchingPage && this.props.widgets) {
2020-01-16 11:46:21 +00:00
return (
<Section>
2020-03-11 13:59:46 +00:00
<AppPage
dsl={this.props.widgets}
appName={this.props.currentAppName}
2020-03-11 13:59:46 +00:00
pageId={this.props.match.params.pageId}
pageName={this.props.currentPageName}
/>
<ConfirmRunModal />
2020-12-30 07:31:20 +00:00
<OnboardingCompletionDialog />
2020-01-16 11:46:21 +00:00
</Section>
);
}
}
}
const mapStateToProps = (state: AppState) => {
const currentApp = getCurrentApplication(state);
const props = {
isFetchingPage: getIsFetchingPage(state),
widgets: getCanvasWidgetDsl(state),
currentPageName: getCurrentPageName(state),
currentAppName: currentApp?.name,
currentAppPermissions: currentApp?.userPermissions,
};
return props;
};
const mapDispatchToProps = (dispatch: any) => ({
2020-05-05 12:16:51 +00:00
fetchPage: (pageId: string, bustCache = false) =>
dispatch(fetchPublishedPage(pageId, bustCache)),
});
export default connect(
mapStateToProps,
mapDispatchToProps,
)(AppViewerPageContainer);