From 2c07d43f46f7ae8f9535c4de0b4ffb56ff3858f6 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Mon, 20 Jun 2016 23:12:56 -0700 Subject: [PATCH] More tree --- server/migrations/20160619080644-initial.js | 2 +- server/models/Atlas.js | 16 +++++++- server/models/Document.js | 5 ++- server/presenters.js | 5 +++ src/scenes/Atlas/Atlas.js | 9 ++++- src/scenes/Atlas/AtlasStore.js | 3 +- src/scenes/DocumentScene/DocumentScene.js | 43 ++++++--------------- 7 files changed, 46 insertions(+), 37 deletions(-) diff --git a/server/migrations/20160619080644-initial.js b/server/migrations/20160619080644-initial.js index 2407ae40..cc6902ad 100644 --- a/server/migrations/20160619080644-initial.js +++ b/server/migrations/20160619080644-initial.js @@ -122,7 +122,7 @@ module.exports = { defaultValue: null, special: [], primaryKey: false }, - rootDocumentForId: + parentDocumentForId: { type: 'UUID', allowNull: true, defaultValue: null, diff --git a/server/models/Atlas.js b/server/models/Atlas.js index 5c358618..bc7784b8 100644 --- a/server/models/Atlas.js +++ b/server/models/Atlas.js @@ -34,10 +34,24 @@ const Atlas = sequelize.define('atlas', { // const slugifiedTitle = slug(this.title); // 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.hasOne(Document, { as: 'rootDocument', foreignKey: 'rootDocumentForId', constraints: false }); +Atlas.hasOne(Document, { as: 'parentDocument', foreignKey: 'parentDocumentForId', constraints: false }); export default Atlas; diff --git a/server/models/Document.js b/server/models/Document.js index 904ba7ad..e0829927 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -45,7 +45,10 @@ const Document = sequelize.define('document', { buildUrl() { const slugifiedTitle = slug(this.title); return `${slugifiedTitle}-${this.urlId}`; - } + }, + getUrl() { + return `/documents/${ this.id }`; + }, } }); diff --git a/server/presenters.js b/server/presenters.js index e00b4fcf..05f27c18 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -31,6 +31,11 @@ export function presentAtlas(atlas, includeRecentDocuments=false) { type: atlas.type, } + if (atlas.type === 'atlas') { + // Todo replace with `.atlasStructure` + data.structure = await atlas.buildStructure(); + } + if (includeRecentDocuments) { const documents = await Document.findAll({ where: { diff --git a/src/scenes/Atlas/Atlas.js b/src/scenes/Atlas/Atlas.js index 52142613..4310ecc7 100644 --- a/src/scenes/Atlas/Atlas.js +++ b/src/scenes/Atlas/Atlas.js @@ -1,6 +1,7 @@ import React from 'react'; import { observer } from 'mobx-react'; import Link from 'react-router/lib/Link'; +import History from 'utils/History'; import store from './AtlasStore'; @@ -16,7 +17,13 @@ import styles from './Atlas.scss'; class Atlas extends React.Component { componentDidMount = () => { 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() { diff --git a/src/scenes/Atlas/AtlasStore.js b/src/scenes/Atlas/AtlasStore.js index d103de55..2eb026d3 100644 --- a/src/scenes/Atlas/AtlasStore.js +++ b/src/scenes/Atlas/AtlasStore.js @@ -8,7 +8,7 @@ const store = new class AtlasStore { /* Actions */ - @action fetchAtlas = async (id) => { + @action fetchAtlas = async (id, successCallback) => { this.isFetching = true; this.atlas = null; @@ -16,6 +16,7 @@ const store = new class AtlasStore { const res = await client.post('/atlases.info', { id: id }); const { data } = res; this.atlas = data; + successCallback(data); } catch (e) { console.error("Something went wrong"); } diff --git a/src/scenes/DocumentScene/DocumentScene.js b/src/scenes/DocumentScene/DocumentScene.js index ac754573..61862c5e 100644 --- a/src/scenes/DocumentScene/DocumentScene.js +++ b/src/scenes/DocumentScene/DocumentScene.js @@ -18,31 +18,10 @@ const cx = classNames.bind(styles); 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 class DocumentScene extends React.Component { state = { didScroll: false, - tree: tree, } componentDidMount = () => { @@ -78,17 +57,17 @@ class DocumentScene extends React.Component { ); } - onClickNode = (node) => { - this.setState({ - active: node - }); - } + // onClickNode = (node) => { + // this.setState({ + // active: node + // }); + // } - handleChange = (tree) => { - this.setState({ - tree: tree - }); - } + // handleChange = (tree) => { + // this.setState({ + // tree: tree + // }); + // } render() { const doc = store.document; @@ -131,7 +110,7 @@ class DocumentScene extends React.Component {