Refactor Dashboard and automatically visit only collection

This commit is contained in:
Jori Lallo
2017-02-09 20:21:37 -08:00
parent dc7480218c
commit 3406af3456
4 changed files with 30 additions and 13 deletions

View File

@@ -1,7 +1,8 @@
import React from 'react';
import { observer } from 'mobx-react';
import { withRouter } from 'react-router';
import store from './DashboardStore';
import DashboardStore from './DashboardStore';
import { Flex } from 'reflexbox';
import Layout from 'components/Layout';
@@ -9,14 +10,21 @@ import AtlasPreview from 'components/AtlasPreview';
import AtlasPreviewLoading from 'components/AtlasPreviewLoading';
import CenteredContent from 'components/CenteredContent';
@withRouter
@observer(['user'])
class Dashboard extends React.Component {
static propTypes = {
user: React.PropTypes.object.isRequired,
router: React.PropTypes.object.isRequired,
}
componentDidMount = () => {
store.fetchCollections(this.props.user.team.id);
constructor(props) {
super(props);
this.store = new DashboardStore({
team: props.user.team,
router: props.router,
});
}
render() {
@@ -25,10 +33,10 @@ class Dashboard extends React.Component {
<Layout>
<CenteredContent>
<Flex column auto>
{ store.isFetching ? (
{ this.store.isFetching ? (
<AtlasPreviewLoading />
) : (
store.collections && store.collections.map((collection) => {
this.store.collections && this.store.collections.map((collection) => {
return (<AtlasPreview key={ collection.id } data={ collection } />);
})
) }

View File

@@ -1,7 +1,7 @@
import { observable, action, runInAction } from 'mobx';
import { client, cacheResponse } from 'utils/ApiClient';
const store = new class DashboardStore {
class DashboardStore {
@observable collections;
@observable pagination;
@@ -9,22 +9,33 @@ const store = new class DashboardStore {
/* Actions */
@action fetchCollections = async (teamId) => {
@action fetchCollections = async () => {
this.isFetching = true;
try {
const res = await client.post('/collections.list', { id: teamId });
const res = await client.post('/collections.list', { id: this.team.id });
const { data, pagination } = res;
runInAction('fetchCollections', () => {
this.collections = data;
this.pagination = pagination;
data.forEach((collection) => cacheResponse(collection.recentDocuments));
});
// If only one collection, visit it automatically
if (this.collections.length === 1) {
this.router.push(this.collections[0].url);
}
} catch (e) {
console.error("Something went wrong");
console.error('Something went wrong');
}
this.isFetching = false;
}
}();
export default store;
constructor(options) {
this.team = options.team;
this.router = options.router;
this.fetchCollections();
}
}
export default DashboardStore;

View File

@@ -26,8 +26,6 @@ Are you sure you want to disgard them?`;
@withRouter
@observer
class DocumentEdit extends Component {
static store;
static propTypes = {
route: React.PropTypes.object.isRequired,
router: React.PropTypes.object.isRequired,