2018-09-03 16:17:36 +02:00
|
|
|
//@flow
|
|
|
|
|
import * as React from "react";
|
|
|
|
|
import Loading from "./../Loading";
|
|
|
|
|
import ErrorNotification from "./../ErrorNotification";
|
|
|
|
|
import Title from "./Title";
|
|
|
|
|
import Subtitle from "./Subtitle";
|
2019-02-20 14:21:01 +01:00
|
|
|
import injectSheet from "react-jss";
|
|
|
|
|
import classNames from "classnames";
|
2019-02-21 14:00:45 +01:00
|
|
|
import PageActions from "./PageActions";
|
2019-03-13 21:41:08 +01:00
|
|
|
import ErrorBoundary from "../ErrorBoundary";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-04-10 14:35:30 +02:00
|
|
|
type State = {
|
|
|
|
|
value: string
|
|
|
|
|
};
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
type Props = {
|
|
|
|
|
title?: string,
|
|
|
|
|
subtitle?: string,
|
|
|
|
|
loading?: boolean,
|
|
|
|
|
error?: Error,
|
|
|
|
|
showContentOnError?: boolean,
|
2019-02-20 14:21:01 +01:00
|
|
|
children: React.Node,
|
2019-04-10 14:35:30 +02:00
|
|
|
filter: string => void,
|
2019-02-20 14:21:01 +01:00
|
|
|
|
|
|
|
|
// context props
|
|
|
|
|
classes: Object
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const styles = {
|
2019-04-10 14:35:30 +02:00
|
|
|
actions: {
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "flex-end"
|
|
|
|
|
},
|
|
|
|
|
inputField: {
|
|
|
|
|
float: "right",
|
2019-02-21 09:39:05 +01:00
|
|
|
marginTop: "1.25rem",
|
2019-04-10 14:35:30 +02:00
|
|
|
marginRight: "1.25rem"
|
|
|
|
|
},
|
|
|
|
|
inputHeight: {
|
|
|
|
|
height: "2.5rem"
|
|
|
|
|
},
|
|
|
|
|
button: {
|
|
|
|
|
float: "right",
|
|
|
|
|
marginTop: "1.25rem"
|
2019-02-20 14:21:01 +01:00
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-04-10 14:35:30 +02:00
|
|
|
class Page extends React.Component<Props, State> {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
render() {
|
2019-02-20 13:47:17 +01:00
|
|
|
const { error } = this.props;
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
2019-03-14 10:12:34 +01:00
|
|
|
<section className="section">
|
|
|
|
|
<div className="container">
|
|
|
|
|
{this.renderPageHeader()}
|
|
|
|
|
<ErrorBoundary>
|
2019-04-10 14:35:30 +02:00
|
|
|
<ErrorNotification error={error} />
|
2019-03-13 11:54:33 +01:00
|
|
|
{this.renderContent()}
|
2019-03-14 10:12:34 +01:00
|
|
|
</ErrorBoundary>
|
|
|
|
|
</div>
|
|
|
|
|
</section>
|
2018-09-03 16:17:36 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 13:47:17 +01:00
|
|
|
renderPageHeader() {
|
2019-02-20 14:21:01 +01:00
|
|
|
const { title, subtitle, children, classes } = this.props;
|
2019-02-20 11:53:38 +01:00
|
|
|
|
2019-02-21 17:21:04 +01:00
|
|
|
let pageActions = null;
|
2019-02-20 13:47:17 +01:00
|
|
|
let pageActionsExists = false;
|
2019-02-20 11:53:38 +01:00
|
|
|
React.Children.forEach(children, child => {
|
2019-02-21 14:00:45 +01:00
|
|
|
if (child && child.type.name === PageActions.name) {
|
2019-02-21 17:21:04 +01:00
|
|
|
pageActions = (
|
2019-04-10 14:35:30 +02:00
|
|
|
<div
|
|
|
|
|
className={classNames(classes.actions, "column is-three-fifths is-mobile-action-spacing")}
|
|
|
|
|
>
|
|
|
|
|
<form className={classNames(classes.inputField, "input-field")}>
|
|
|
|
|
<div
|
|
|
|
|
className="control has-icons-left"
|
|
|
|
|
onSubmit={this.handleSubmit}
|
|
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
className={classNames(classes.inputHeight, "input")}
|
|
|
|
|
type="search"
|
|
|
|
|
placeholder="filter text"
|
|
|
|
|
value={this.state.value}
|
|
|
|
|
onChange={this.handleChange}
|
|
|
|
|
/>
|
|
|
|
|
<span className="icon is-small is-left">
|
|
|
|
|
<i className="fas fa-search" />
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
2019-02-21 17:21:04 +01:00
|
|
|
<div
|
|
|
|
|
className={classNames(
|
2019-04-10 14:35:30 +02:00
|
|
|
classes.button,
|
|
|
|
|
"input-button control"
|
2019-02-21 17:21:04 +01:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{child}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2019-02-20 13:47:17 +01:00
|
|
|
pageActionsExists = true;
|
2019-02-20 11:53:38 +01:00
|
|
|
}
|
|
|
|
|
});
|
2019-02-20 14:21:01 +01:00
|
|
|
let underline = pageActionsExists ? (
|
2019-04-10 14:35:30 +02:00
|
|
|
<hr className="header-with-actions" />
|
2019-02-20 14:21:01 +01:00
|
|
|
) : null;
|
2019-02-20 13:47:17 +01:00
|
|
|
|
|
|
|
|
return (
|
2019-02-20 14:13:38 +01:00
|
|
|
<>
|
|
|
|
|
<div className="columns">
|
|
|
|
|
<div className="column">
|
2019-04-10 14:35:30 +02:00
|
|
|
<Title title={title} />
|
|
|
|
|
<Subtitle subtitle={subtitle} />
|
2019-02-20 14:13:38 +01:00
|
|
|
</div>
|
2019-02-21 17:21:04 +01:00
|
|
|
{pageActions}
|
2019-02-20 13:47:17 +01:00
|
|
|
</div>
|
2019-02-20 14:13:38 +01:00
|
|
|
{underline}
|
|
|
|
|
</>
|
2019-02-20 13:47:17 +01:00
|
|
|
);
|
2019-02-20 11:53:38 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
renderContent() {
|
|
|
|
|
const { loading, children, showContentOnError, error } = this.props;
|
2019-02-20 11:53:38 +01:00
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
if (error && !showContentOnError) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (loading) {
|
2019-04-10 14:35:30 +02:00
|
|
|
return <Loading />;
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
2019-02-20 11:16:59 +01:00
|
|
|
|
|
|
|
|
let content = [];
|
|
|
|
|
React.Children.forEach(children, child => {
|
2019-02-21 16:53:13 +01:00
|
|
|
if (child && child.type.name !== PageActions.name) {
|
2019-02-20 11:16:59 +01:00
|
|
|
content.push(child);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return content;
|
|
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-20 14:21:01 +01:00
|
|
|
export default injectSheet(styles)(Page);
|