2017-05-26 19:56:10 +00:00
|
|
|
// @flow
|
2017-10-31 07:03:17 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-07-18 06:30:16 +00:00
|
|
|
import { observable } from 'mobx';
|
2017-05-26 19:56:10 +00:00
|
|
|
import { observer, inject } from 'mobx-react';
|
2017-11-20 07:39:56 +00:00
|
|
|
import { withRouter, Link } from 'react-router-dom';
|
2017-07-18 06:30:16 +00:00
|
|
|
import styled from 'styled-components';
|
|
|
|
import { newDocumentUrl } from 'utils/routeHelpers';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
|
|
|
import CollectionsStore from 'stores/CollectionsStore';
|
2017-11-20 05:32:18 +00:00
|
|
|
import DocumentsStore from 'stores/DocumentsStore';
|
2017-11-20 04:16:49 +00:00
|
|
|
import UiStore from 'stores/UiStore';
|
2017-07-24 01:07:11 +00:00
|
|
|
import Collection from 'models/Collection';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
import Search from 'scenes/Search';
|
2017-11-20 07:39:56 +00:00
|
|
|
import CollectionMenu from 'menus/CollectionMenu';
|
|
|
|
import Actions, { Action, Separator } from 'components/Actions';
|
2017-05-26 19:56:10 +00:00
|
|
|
import CenteredContent from 'components/CenteredContent';
|
2017-11-20 04:16:49 +00:00
|
|
|
import CollectionIcon from 'components/Icon/CollectionIcon';
|
2017-11-20 07:39:56 +00:00
|
|
|
import NewDocumentIcon from 'components/Icon/NewDocumentIcon';
|
2017-11-20 05:50:42 +00:00
|
|
|
import { ListPlaceholder } from 'components/LoadingPlaceholder';
|
2017-07-18 06:30:16 +00:00
|
|
|
import Button from 'components/Button';
|
|
|
|
import HelpText from 'components/HelpText';
|
2017-11-20 05:32:18 +00:00
|
|
|
import DocumentList from 'components/DocumentList';
|
2017-11-20 04:16:49 +00:00
|
|
|
import Subheading from 'components/Subheading';
|
|
|
|
import PageTitle from 'components/PageTitle';
|
2017-10-31 07:03:17 +00:00
|
|
|
import Flex from 'shared/components/Flex';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
|
|
|
type Props = {
|
2017-11-20 04:16:49 +00:00
|
|
|
ui: UiStore,
|
2017-11-20 05:32:18 +00:00
|
|
|
documents: DocumentsStore,
|
2017-05-26 19:56:10 +00:00
|
|
|
collections: CollectionsStore,
|
2017-11-20 07:39:56 +00:00
|
|
|
history: Object,
|
2017-05-26 19:56:10 +00:00
|
|
|
match: Object,
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@observer
|
|
|
|
class CollectionScene extends Component {
|
2017-05-26 19:56:10 +00:00
|
|
|
props: Props;
|
2017-10-31 07:03:17 +00:00
|
|
|
@observable collection: ?Collection;
|
2017-11-20 04:16:49 +00:00
|
|
|
@observable isFetching: boolean = true;
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2017-11-20 05:32:18 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.loadContent(this.props.match.params.id);
|
|
|
|
}
|
2017-07-18 06:30:16 +00:00
|
|
|
|
2017-09-18 06:45:40 +00:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.match.params.id !== this.props.match.params.id) {
|
2017-11-20 05:32:18 +00:00
|
|
|
this.loadContent(nextProps.match.params.id);
|
2017-09-18 06:45:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 08:15:34 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.ui.clearActiveCollection();
|
|
|
|
}
|
|
|
|
|
2017-11-20 05:32:18 +00:00
|
|
|
loadContent = async (id: string) => {
|
2017-07-18 06:30:16 +00:00
|
|
|
const { collections } = this.props;
|
|
|
|
|
2017-11-20 05:50:42 +00:00
|
|
|
const collection = collections.getById(id) || (await collections.fetch(id));
|
2017-11-20 05:32:18 +00:00
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
if (collection) {
|
|
|
|
this.props.ui.setActiveCollection(collection);
|
|
|
|
this.collection = collection;
|
2017-11-20 05:32:18 +00:00
|
|
|
await this.props.documents.fetchRecentlyModified({
|
2017-11-23 02:40:37 +00:00
|
|
|
limit: 10,
|
|
|
|
collection: collection.id,
|
2017-11-20 05:32:18 +00:00
|
|
|
});
|
2017-07-18 06:30:16 +00:00
|
|
|
}
|
2017-11-20 04:16:49 +00:00
|
|
|
|
2017-07-18 06:30:16 +00:00
|
|
|
this.isFetching = false;
|
2017-05-26 19:56:10 +00:00
|
|
|
};
|
|
|
|
|
2017-11-20 07:39:56 +00:00
|
|
|
onNewDocument = (ev: SyntheticEvent) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
|
|
|
|
if (this.collection) {
|
|
|
|
this.props.history.push(`${this.collection.url}/new`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-30 21:16:20 +00:00
|
|
|
renderActions() {
|
|
|
|
return (
|
|
|
|
<Actions align="center" justify="flex-end">
|
|
|
|
<Action>
|
2018-01-31 07:39:43 +00:00
|
|
|
<CollectionMenu
|
|
|
|
history={this.props.history}
|
|
|
|
collection={this.collection}
|
|
|
|
/>
|
2018-01-30 21:16:20 +00:00
|
|
|
</Action>
|
|
|
|
<Separator />
|
|
|
|
<Action>
|
|
|
|
<a onClick={this.onNewDocument}>
|
|
|
|
<NewDocumentIcon />
|
|
|
|
</a>
|
|
|
|
</Action>
|
|
|
|
</Actions>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-31 07:03:17 +00:00
|
|
|
renderEmptyCollection() {
|
|
|
|
if (!this.collection) return;
|
|
|
|
|
2017-07-18 06:30:16 +00:00
|
|
|
return (
|
2017-11-20 04:16:49 +00:00
|
|
|
<CenteredContent>
|
|
|
|
<PageTitle title={this.collection.name} />
|
|
|
|
<Heading>
|
|
|
|
<CollectionIcon color={this.collection.color} size={40} expanded />{' '}
|
|
|
|
{this.collection.name}
|
|
|
|
</Heading>
|
2017-07-18 06:30:16 +00:00
|
|
|
<HelpText>
|
2017-11-20 04:16:49 +00:00
|
|
|
Publish your first document to start building this collection.
|
2017-07-18 06:30:16 +00:00
|
|
|
</HelpText>
|
2017-11-20 07:39:56 +00:00
|
|
|
<Wrapper>
|
2017-10-31 07:03:17 +00:00
|
|
|
<Link to={newDocumentUrl(this.collection)}>
|
|
|
|
<Button>Create new document</Button>
|
|
|
|
</Link>
|
2017-11-20 07:39:56 +00:00
|
|
|
</Wrapper>
|
2018-01-31 07:39:43 +00:00
|
|
|
{this.renderActions()}
|
2017-11-20 04:16:49 +00:00
|
|
|
</CenteredContent>
|
2017-07-18 06:30:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
renderNotFound() {
|
|
|
|
return <Search notFound />;
|
|
|
|
}
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
render() {
|
2017-11-20 05:50:42 +00:00
|
|
|
if (!this.isFetching && !this.collection) {
|
|
|
|
return this.renderNotFound();
|
|
|
|
}
|
|
|
|
if (this.collection && this.collection.isEmpty) {
|
|
|
|
return this.renderEmptyCollection();
|
|
|
|
}
|
2017-10-31 07:03:17 +00:00
|
|
|
|
2017-07-18 06:30:16 +00:00
|
|
|
return (
|
|
|
|
<CenteredContent>
|
2017-11-20 05:50:42 +00:00
|
|
|
{this.collection ? (
|
|
|
|
<span>
|
|
|
|
<PageTitle title={this.collection.name} />
|
|
|
|
<Heading>
|
|
|
|
<CollectionIcon
|
|
|
|
color={this.collection.color}
|
|
|
|
size={40}
|
|
|
|
expanded
|
|
|
|
/>{' '}
|
|
|
|
{this.collection.name}
|
|
|
|
</Heading>
|
|
|
|
<Subheading>Recently edited</Subheading>
|
|
|
|
<DocumentList
|
2017-11-28 16:55:15 +00:00
|
|
|
documents={this.props.documents.recentlyEditedIn(
|
|
|
|
this.collection.documentIds
|
2017-11-20 05:50:42 +00:00
|
|
|
)}
|
|
|
|
/>
|
2018-01-30 21:16:20 +00:00
|
|
|
{this.renderActions()}
|
2017-11-20 05:50:42 +00:00
|
|
|
</span>
|
|
|
|
) : (
|
|
|
|
<ListPlaceholder count={5} />
|
|
|
|
)}
|
2017-07-18 06:30:16 +00:00
|
|
|
</CenteredContent>
|
|
|
|
);
|
2017-05-26 19:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 06:30:16 +00:00
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
const Heading = styled.h1`
|
|
|
|
display: flex;
|
|
|
|
|
|
|
|
svg {
|
|
|
|
margin-left: -6px;
|
|
|
|
margin-right: 6px;
|
|
|
|
}
|
2017-07-18 06:30:16 +00:00
|
|
|
`;
|
|
|
|
|
2017-11-20 07:39:56 +00:00
|
|
|
const Wrapper = styled(Flex)`
|
2017-10-31 07:03:17 +00:00
|
|
|
margin: 10px 0;
|
2017-07-18 06:30:16 +00:00
|
|
|
`;
|
|
|
|
|
2017-11-20 07:39:56 +00:00
|
|
|
export default withRouter(
|
|
|
|
inject('collections', 'documents', 'ui')(CollectionScene)
|
|
|
|
);
|