Merge pull request #122 from jorilallo/new-collection
Create a collection
This commit is contained in:
@@ -3,12 +3,16 @@ import { extendObservable, action, computed, runInAction } from 'mobx';
|
||||
import invariant from 'invariant';
|
||||
import _ from 'lodash';
|
||||
|
||||
import ApiClient, { client } from 'utils/ApiClient';
|
||||
import { client } from 'utils/ApiClient';
|
||||
import stores from 'stores';
|
||||
import ErrorsStore from 'stores/ErrorsStore';
|
||||
import type { NavigationNode } from 'types';
|
||||
|
||||
class Collection {
|
||||
isSaving: boolean = false;
|
||||
hasPendingChanges: boolean = false;
|
||||
errors: ErrorsStore;
|
||||
|
||||
createdAt: string;
|
||||
description: ?string;
|
||||
id: string;
|
||||
@@ -18,9 +22,6 @@ class Collection {
|
||||
updatedAt: string;
|
||||
url: string;
|
||||
|
||||
client: ApiClient;
|
||||
errors: ErrorsStore;
|
||||
|
||||
/* Computed */
|
||||
|
||||
@computed get entryUrl(): string {
|
||||
@@ -29,26 +30,60 @@ class Collection {
|
||||
|
||||
/* Actions */
|
||||
|
||||
@action update = async () => {
|
||||
@action fetch = async () => {
|
||||
try {
|
||||
const res = await this.client.post('/collections.info', { id: this.id });
|
||||
const res = await client.post('/collections.info', { id: this.id });
|
||||
invariant(res && res.data, 'API response should be available');
|
||||
const { data } = res;
|
||||
runInAction('Collection#update', () => {
|
||||
runInAction('Collection#fetch', () => {
|
||||
this.updateData(data);
|
||||
});
|
||||
} catch (e) {
|
||||
this.errors.add('Collection failed loading');
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
updateData(data: Collection) {
|
||||
@action save = async () => {
|
||||
if (this.isSaving) return this;
|
||||
this.isSaving = true;
|
||||
|
||||
try {
|
||||
let res;
|
||||
if (this.id) {
|
||||
res = await client.post('/collections.update', {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
description: this.description,
|
||||
});
|
||||
} else {
|
||||
res = await client.post('/collections.create', {
|
||||
name: this.name,
|
||||
description: this.description,
|
||||
});
|
||||
}
|
||||
invariant(res && res.data, 'Data should be available');
|
||||
this.updateData({
|
||||
...res.data,
|
||||
hasPendingChanges: false,
|
||||
});
|
||||
} catch (e) {
|
||||
this.errors.add('Collection failed saving');
|
||||
return false;
|
||||
} finally {
|
||||
this.isSaving = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
updateData(data: Object = {}) {
|
||||
extendObservable(this, data);
|
||||
}
|
||||
|
||||
constructor(collection: Collection) {
|
||||
constructor(collection: Object = {}) {
|
||||
this.updateData(collection);
|
||||
this.client = client;
|
||||
this.errors = stores.errors;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
/* eslint-disable */
|
||||
import Collection from './Collection';
|
||||
|
||||
jest.mock('utils/ApiClient', () => ({
|
||||
client: { post: {} },
|
||||
}));
|
||||
jest.mock('stores', () => ({ errors: {} }));
|
||||
const { client } = require('utils/ApiClient');
|
||||
|
||||
describe('Collection model', () => {
|
||||
test('should initialize with data', () => {
|
||||
@@ -15,40 +11,34 @@ describe('Collection model', () => {
|
||||
expect(collection.name).toBe('Engineering');
|
||||
});
|
||||
|
||||
describe('#update', () => {
|
||||
test('should update', async () => {
|
||||
describe('#fetch', () => {
|
||||
test('should update data', async () => {
|
||||
client.post = jest.fn(() => ({
|
||||
data: {
|
||||
name: 'New collection',
|
||||
},
|
||||
}))
|
||||
|
||||
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,
|
||||
});
|
||||
await collection.fetch();
|
||||
expect(collection.name).toBe('New collection');
|
||||
});
|
||||
|
||||
test('should report errors', async () => {
|
||||
client.post = jest.fn(() => Promise.reject())
|
||||
|
||||
const collection = new Collection({
|
||||
id: 123,
|
||||
});
|
||||
collection.client = {
|
||||
post: jest.fn(() => Promise.reject),
|
||||
};
|
||||
collection.errors = {
|
||||
add: jest.fn(),
|
||||
};
|
||||
|
||||
await collection.update();
|
||||
await collection.fetch();
|
||||
|
||||
expect(collection.errors.add).toHaveBeenCalledWith(
|
||||
'Collection failed loading'
|
||||
|
||||
@@ -30,6 +30,7 @@ class Document {
|
||||
starred: boolean = false;
|
||||
text: string = '';
|
||||
title: string = 'Untitled document';
|
||||
parentDocument: ?Document;
|
||||
updatedAt: string;
|
||||
updatedBy: User;
|
||||
url: string;
|
||||
@@ -151,13 +152,14 @@ class Document {
|
||||
return this;
|
||||
};
|
||||
|
||||
updateData(data: Object | Document) {
|
||||
updateData(data: Object = {}, dirty: boolean = false) {
|
||||
if (data.text) data.title = parseHeader(data.text);
|
||||
if (dirty) data.hasPendingChanges = true;
|
||||
extendObservable(this, data);
|
||||
}
|
||||
|
||||
constructor(document?: Object = {}) {
|
||||
this.updateData(document);
|
||||
constructor(data?: Object = {}) {
|
||||
this.updateData(data);
|
||||
this.errors = stores.errors;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user