Add recently viewed docs to dashboard

This commit is contained in:
Tom Moor
2017-06-25 20:32:05 -07:00
parent 93a44e4a4c
commit 86b0966703
7 changed files with 75 additions and 42 deletions

View File

@ -2,7 +2,6 @@
import React from 'react'; import React from 'react';
import type { Document } from 'types'; import type { Document } from 'types';
import DocumentPreview from 'components/DocumentPreview'; import DocumentPreview from 'components/DocumentPreview';
import Divider from 'components/Divider';
class DocumentList extends React.Component { class DocumentList extends React.Component {
props: { props: {
@ -14,10 +13,7 @@ class DocumentList extends React.Component {
<div> <div>
{this.props.documents && {this.props.documents &&
this.props.documents.map(document => ( this.props.documents.map(document => (
<div> <DocumentPreview document={document} />
<DocumentPreview document={document} />
<Divider />
</div>
))} ))}
</div> </div>
); );

View File

@ -1,21 +1,20 @@
// @flow // @flow
import React, { Component } from 'react'; import React, { Component } from 'react';
import { toJS } from 'mobx';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import type { Document } from 'types'; import type { Document } from 'types';
import styled from 'styled-components'; import styled from 'styled-components';
import { color } from 'styles/constants'; import { color } from 'styles/constants';
import Markdown from 'components/Markdown';
import PublishingInfo from 'components/PublishingInfo'; import PublishingInfo from 'components/PublishingInfo';
type Props = { type Props = {
document: Document, document: Document,
highlight?: string,
innerRef?: Function, innerRef?: Function,
}; };
const DocumentLink = styled(Link)` const DocumentLink = styled(Link)`
display: block; display: block;
margin: 16px -16px; margin: 0 -16px;
padding: 16px; padding: 16px;
border-radius: 8px; border-radius: 8px;
border: 2px solid transparent; border: 2px solid transparent;
@ -35,16 +34,11 @@ const DocumentLink = styled(Link)`
border: 2px solid ${color.slateDark}; border: 2px solid ${color.slateDark};
} }
h1 { h3 {
margin-top: 0; margin-top: 0;
} }
`; `;
// $FlowIssue
const TruncatedMarkdown = styled(Markdown)`
pointer-events: none;
`;
class DocumentPreview extends Component { class DocumentPreview extends Component {
props: Props; props: Props;
@ -53,14 +47,13 @@ class DocumentPreview extends Component {
return ( return (
<DocumentLink to={document.url} innerRef={innerRef} {...rest}> <DocumentLink to={document.url} innerRef={innerRef} {...rest}>
<h3>{document.title}</h3>
<PublishingInfo <PublishingInfo
createdAt={document.createdAt} createdAt={document.createdAt}
createdBy={document.createdBy} createdBy={document.createdBy}
updatedAt={document.updatedAt} updatedAt={document.updatedAt}
updatedBy={document.updatedBy} updatedBy={document.updatedBy}
collaborators={toJS(document.collaborators)}
/> />
<TruncatedMarkdown text={document.text} limit={150} />
</DocumentLink> </DocumentLink>
); );
} }

View File

@ -106,7 +106,7 @@ type Props = {
<SidebarLink to="/search">Search</SidebarLink> <SidebarLink to="/search">Search</SidebarLink>
</LinkSection> </LinkSection>
<LinkSection> <LinkSection>
<SidebarLink to="/dashboard">Dashboard</SidebarLink> <SidebarLink to="/dashboard">Home</SidebarLink>
<SidebarLink to="/starred">Starred</SidebarLink> <SidebarLink to="/starred">Starred</SidebarLink>
</LinkSection> </LinkSection>
<LinkSection> <LinkSection>

View File

@ -7,7 +7,7 @@ import { Flex } from 'reflexbox';
const Container = styled(Flex)` const Container = styled(Flex)`
justify-content: space-between; justify-content: space-between;
color: #ccc; color: #bbb;
font-size: 13px; font-size: 13px;
`; `;
@ -26,7 +26,7 @@ const Avatar = styled.img`
class PublishingInfo extends Component { class PublishingInfo extends Component {
props: { props: {
collaborators: Array<User>, collaborators?: Array<User>,
createdAt: string, createdAt: string,
createdBy: User, createdBy: User,
updatedAt: string, updatedAt: string,
@ -35,13 +35,16 @@ class PublishingInfo extends Component {
}; };
render() { render() {
const { collaborators } = this.props;
return ( return (
<Container align="center"> <Container align="center">
<Avatars> {collaborators &&
{this.props.collaborators.map(user => ( <Avatars>
<Avatar key={user.id} src={user.avatarUrl} title={user.name} /> {collaborators.map(user => (
))} <Avatar key={user.id} src={user.avatarUrl} title={user.name} />
</Avatars> ))}
</Avatars>}
<span> <span>
{this.props.createdBy.name} {this.props.createdBy.name}
{' '} {' '}

View File

@ -1,38 +1,49 @@
// @flow // @flow
import React from 'react'; import React from 'react';
import { observer, inject } from 'mobx-react'; import { observer } from 'mobx-react';
import { Flex } from 'reflexbox'; import styled from 'styled-components';
import CollectionsStore from 'stores/CollectionsStore'; import DocumentList from 'components/DocumentList';
import PageTitle from 'components/PageTitle'; import PageTitle from 'components/PageTitle';
import Collection from 'components/Collection';
import CenteredContent from 'components/CenteredContent'; import CenteredContent from 'components/CenteredContent';
import PreviewLoading from 'components/PreviewLoading'; import RecentDocumentsStore from './RecentDocumentsStore';
type Props = { const Subheading = styled.h3`
collections: CollectionsStore, font-size: 11px;
}; font-weight: 500;
text-transform: uppercase;
color: #9FA6AB;
letter-spacing: 0.04em;
border-bottom: 1px solid #ddd;
padding-bottom: 10px;
`;
type Props = {};
@observer class Dashboard extends React.Component { @observer class Dashboard extends React.Component {
props: Props; props: Props;
store: RecentDocumentsStore;
constructor(props: Props) {
super(props);
this.store = new RecentDocumentsStore();
}
componentDidMount() {
this.store.fetchDocuments();
}
render() { render() {
const { collections } = this.props;
return ( return (
<CenteredContent> <CenteredContent>
<PageTitle title="Home" /> <PageTitle title="Home" />
<h1>Home</h1> <h1>Home</h1>
<Flex column auto> <Subheading>Recently viewed</Subheading>
{!collections.isLoaded <DocumentList documents={this.store.documents} />
? <PreviewLoading />
: collections.data.map(collection => (
<Collection key={collection.id} data={collection} />
))}
</Flex>
</CenteredContent> </CenteredContent>
); );
} }
} }
export default inject('collections')(Dashboard); export default Dashboard;

View File

@ -0,0 +1,29 @@
// @flow
import { observable, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import type { Document } from 'types';
class RecentDocumentsStore {
@observable documents: Array<Document> = [];
@observable isFetching = false;
@action fetchDocuments = async () => {
this.isFetching = true;
try {
const res = await client.get('/documents.viewed');
invariant(res && res.data, 'res or res.data missing');
const { data } = res;
runInAction('update state after fetching data', () => {
this.documents = data;
});
} catch (e) {
console.error('Something went wrong');
}
this.isFetching = false;
};
}
export default RecentDocumentsStore;

View File

@ -109,6 +109,7 @@ const ResultsWrapper = styled(Flex)`
innerRef={ref => index === 0 && this.setFirstDocumentRef(ref)} innerRef={ref => index === 0 && this.setFirstDocumentRef(ref)}
key={document.id} key={document.id}
document={document} document={document}
highlight={this.store.searchTerm}
/> />
))} ))}
</ArrowKeyNavigation> </ArrowKeyNavigation>