diff --git a/frontend/index.js b/frontend/index.js index c09fac02..10228033 100644 --- a/frontend/index.js +++ b/frontend/index.js @@ -11,6 +11,7 @@ import { import { Flex } from 'reflexbox'; import stores from 'stores'; +import DocumentsStore from 'stores/DocumentsStore'; import CollectionsStore from 'stores/CollectionsStore'; import 'normalize.css/normalize.css'; @@ -57,12 +58,14 @@ const Auth = ({ children }: AuthProps) => { const user = stores.auth.getUserStore(); authenticatedStores = { user, + documents: new DocumentsStore(), collections: new CollectionsStore({ teamId: user.team.id, }), }; - authenticatedStores.collections.fetch(); + authenticatedStores.documents.fetchAll(); + authenticatedStores.collections.fetchAll(); } return ( @@ -135,3 +138,5 @@ render( , document.getElementById('root') ); + +window.authenticatedStores = authenticatedStores; diff --git a/frontend/stores/DocumentsStore.js b/frontend/stores/DocumentsStore.js index 250d107b..fa74e4aa 100644 --- a/frontend/stores/DocumentsStore.js +++ b/frontend/stores/DocumentsStore.js @@ -8,10 +8,6 @@ import stores from 'stores'; import Document from 'models/Document'; import ErrorsStore from 'stores/ErrorsStore'; -type Options = { - teamId: string, -}; - class DocumentsStore { @observable data: Object = {}; @observable isLoaded: boolean = false; @@ -19,9 +15,9 @@ class DocumentsStore { /* Actions */ - @action fetchAll = async (): Promise<*> => { + @action fetchAll = async (request?: string = 'list'): Promise<*> => { try { - const res = await client.post('/documents.list'); + const res = await client.post(`/documents.${request}`); invariant(res && res.data, 'Document list not available'); const { data } = res; runInAction('DocumentsStore#fetchAll', () => { @@ -35,18 +31,29 @@ class DocumentsStore { }; this.isLoaded = true; }); + return data; } catch (e) { 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<*> => { try { const res = await client.post('/documents.info', { id }); invariant(res && res.data, 'Document not available'); const { data } = res; runInAction('DocumentsStore#fetch', () => { - this.update(new Document(data)); + this.add(new Document(data)); this.isLoaded = true; }); } catch (e) { @@ -62,20 +69,11 @@ class DocumentsStore { 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 => { return _.find(this.data, { id }); }; - constructor(options: Options) { + constructor() { this.errors = stores.errors; } }