//@flow import * as React from "react"; import { compose } from "redux"; import injectSheet from "react-jss"; import classNames from "classnames"; import { translate } from "react-i18next"; import Loading from "./../Loading"; import ErrorNotification from "./../ErrorNotification"; import Title from "./Title"; import Subtitle from "./Subtitle"; import PageActions from "./PageActions"; import ErrorBoundary from "../ErrorBoundary"; type State = { value: string }; type Props = { title?: string, subtitle?: string, loading?: boolean, error?: Error, showContentOnError?: boolean, children: React.Node, filter: string => void, // context props classes: Object, t: string => string }; const styles = { actions: { display: "flex", justifyContent: "flex-end" }, inputField: { float: "right", marginTop: "1.25rem", marginRight: "1.25rem" }, inputHeight: { height: "2.5rem" }, button: { float: "right", marginTop: "1.25rem" } }; class Page extends React.Component { constructor(props) { super(props); this.state = { value: "" }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange(event) { this.setState({ value: event.target.value }); } handleSubmit(event) { this.props.filter(this.state.value); event.preventDefault(); } render() { const { error } = this.props; return (
{this.renderPageHeader()} {this.renderContent()}
); } renderPageHeader() { const { title, subtitle, children, classes, t } = this.props; let pageActions = null; let pageActionsExists = false; React.Children.forEach(children, child => { if (child && child.type.name === PageActions.name) { pageActions = (
{child}
); pageActionsExists = true; } }); let underline = pageActionsExists ? (
) : null; return ( <>
<Subtitle subtitle={subtitle} /> </div> {pageActions} </div> {underline} </> ); } renderContent() { const { loading, children, showContentOnError, error } = this.props; if (error && !showContentOnError) { return null; } if (loading) { return <Loading />; } let content = []; React.Children.forEach(children, child => { if (child && child.type.name !== PageActions.name) { content.push(child); } }); return content; } } export default compose( injectSheet(styles), translate("commons") )(Page);