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

170 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-10-18 08:16:26 +00:00
import React, { Component } from "react";
import { connect } from "react-redux";
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,
2019-10-24 09:23:50 +00:00
} from "../../selectors/editorSelectors";
import {
ReduxActionTypes,
PageListPayload,
} from "../../constants/ReduxActionConstants";
import { Dialog, Classes, AnchorButton } from "@blueprintjs/core";
2019-11-08 11:02:00 +00:00
import { initEditor } from "../../actions/initActions";
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;
publishApplication: Function;
previewPage: Function;
2019-11-08 11:02:00 +00:00
initEditor: Function;
createPage: Function;
pages: PageListPayload;
switchPage: (pageId: string) => void;
isPublishing: boolean;
errorPublishing: boolean;
};
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() {
2019-11-08 11:02:00 +00:00
this.props.initEditor();
}
componentDidUpdate(currently: EditorProps) {
const previously = this.props;
if (
!currently.isPublishing &&
previously.isPublishing &&
!currently.errorPublishing
) {
this.setState({
isDialogOpen: true,
});
}
}
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);
}
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
};
2019-08-29 11:22:09 +00:00
public render() {
return (
2019-10-18 08:16:26 +00:00
<div>
<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}
switchToPage={this.props.switchPage}
isPublishing={this.props.isPublishing}
/>
<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="_blank"
href={`/view/pages/${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),
currentPageId: getCurrentPageId(state),
currentLayoutId: getCurrentLayoutId(state),
pages: getPageList(state),
errorPublishing: getPublishingError(state),
isPublishing: getIsPublishingApplication(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 {
2019-11-08 11:02:00 +00:00
initEditor: () => dispatch(initEditor()),
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,
},
});
},
createPage: (applicationId: string, name: string) => {
dispatch({
type: ReduxActionTypes.CREATE_PAGE_INIT,
payload: {
applicationId,
name,
},
});
},
switchPage: (pageId: string) => {
dispatch({
type: ReduxActionTypes.FETCH_PAGE,
payload: {
pageId,
},
});
},
2019-10-24 09:23:50 +00:00
};
};
export default connect(
mapStateToProps,
mapDispatchToProps,
)(Editor);