2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 16:59:02 +02:00
|
|
|
import React, { ReactNode } from "react";
|
|
|
|
|
import classNames from "classnames";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import Loading from "./../Loading";
|
|
|
|
|
import ErrorNotification from "./../ErrorNotification";
|
|
|
|
|
import Title from "./Title";
|
|
|
|
|
import Subtitle from "./Subtitle";
|
|
|
|
|
import PageActions from "./PageActions";
|
|
|
|
|
import ErrorBoundary from "../ErrorBoundary";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
title?: string;
|
|
|
|
|
subtitle?: string;
|
|
|
|
|
loading?: boolean;
|
|
|
|
|
error?: Error;
|
|
|
|
|
showContentOnError?: boolean;
|
2019-10-20 16:59:02 +02:00
|
|
|
children: ReactNode;
|
2019-02-20 14:21:01 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
const PageActionContainer = styled.div`
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
|
|
// every child except first
|
|
|
|
|
> * ~ * {
|
|
|
|
|
margin-left: 1.25rem;
|
2019-02-20 14:21:01 +01:00
|
|
|
}
|
2019-10-08 16:42:08 +02:00
|
|
|
`;
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-08 16:42:08 +02:00
|
|
|
export default class Page extends React.Component<Props> {
|
2020-01-21 15:05:15 +01:00
|
|
|
componentDidUpdate() {
|
|
|
|
|
const { title } = this.props;
|
2020-01-21 15:18:37 +01:00
|
|
|
if (title && title !== document.title) {
|
2020-01-21 15:05:15 +01:00
|
|
|
document.title = title;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-08-28 08:07:46 +02:00
|
|
|
isPageAction(node: any) {
|
2019-10-08 16:42:08 +02:00
|
|
|
return (
|
2019-10-21 10:57:56 +02:00
|
|
|
node.displayName === PageActions.displayName || (node.type && node.type.displayName === PageActions.displayName)
|
2019-10-08 16:42:08 +02:00
|
|
|
);
|
2019-08-28 08:07:46 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-20 13:47:17 +01:00
|
|
|
renderPageHeader() {
|
2019-10-08 16:42:08 +02:00
|
|
|
const { error, title, subtitle, children } = 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-04-19 13:27:12 +02:00
|
|
|
if (child && !error) {
|
2019-08-28 08:07:46 +02:00
|
|
|
if (this.isPageAction(child)) {
|
2019-04-19 13:27:12 +02:00
|
|
|
pageActions = (
|
2019-10-08 16:42:08 +02:00
|
|
|
<PageActionContainer
|
2019-10-21 10:57:56 +02:00
|
|
|
className={classNames("column", "is-three-fifths", "is-mobile-action-spacing", "is-flex")}
|
2019-04-19 13:27:12 +02:00
|
|
|
>
|
|
|
|
|
{child}
|
2019-10-08 16:42:08 +02:00
|
|
|
</PageActionContainer>
|
2019-04-19 13:27:12 +02:00
|
|
|
);
|
2019-04-24 16:45:20 +02:00
|
|
|
pageActionsExists = true;
|
|
|
|
|
}
|
2019-02-20 11:53:38 +01:00
|
|
|
}
|
|
|
|
|
});
|
2019-10-21 10:57:56 +02:00
|
|
|
const underline = pageActionsExists ? <hr className="header-with-actions" /> : 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
|
|
|
|
2019-10-21 10:57:56 +02:00
|
|
|
const content: ReactNode[] = [];
|
2019-02-20 11:16:59 +01:00
|
|
|
React.Children.forEach(children, child => {
|
2019-04-19 13:27:12 +02:00
|
|
|
if (child) {
|
2019-08-28 08:07:46 +02:00
|
|
|
if (!this.isPageAction(child)) {
|
2019-04-19 13:27:12 +02:00
|
|
|
content.push(child);
|
|
|
|
|
}
|
2019-02-20 11:16:59 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return content;
|
|
|
|
|
}
|
2018-09-03 16:17:36 +02:00
|
|
|
}
|