Expose child document in sidebar

This commit is contained in:
Jori Lallo
2017-06-25 16:26:28 -07:00
parent c0d89be516
commit 42d646c16f
5 changed files with 53 additions and 24 deletions

View File

@ -59,7 +59,7 @@ type Props = {
}; };
render() { render() {
const { user, auth, ui, collections } = this.props; const { user, auth, ui } = this.props;
return ( return (
<Container column auto> <Container column auto>
@ -112,7 +112,8 @@ type Props = {
<LinkSection> <LinkSection>
{ui.activeCollection {ui.activeCollection
? <SidebarCollection ? <SidebarCollection
collection={collections.getById(ui.activeCollection)} document={ui.activeDocument}
collection={ui.activeCollection}
/> />
: <SidebarCollectionList />} : <SidebarCollectionList />}
</LinkSection> </LinkSection>

View File

@ -9,24 +9,40 @@ import SidebarLink from '../SidebarLink';
import Collection from 'models/Collection'; import Collection from 'models/Collection';
type Props = { type Props = {
collection: Collection, collection: ?Collection,
document: ?Document,
}; };
const SidebarCollection = ({ collection }: Props) => { class SidebarCollection extends React.Component {
props: Props;
renderDocuments(documentList) {
return documentList.map(document => (
<Flex column key={document.id}>
<SidebarLink key={document.id} to={document.url}>
{document.title}
</SidebarLink>
<Children>
{document.children && this.renderDocuments(document.children)}
</Children>
</Flex>
));
}
render() {
const { collection } = this.props;
if (collection) { if (collection) {
return ( return (
<Flex column> <Flex column>
<Header>{collection.name}</Header> <Header>{collection.name}</Header>
{collection.documents.map(document => ( {this.renderDocuments(collection.documents)}
<SidebarLink key={document.id} to={document.url}>
{document.title}
</SidebarLink>
))}
</Flex> </Flex>
); );
} }
return null; return null;
}; }
}
const Header = styled(Flex)` const Header = styled(Flex)`
font-size: 11px; font-size: 11px;
@ -36,4 +52,8 @@ const Header = styled(Flex)`
letter-spacing: 0.04em; letter-spacing: 0.04em;
`; `;
const Children = styled(Flex)`
margin-left: 20px;
`;
export default observer(SidebarCollection); export default observer(SidebarCollection);

View File

@ -73,7 +73,7 @@ type Props = {
} }
componentWillUnmount() { componentWillUnmount() {
this.props.ui.clearActiveCollection(); this.props.ui.clearActiveDocument();
} }
onEdit = () => { onEdit = () => {

View File

@ -119,7 +119,7 @@ class DocumentStore {
this.parentDocument = res.data; this.parentDocument = res.data;
} else { } else {
this.document = res.data; this.document = res.data;
this.ui.setActiveCollection(this.document.collection.id); this.ui.setActiveDocument(res.data);
} }
} catch (e) { } catch (e) {
console.error('Something went wrong'); console.error('Something went wrong');

View File

@ -1,18 +1,26 @@
// @flow // @flow
import { observable, action } from 'mobx'; import { observable, action, computed } from 'mobx';
import type { Document } from 'types';
import Collection from 'models/Collection';
class UiStore { class UiStore {
@observable activeCollection: ?string; @observable activeDocument: ?Document;
@observable editMode: boolean = false; @observable editMode: boolean = false;
/* Computed */
@computed get activeCollection(): ?Collection {
return this.activeDocument ? this.activeDocument.collection : undefined;
}
/* Actions */ /* Actions */
@action setActiveCollection = (id: string): void => { @action setActiveDocument = (document: Document): void => {
this.activeCollection = id; this.activeDocument = document;
}; };
@action clearActiveCollection = (): void => { @action clearActiveDocument = (): void => {
this.activeCollection = null; this.activeDocument = undefined;
}; };
@action enableEditMode() { @action enableEditMode() {