Merge ErrorsStore into UiStore

This commit is contained in:
Tom Moor
2018-05-31 11:42:39 -07:00
parent 55e1451160
commit f633f63a61
18 changed files with 105 additions and 158 deletions

View File

@@ -6,13 +6,13 @@ import BaseModel from 'models/BaseModel';
import Document from 'models/Document';
import { client } from 'utils/ApiClient';
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
import UiStore from 'stores/UiStore';
import type { NavigationNode } from 'types';
class Collection extends BaseModel {
isSaving: boolean = false;
hasPendingChanges: boolean = false;
errors: ErrorsStore;
ui: UiStore;
data: Object;
createdAt: string;
@@ -79,7 +79,7 @@ class Collection extends BaseModel {
this.updateData(data);
});
} catch (e) {
this.errors.add('Collection failed loading');
this.ui.showToast('Collection failed loading');
}
return this;
@@ -112,7 +112,7 @@ class Collection extends BaseModel {
this.hasPendingChanges = false;
});
} catch (e) {
this.errors.add('Collection failed saving');
this.ui.showToast('Collection failed saving');
return false;
} finally {
this.isSaving = false;
@@ -128,7 +128,7 @@ class Collection extends BaseModel {
this.emit('collections.delete', { id: this.id });
return true;
} catch (e) {
this.errors.add('Collection failed to delete');
this.ui.showToast('Collection failed to delete');
}
return false;
};
@@ -143,7 +143,7 @@ class Collection extends BaseModel {
super();
this.updateData(collection);
this.errors = stores.errors;
this.ui = stores.ui;
this.on('documents.delete', (data: { collectionId: string }) => {
if (data.collectionId === this.id) this.fetch();

View File

@@ -28,22 +28,5 @@ describe('Collection model', () => {
expect(client.post).toHaveBeenCalledWith('/collections.info', { id: 123 });
expect(collection.name).toBe('New collection');
});
test('should report errors', async () => {
client.post = jest.fn(() => Promise.reject())
const collection = new Collection({
id: 123,
});
collection.errors = {
add: jest.fn(),
};
await collection.fetch();
expect(collection.errors.add).toHaveBeenCalledWith(
'Collection failed loading'
);
});
});
});

View File

@@ -4,7 +4,7 @@ import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
import UiStore from 'stores/UiStore';
import parseTitle from '../../shared/utils/parseTitle';
import type { User } from 'types';
@@ -16,7 +16,7 @@ type SaveOptions = { publish?: boolean, done?: boolean, autosave?: boolean };
class Document extends BaseModel {
isSaving: boolean = false;
hasPendingChanges: boolean = false;
errors: ErrorsStore;
ui: UiStore;
collaborators: User[];
collection: $Shape<Collection>;
@@ -107,7 +107,7 @@ class Document extends BaseModel {
this.shareUrl = res.data.url;
} catch (e) {
this.errors.add('Document failed to share');
this.ui.showToast('Document failed to share');
}
};
@@ -118,7 +118,7 @@ class Document extends BaseModel {
await client.post('/documents.pin', { id: this.id });
} catch (e) {
this.pinned = false;
this.errors.add('Document failed to pin');
this.ui.showToast('Document failed to pin');
}
};
@@ -129,7 +129,7 @@ class Document extends BaseModel {
await client.post('/documents.unpin', { id: this.id });
} catch (e) {
this.pinned = true;
this.errors.add('Document failed to unpin');
this.ui.showToast('Document failed to unpin');
}
};
@@ -140,7 +140,7 @@ class Document extends BaseModel {
await client.post('/documents.star', { id: this.id });
} catch (e) {
this.starred = false;
this.errors.add('Document failed star');
this.ui.showToast('Document failed star');
}
};
@@ -151,7 +151,7 @@ class Document extends BaseModel {
await client.post('/documents.unstar', { id: this.id });
} catch (e) {
this.starred = false;
this.errors.add('Document failed unstar');
this.ui.showToast('Document failed unstar');
}
};
@@ -161,7 +161,7 @@ class Document extends BaseModel {
try {
await client.post('/views.create', { id: this.id });
} catch (e) {
this.errors.add('Document failed to record view');
this.ui.showToast('Document failed to record view');
}
};
@@ -175,7 +175,7 @@ class Document extends BaseModel {
this.updateData(data);
});
} catch (e) {
this.errors.add('Document failed loading');
this.ui.showToast('Document failed loading');
}
};
@@ -228,7 +228,7 @@ class Document extends BaseModel {
});
}
} catch (e) {
this.errors.add('Document failed to save');
this.ui.showToast('Document failed to save');
} finally {
this.isSaving = false;
}
@@ -250,7 +250,7 @@ class Document extends BaseModel {
collectionId: this.collection.id,
});
} catch (e) {
this.errors.add('Error while moving the document');
this.ui.showToast('Error while moving the document');
}
return;
};
@@ -265,7 +265,7 @@ class Document extends BaseModel {
});
return true;
} catch (e) {
this.errors.add('Error while deleting the document');
this.ui.showToast('Error while deleting the document');
}
return false;
};
@@ -294,7 +294,7 @@ class Document extends BaseModel {
super();
this.updateData(data);
this.errors = stores.errors;
this.ui = stores.ui;
}
}

View File

@@ -4,7 +4,7 @@ import { extendObservable, action } from 'mobx';
import BaseModel from 'models/BaseModel';
import { client } from 'utils/ApiClient';
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
import UiStore from 'stores/UiStore';
type Settings = {
url: string,
@@ -15,7 +15,7 @@ type Settings = {
type Events = 'documents.create' | 'collections.create';
class Integration extends BaseModel {
errors: ErrorsStore;
ui: UiStore;
id: string;
serviceId: string;
@@ -29,7 +29,7 @@ class Integration extends BaseModel {
await client.post('/integrations.update', { id: this.id, ...data });
extendObservable(this, data);
} catch (e) {
this.errors.add('Integration failed to update');
this.ui.showToast('Integration failed to update');
}
return false;
};
@@ -41,7 +41,7 @@ class Integration extends BaseModel {
this.emit('integrations.delete', { id: this.id });
return true;
} catch (e) {
this.errors.add('Integration failed to delete');
this.ui.showToast('Integration failed to delete');
}
return false;
};
@@ -50,7 +50,7 @@ class Integration extends BaseModel {
super();
extendObservable(this, data);
this.errors = stores.errors;
this.ui = stores.ui;
}
}