This commit is contained in:
Tom Moor 2017-06-27 21:20:09 -07:00
parent 72fd39b494
commit 4c3f4fea16
No known key found for this signature in database
GPG Key ID: 495FE29B5F21BD41
2 changed files with 21 additions and 18 deletions

View File

@ -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(
</div>,
document.getElementById('root')
);
window.authenticatedStores = authenticatedStores;

View File

@ -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;
}
}