DocumentsStore WIP
This commit is contained in:
parent
f8a715dcef
commit
72fd39b494
|
@ -0,0 +1,20 @@
|
||||||
|
const storage = {};
|
||||||
|
|
||||||
|
export default {
|
||||||
|
setItem: function(key, value) {
|
||||||
|
storage[key] = value || '';
|
||||||
|
},
|
||||||
|
getItem: function(key) {
|
||||||
|
return key in storage ? storage[key] : null;
|
||||||
|
},
|
||||||
|
removeItem: function(key) {
|
||||||
|
delete storage[key];
|
||||||
|
},
|
||||||
|
get length() {
|
||||||
|
return Object.keys(storage).length;
|
||||||
|
},
|
||||||
|
key: function(i) {
|
||||||
|
var keys = Object.keys(storage);
|
||||||
|
return keys[i] || null;
|
||||||
|
},
|
||||||
|
};
|
|
@ -2,7 +2,7 @@
|
||||||
import { extendObservable, action, runInAction, computed } from 'mobx';
|
import { extendObservable, action, runInAction, computed } from 'mobx';
|
||||||
import invariant from 'invariant';
|
import invariant from 'invariant';
|
||||||
|
|
||||||
import ApiClient, { client } from 'utils/ApiClient';
|
import { client } from 'utils/ApiClient';
|
||||||
import stores from 'stores';
|
import stores from 'stores';
|
||||||
import ErrorsStore from 'stores/ErrorsStore';
|
import ErrorsStore from 'stores/ErrorsStore';
|
||||||
|
|
||||||
|
@ -17,14 +17,13 @@ class Document {
|
||||||
html: string;
|
html: string;
|
||||||
id: string;
|
id: string;
|
||||||
private: boolean;
|
private: boolean;
|
||||||
|
starred: boolean;
|
||||||
team: string;
|
team: string;
|
||||||
text: string;
|
text: string;
|
||||||
title: string;
|
title: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
updatedBy: User;
|
updatedBy: User;
|
||||||
url: string;
|
url: string;
|
||||||
|
|
||||||
client: ApiClient;
|
|
||||||
errors: ErrorsStore;
|
errors: ErrorsStore;
|
||||||
|
|
||||||
/* Computed */
|
/* Computed */
|
||||||
|
@ -56,7 +55,7 @@ class Document {
|
||||||
|
|
||||||
@action update = async () => {
|
@action update = async () => {
|
||||||
try {
|
try {
|
||||||
const res = await this.client.post('/documents.info', { id: this.id });
|
const res = await client.post('/documents.info', { id: this.id });
|
||||||
invariant(res && res.data, 'Document API response should be available');
|
invariant(res && res.data, 'Document API response should be available');
|
||||||
const { data } = res;
|
const { data } = res;
|
||||||
runInAction('Document#update', () => {
|
runInAction('Document#update', () => {
|
||||||
|
@ -73,7 +72,6 @@ class Document {
|
||||||
|
|
||||||
constructor(document: Document) {
|
constructor(document: Document) {
|
||||||
this.updateData(document);
|
this.updateData(document);
|
||||||
this.client = client;
|
|
||||||
this.errors = stores.errors;
|
this.errors = stores.errors;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
/* eslint-disable */
|
||||||
|
import Document from './Document';
|
||||||
|
|
||||||
|
describe('Document model', () => {
|
||||||
|
test('should initialize with data', () => {
|
||||||
|
const document = new Document({
|
||||||
|
id: 123,
|
||||||
|
title: 'Onboarding',
|
||||||
|
text: 'Some body text'
|
||||||
|
});
|
||||||
|
expect(document.title).toBe('Onboarding');
|
||||||
|
});
|
||||||
|
});
|
|
@ -22,7 +22,7 @@ class CollectionsStore {
|
||||||
|
|
||||||
/* Actions */
|
/* Actions */
|
||||||
|
|
||||||
@action fetch = async (): Promise<*> => {
|
@action fetchAll = async (): Promise<*> => {
|
||||||
try {
|
try {
|
||||||
const res = await this.client.post('/collections.list', {
|
const res = await this.client.post('/collections.list', {
|
||||||
id: this.teamId,
|
id: this.teamId,
|
||||||
|
|
|
@ -27,7 +27,7 @@ describe('CollectionsStore', () => {
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
|
|
||||||
await store.fetch();
|
await store.fetchAll();
|
||||||
|
|
||||||
expect(store.client.post).toHaveBeenCalledWith('/collections.list', {
|
expect(store.client.post).toHaveBeenCalledWith('/collections.list', {
|
||||||
id: 123,
|
id: 123,
|
||||||
|
@ -44,7 +44,7 @@ describe('CollectionsStore', () => {
|
||||||
add: jest.fn(),
|
add: jest.fn(),
|
||||||
};
|
};
|
||||||
|
|
||||||
await store.fetch();
|
await store.fetchAll();
|
||||||
|
|
||||||
expect(store.errors.add).toHaveBeenCalledWith(
|
expect(store.errors.add).toHaveBeenCalledWith(
|
||||||
'Failed to load collections'
|
'Failed to load collections'
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
// @flow
|
||||||
|
import { observable, action, runInAction } from 'mobx';
|
||||||
|
import { client } from 'utils/ApiClient';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import invariant from 'invariant';
|
||||||
|
|
||||||
|
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;
|
||||||
|
errors: ErrorsStore;
|
||||||
|
|
||||||
|
/* Actions */
|
||||||
|
|
||||||
|
@action fetchAll = async (): Promise<*> => {
|
||||||
|
try {
|
||||||
|
const res = await client.post('/documents.list');
|
||||||
|
invariant(res && res.data, 'Document list not available');
|
||||||
|
const { data } = res;
|
||||||
|
runInAction('DocumentsStore#fetchAll', () => {
|
||||||
|
const loaded = _.keyBy(
|
||||||
|
data.map(document => new Document(document)),
|
||||||
|
'id'
|
||||||
|
);
|
||||||
|
this.data = {
|
||||||
|
...this.data,
|
||||||
|
...loaded,
|
||||||
|
};
|
||||||
|
this.isLoaded = true;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
this.errors.add('Failed to load documents');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@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.isLoaded = true;
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
this.errors.add('Failed to load documents');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@action add = (document: Document): void => {
|
||||||
|
this.data[document.id] = document;
|
||||||
|
};
|
||||||
|
|
||||||
|
@action remove = (id: string): void => {
|
||||||
|
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) {
|
||||||
|
this.errors = stores.errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default DocumentsStore;
|
|
@ -1,6 +1,6 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { observable, action, computed } from 'mobx';
|
import { observable, action, computed } from 'mobx';
|
||||||
import type { Document } from 'types';
|
import Document from 'models/Document';
|
||||||
import Collection from 'models/Collection';
|
import Collection from 'models/Collection';
|
||||||
|
|
||||||
class UiStore {
|
class UiStore {
|
||||||
|
|
|
@ -2,10 +2,12 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { shallow } from 'enzyme';
|
import { shallow } from 'enzyme';
|
||||||
import toJson from 'enzyme-to-json';
|
import toJson from 'enzyme-to-json';
|
||||||
|
import localStorage from '../../__mocks__/localStorage';
|
||||||
|
|
||||||
const snap = children => {
|
const snap = children => {
|
||||||
const wrapper = shallow(children);
|
const wrapper = shallow(children);
|
||||||
expect(toJson(wrapper)).toMatchSnapshot();
|
expect(toJson(wrapper)).toMatchSnapshot();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
global.localStorage = localStorage;
|
||||||
global.snap = snap;
|
global.snap = snap;
|
||||||
|
|
Reference in New Issue