From 6b566a568fd5ea7a44c2978da34ccc7f7deebe58 Mon Sep 17 00:00:00 2001 From: Jori Lallo Date: Tue, 23 May 2017 00:45:15 -0700 Subject: [PATCH] Collection model --- frontend/models/Collection.js | 50 ++++++++++++++++++ frontend/models/Collection.test.js | 58 +++++++++++++++++++++ frontend/scenes/Atlas/AtlasStore.js | 4 +- frontend/scenes/Dashboard/DashboardStore.js | 5 +- frontend/stores/index.js | 2 + frontend/types/index.js | 13 +---- 6 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 frontend/models/Collection.js create mode 100644 frontend/models/Collection.test.js diff --git a/frontend/models/Collection.js b/frontend/models/Collection.js new file mode 100644 index 00000000..65102812 --- /dev/null +++ b/frontend/models/Collection.js @@ -0,0 +1,50 @@ +// @flow +import { extendObservable, action, computed, runInAction } from 'mobx'; +import invariant from 'invariant'; +import _ from 'lodash'; + +import ApiClient, { client } from 'utils/ApiClient'; +import stores from 'stores'; +import ErrorsStore from 'stores/ErrorsStore'; +import type { NavigationNode } from 'types'; + +class Collection { + createdAt: string; + description: ?string; + id: string; + name: string; + type: 'atlas' | 'journal'; + navigationTree: NavigationNode; + updatedAt: string; + url: string; + + client: ApiClient; + errors: ErrorsStore; + + /* Actions */ + + @action update = async () => { + try { + const res = await this.client.post('/collections.info', { id: this.id }); + invariant(res && res.data, 'API response should be available'); + const { data } = res; + runInAction('Collection#update', () => { + this.updateData(data); + }); + } catch (e) { + this.errors.add('Collection failed loading'); + } + }; + + updateData(data: Collection) { + extendObservable(this, data); + } + + constructor(collection: Collection) { + this.updateData(collection); + this.client = client; + this.errors = stores.errors; + } +} + +export default Collection; diff --git a/frontend/models/Collection.test.js b/frontend/models/Collection.test.js new file mode 100644 index 00000000..a9a1857d --- /dev/null +++ b/frontend/models/Collection.test.js @@ -0,0 +1,58 @@ +/* eslint-disable */ +import Collection from './Collection'; + +jest.mock('utils/ApiClient', () => ({ + client: { post: {} }, +})); +jest.mock('stores', () => ({ errors: {} })); + +describe('Collection model', () => { + test('should initialize with data', () => { + const collection = new Collection({ + id: 123, + name: 'Engineering', + }); + expect(collection.name).toBe('Engineering'); + }); + + describe('#update', () => { + test('should update', async () => { + const collection = new Collection({ + id: 123, + name: 'Engineering', + }); + collection.client = { + post: jest.fn(() => ({ + data: { + name: 'New collection', + }, + })), + }; + + await collection.update(); + + expect(collection.client.post).toHaveBeenCalledWith('/collections.info', { + id: 123, + }); + expect(collection.name).toBe('New collection'); + }); + + test('should report errors', async () => { + const collection = new Collection({ + id: 123, + }); + collection.client = { + post: jest.fn(() => Promise.reject), + }; + collection.errors = { + add: jest.fn(), + }; + + await collection.update(); + + expect(collection.errors.add).toHaveBeenCalledWith( + 'Collection failed loading' + ); + }); + }); +}); diff --git a/frontend/scenes/Atlas/AtlasStore.js b/frontend/scenes/Atlas/AtlasStore.js index 9d6067af..8aa446e8 100644 --- a/frontend/scenes/Atlas/AtlasStore.js +++ b/frontend/scenes/Atlas/AtlasStore.js @@ -2,7 +2,7 @@ import { observable, action, computed } from 'mobx'; import invariant from 'invariant'; import { client } from 'utils/ApiClient'; -import type { Collection } from 'types'; +import Collection from 'models/Collection'; const store = new class AtlasStore { @observable collection: ?(Collection & { recentDocuments?: Object[] }); @@ -25,7 +25,7 @@ const store = new class AtlasStore { const res = await client.get('/collections.info', { id }); invariant(res && res.data, 'Data should be available'); const { data } = res; - this.collection = data; + this.collection = new Collection(data); successCallback(data); } catch (e) { console.error('Something went wrong'); diff --git a/frontend/scenes/Dashboard/DashboardStore.js b/frontend/scenes/Dashboard/DashboardStore.js index 2f57b31e..35071254 100644 --- a/frontend/scenes/Dashboard/DashboardStore.js +++ b/frontend/scenes/Dashboard/DashboardStore.js @@ -2,7 +2,8 @@ import { observable, action, runInAction } from 'mobx'; import invariant from 'invariant'; import { client } from 'utils/ApiClient'; -import type { Pagination, Collection } from 'types'; +import type { Pagination } from 'types'; +import Collection from 'models/Collection'; type Options = { team: Object, @@ -30,7 +31,7 @@ class DashboardStore { ); const { data, pagination } = res; runInAction('fetchCollections', () => { - this.collections = data; + this.collections = data.map(collection => new Collection(collection)); this.pagination = pagination; }); } catch (e) { diff --git a/frontend/stores/index.js b/frontend/stores/index.js index f90a12f9..8fd69701 100644 --- a/frontend/stores/index.js +++ b/frontend/stores/index.js @@ -2,10 +2,12 @@ import { autorunAsync } from 'mobx'; import UserStore, { USER_STORE } from './UserStore'; import UiStore, { UI_STORE } from './UiStore'; +import ErrorsStore from './ErrorsStore'; const stores = { user: new UserStore(), ui: new UiStore(), + errors: new ErrorsStore(), }; // Persist stores to localStorage diff --git a/frontend/types/index.js b/frontend/types/index.js index ac6ad694..ac157676 100644 --- a/frontend/types/index.js +++ b/frontend/types/index.js @@ -19,20 +19,9 @@ export type NavigationNode = { children: Array, }; -export type Collection = { - createdAt: string, - description: ?string, - id: string, - name: string, - type: 'atlas' | 'journal', - navigationTree: NavigationNode, - updatedAt: string, - url: string, -}; - export type Document = { collaborators: Array, - collection: Collection, + collection: Object, createdAt: string, createdBy: string, html: string,