More tree

This commit is contained in:
Jori Lallo
2016-06-20 23:12:56 -07:00
parent 24e02bfdc4
commit 2c07d43f46
7 changed files with 46 additions and 37 deletions

View File

@ -122,7 +122,7 @@ module.exports = {
defaultValue: null, defaultValue: null,
special: [], special: [],
primaryKey: false }, primaryKey: false },
rootDocumentForId: parentDocumentForId:
{ type: 'UUID', { type: 'UUID',
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,

View File

@ -34,10 +34,24 @@ const Atlas = sequelize.define('atlas', {
// const slugifiedTitle = slug(this.title); // const slugifiedTitle = slug(this.title);
// return `${slugifiedTitle}-${this.urlId}`; // return `${slugifiedTitle}-${this.urlId}`;
// } // }
async buildStructure() {
// TODO
const rootDocument = await Document.findOne({ where: {
parentDocumentForId: null,
atlasId: this.id,
}});
return {
name: rootDocument.title,
id: rootDocument.id,
url: rootDocument.getUrl(),
children: null,
}
}
} }
}); });
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' }); Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
Atlas.hasOne(Document, { as: 'rootDocument', foreignKey: 'rootDocumentForId', constraints: false }); Atlas.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false });
export default Atlas; export default Atlas;

View File

@ -45,7 +45,10 @@ const Document = sequelize.define('document', {
buildUrl() { buildUrl() {
const slugifiedTitle = slug(this.title); const slugifiedTitle = slug(this.title);
return `${slugifiedTitle}-${this.urlId}`; return `${slugifiedTitle}-${this.urlId}`;
} },
getUrl() {
return `/documents/${ this.id }`;
},
} }
}); });

View File

@ -31,6 +31,11 @@ export function presentAtlas(atlas, includeRecentDocuments=false) {
type: atlas.type, type: atlas.type,
} }
if (atlas.type === 'atlas') {
// Todo replace with `.atlasStructure`
data.structure = await atlas.buildStructure();
}
if (includeRecentDocuments) { if (includeRecentDocuments) {
const documents = await Document.findAll({ const documents = await Document.findAll({
where: { where: {

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { observer } from 'mobx-react'; import { observer } from 'mobx-react';
import Link from 'react-router/lib/Link'; import Link from 'react-router/lib/Link';
import History from 'utils/History';
import store from './AtlasStore'; import store from './AtlasStore';
@ -16,7 +17,13 @@ import styles from './Atlas.scss';
class Atlas extends React.Component { class Atlas extends React.Component {
componentDidMount = () => { componentDidMount = () => {
const { id } = this.props.params; const { id } = this.props.params;
store.fetchAtlas(id); store.fetchAtlas(id, data => {
// Forward directly to root document
if (data.type === 'atlas') {
History.replace(data.structure.url);
}
})
} }
render() { render() {

View File

@ -8,7 +8,7 @@ const store = new class AtlasStore {
/* Actions */ /* Actions */
@action fetchAtlas = async (id) => { @action fetchAtlas = async (id, successCallback) => {
this.isFetching = true; this.isFetching = true;
this.atlas = null; this.atlas = null;
@ -16,6 +16,7 @@ const store = new class AtlasStore {
const res = await client.post('/atlases.info', { id: id }); const res = await client.post('/atlases.info', { id: id });
const { data } = res; const { data } = res;
this.atlas = data; this.atlas = data;
successCallback(data);
} catch (e) { } catch (e) {
console.error("Something went wrong"); console.error("Something went wrong");
} }

View File

@ -18,31 +18,10 @@ const cx = classNames.bind(styles);
import treeStyles from 'components/Tree/Tree.scss'; import treeStyles from 'components/Tree/Tree.scss';
const tree = {
name: "Introduction",
id: "1",
children: [{
collapsed: false,
name: "dist",
id: "2",
children: [
{
name: "Details",
id: "21",
},
{
name: "Distribution",
id: "22",
}
]
}]
};
@observer @observer
class DocumentScene extends React.Component { class DocumentScene extends React.Component {
state = { state = {
didScroll: false, didScroll: false,
tree: tree,
} }
componentDidMount = () => { componentDidMount = () => {
@ -78,17 +57,17 @@ class DocumentScene extends React.Component {
); );
} }
onClickNode = (node) => { // onClickNode = (node) => {
this.setState({ // this.setState({
active: node // active: node
}); // });
} // }
handleChange = (tree) => { // handleChange = (tree) => {
this.setState({ // this.setState({
tree: tree // tree: tree
}); // });
} // }
render() { render() {
const doc = store.document; const doc = store.document;
@ -131,7 +110,7 @@ class DocumentScene extends React.Component {
<div className={ styles.sidebar }> <div className={ styles.sidebar }>
<Tree <Tree
paddingLeft={20} paddingLeft={20}
tree={this.state.tree} tree={ doc.atlas.structure }
onChange={this.handleChange} onChange={this.handleChange}
isNodeCollapsed={this.isNodeCollapsed} isNodeCollapsed={this.isNodeCollapsed}
renderNode={this.renderNode} renderNode={this.renderNode}