This commit is contained in:
Tom Moor
2017-06-27 21:20:09 -07:00
parent 72fd39b494
commit 4c3f4fea16
2 changed files with 21 additions and 18 deletions

View File

@ -11,6 +11,7 @@ import {
import { Flex } from 'reflexbox'; import { Flex } from 'reflexbox';
import stores from 'stores'; import stores from 'stores';
import DocumentsStore from 'stores/DocumentsStore';
import CollectionsStore from 'stores/CollectionsStore'; import CollectionsStore from 'stores/CollectionsStore';
import 'normalize.css/normalize.css'; import 'normalize.css/normalize.css';
@ -57,12 +58,14 @@ const Auth = ({ children }: AuthProps) => {
const user = stores.auth.getUserStore(); const user = stores.auth.getUserStore();
authenticatedStores = { authenticatedStores = {
user, user,
documents: new DocumentsStore(),
collections: new CollectionsStore({ collections: new CollectionsStore({
teamId: user.team.id, teamId: user.team.id,
}), }),
}; };
authenticatedStores.collections.fetch(); authenticatedStores.documents.fetchAll();
authenticatedStores.collections.fetchAll();
} }
return ( return (
@ -135,3 +138,5 @@ render(
</div>, </div>,
document.getElementById('root') document.getElementById('root')
); );
window.authenticatedStores = authenticatedStores;

View File

@ -8,10 +8,6 @@ import stores from 'stores';
import Document from 'models/Document'; import Document from 'models/Document';
import ErrorsStore from 'stores/ErrorsStore'; import ErrorsStore from 'stores/ErrorsStore';
type Options = {
teamId: string,
};
class DocumentsStore { class DocumentsStore {
@observable data: Object = {}; @observable data: Object = {};
@observable isLoaded: boolean = false; @observable isLoaded: boolean = false;
@ -19,9 +15,9 @@ class DocumentsStore {
/* Actions */ /* Actions */
@action fetchAll = async (): Promise<*> => { @action fetchAll = async (request?: string = 'list'): Promise<*> => {
try { try {
const res = await client.post('/documents.list'); const res = await client.post(`/documents.${request}`);
invariant(res && res.data, 'Document list not available'); invariant(res && res.data, 'Document list not available');
const { data } = res; const { data } = res;
runInAction('DocumentsStore#fetchAll', () => { runInAction('DocumentsStore#fetchAll', () => {
@ -35,18 +31,29 @@ class DocumentsStore {
}; };
this.isLoaded = true; this.isLoaded = true;
}); });
return data;
} catch (e) { } catch (e) {
this.errors.add('Failed to load documents'); this.errors.add('Failed to load documents');
} }
}; };
@action fetchRecent = async (): Promise<*> => {
const data = await this.fetchAll('recent');
console.log(data);
};
@action fetchViewed = async (): Promise<*> => {
const data = await this.fetchAll('viewed');
console.log(data);
};
@action fetch = async (id: string): Promise<*> => { @action fetch = async (id: string): Promise<*> => {
try { try {
const res = await client.post('/documents.info', { id }); const res = await client.post('/documents.info', { id });
invariant(res && res.data, 'Document not available'); invariant(res && res.data, 'Document not available');
const { data } = res; const { data } = res;
runInAction('DocumentsStore#fetch', () => { runInAction('DocumentsStore#fetch', () => {
this.update(new Document(data)); this.add(new Document(data));
this.isLoaded = true; this.isLoaded = true;
}); });
} catch (e) { } catch (e) {
@ -62,20 +69,11 @@ class DocumentsStore {
delete this.data[id]; delete this.data[id];
}; };
@action update = (document: Document): void => {
const existing = this.data[document.id];
this.data[document.id] = {
...existing,
document,
};
};
getById = (id: string): Document => { getById = (id: string): Document => {
return _.find(this.data, { id }); return _.find(this.data, { id });
}; };
constructor(options: Options) { constructor() {
this.errors = stores.errors; this.errors = stores.errors;
} }
} }