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

231 lines
6.8 KiB
TypeScript
Raw Normal View History

2019-10-18 08:16:26 +00:00
import React, { Component } from "react";
2019-12-03 09:21:06 +00:00
import { Helmet } from "react-helmet";
import { connect } from "react-redux";
import { Redirect } from "react-router-dom";
import { withRouter, RouteComponentProps } from "react-router-dom";
import {
BuilderRouteParams,
getApplicationViewerPageURL,
BUILDER_PAGE_URL,
} from "constants/routes";
import { UserApplication } from "constants/userConstants";
2019-11-25 05:07:27 +00:00
import { AppState } from "reducers";
import EditorHeader from "./EditorHeader";
import MainContainer from "./MainContainer";
2019-10-24 09:23:50 +00:00
import {
getCurrentApplicationId,
getCurrentLayoutId,
getCurrentPageId,
getPageList,
getIsPublishingApplication,
getPublishingError,
getIsPageSaving,
getIsEditorLoading,
getLoadingError,
getPublishedTime,
2020-01-24 09:54:40 +00:00
getIsEditorInitialized,
} from "selectors/editorSelectors";
import {
ReduxActionTypes,
PageListPayload,
} from "constants/ReduxActionConstants";
import { Dialog, Classes, AnchorButton } from "@blueprintjs/core";
import { initEditor } from "actions/initActions";
import { RenderModes } from "constants/WidgetConstants";
2020-03-06 04:59:24 +00:00
import { getCurrentApplication } from "selectors/applicationSelectors";
import AnalyticsUtil from "utils/AnalyticsUtil";
2020-03-24 14:05:19 +00:00
import { fetchPage } from "actions/pageActions";
2019-08-20 13:19:19 +00:00
2019-08-29 11:22:09 +00:00
type EditorProps = {
currentPageName?: string;
isSaving: boolean;
2019-10-24 09:23:50 +00:00
currentApplicationId?: string;
currentLayoutId?: string;
currentPageId?: string;
2019-10-24 09:23:50 +00:00
publishApplication: Function;
previewPage: Function;
2019-11-08 11:02:00 +00:00
initEditor: Function;
createPage: Function;
2020-03-24 14:05:19 +00:00
fetchPage: (pageId: string) => void;
pages: PageListPayload;
isPublishing: boolean;
isEditorLoading: boolean;
2020-01-24 09:54:40 +00:00
isEditorInitialized: boolean;
editorLoadingError: boolean;
errorPublishing: boolean;
publishedTime?: string;
isPageSwitching: boolean;
createModal: () => void;
currentApplication: UserApplication;
} & RouteComponentProps<BuilderRouteParams>;
2019-08-26 12:41:21 +00:00
2019-08-29 11:22:09 +00:00
class Editor extends Component<EditorProps> {
public state = {
isDialogOpen: false,
};
componentDidMount() {
2020-03-24 14:05:19 +00:00
const { applicationId, pageId } = this.props.match.params;
if (applicationId && pageId) {
this.props.initEditor(applicationId, pageId);
}
}
componentDidUpdate(previously: EditorProps) {
if (
previously.isPublishing &&
!(this.props.isPublishing || this.props.errorPublishing)
) {
this.setState({
isDialogOpen: true,
});
}
2020-03-24 14:05:19 +00:00
if (this.props.match.params.pageId !== previously.match.params.pageId) {
this.props.fetchPage(this.props.match.params.pageId);
}
}
handleDialogClose = () => {
this.setState({
isDialogOpen: false,
});
};
2019-10-24 09:23:50 +00:00
handlePublish = () => {
if (this.props.currentApplicationId) {
2019-10-24 09:23:50 +00:00
this.props.publishApplication(this.props.currentApplicationId);
2020-03-06 04:59:24 +00:00
const appName = this.props.currentApplication
? this.props.currentApplication.name
: "";
AnalyticsUtil.logEvent("PUBLISH_APP", {
appId: this.props.currentApplicationId,
appName: appName,
});
}
2019-10-24 09:23:50 +00:00
};
handleCreatePage = (pageName: string) => {
this.props.createPage(this.props.currentApplicationId, pageName);
2019-10-24 09:23:50 +00:00
};
redirectToPage = (pageId: string) => {
if (this.props.currentApplicationId) {
this.props.history.push(
BUILDER_PAGE_URL(this.props.currentApplicationId, pageId),
);
}
};
2019-08-29 11:22:09 +00:00
public render() {
if (!this.props.match.params.applicationId) {
return <Redirect to="/applications" />;
}
2020-01-24 09:54:40 +00:00
if (!this.props.isEditorInitialized) return null;
return (
2019-10-18 08:16:26 +00:00
<div>
2019-12-03 09:21:06 +00:00
<Helmet>
<meta charSet="utf-8" />
<title>Editor | Appsmith</title>
</Helmet>
<EditorHeader
2019-11-13 07:34:59 +00:00
isSaving={this.props.isSaving}
pageName={this.props.currentPageName}
2019-10-24 09:23:50 +00:00
onPublish={this.handlePublish}
onCreatePage={this.handleCreatePage}
pages={this.props.pages}
currentPageId={this.props.currentPageId}
2020-01-27 08:24:58 +00:00
currentApplicationId={this.props.currentApplicationId}
isPublishing={this.props.isPublishing}
publishedTime={this.props.publishedTime}
createModal={this.props.createModal}
/>
<MainContainer />
<Dialog
isOpen={this.state.isDialogOpen}
canOutsideClickClose={true}
canEscapeKeyClose={true}
title="Application Published"
onClose={this.handleDialogClose}
icon="tick-circle"
>
<div className={Classes.DIALOG_BODY}>
<p>
2019-11-04 14:22:50 +00:00
{
"Your awesome application is now published with the current changes!"
}
</p>
</div>
<div className={Classes.DIALOG_FOOTER}>
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
<AnchorButton
target={this.props.currentApplicationId}
href={getApplicationViewerPageURL(
this.props.currentApplicationId,
this.props.currentPageId,
)}
text="View Application"
/>
</div>
</div>
</Dialog>
2019-10-18 08:16:26 +00:00
</div>
);
}
}
2019-10-24 09:23:50 +00:00
const mapStateToProps = (state: AppState) => ({
2019-10-18 08:16:26 +00:00
currentPageName: state.ui.editor.currentPageName,
isSaving: getIsPageSaving(state),
2019-10-24 09:23:50 +00:00
currentApplicationId: getCurrentApplicationId(state),
2020-03-06 04:59:24 +00:00
currentApplication: getCurrentApplication(state),
2019-10-24 09:23:50 +00:00
currentPageId: getCurrentPageId(state),
currentLayoutId: getCurrentLayoutId(state),
pages: getPageList(state),
errorPublishing: getPublishingError(state),
isPublishing: getIsPublishingApplication(state),
isEditorLoading: getIsEditorLoading(state),
2020-01-24 09:54:40 +00:00
isEditorInitialized: getIsEditorInitialized(state),
editorLoadingError: getLoadingError(state),
publishedTime: getPublishedTime(state),
2019-10-18 08:16:26 +00:00
});
2019-08-26 12:41:21 +00:00
2019-10-24 09:23:50 +00:00
const mapDispatchToProps = (dispatch: any) => {
return {
2020-03-24 14:05:19 +00:00
initEditor: (applicationId: string, pageId: string) =>
dispatch(initEditor(applicationId, pageId)),
2019-10-24 09:23:50 +00:00
publishApplication: (applicationId: string) => {
dispatch({
type: ReduxActionTypes.PUBLISH_APPLICATION_INIT,
payload: {
applicationId,
},
});
},
previewPage: (pageId: string, layoutId: string) => {
dispatch({
type: ReduxActionTypes.FETCH_PUBLISHED_PAGE_INIT,
payload: {
pageId,
layoutId,
},
});
},
2020-03-24 14:05:19 +00:00
fetchPage: (pageId: string) => dispatch(fetchPage(pageId)),
createPage: (applicationId: string, name: string) => {
dispatch({
type: ReduxActionTypes.CREATE_PAGE_INIT,
payload: {
applicationId,
name,
},
});
},
// TODO(abhinav): get the render mode from context
createModal: () =>
dispatch({
type: ReduxActionTypes.CREATE_MODAL_INIT,
payload: {
renderMode: RenderModes.CANVAS,
},
}),
2019-10-24 09:23:50 +00:00
};
};
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Editor));