1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

Use parameters instead of meta objects in state to pass information to sources

This commit is contained in:
Brett Sun 2016-01-15 13:55:58 +01:00
parent a6463087c0
commit e5a8ebd623
10 changed files with 90 additions and 77 deletions

View File

@ -7,9 +7,9 @@ import EditionActions from '../actions/edition_actions';
const CoaSource = { const CoaSource = {
lookupCoa: { lookupCoa: {
remote(state) { remote(state, coaId) {
return requests return requests
.get('coa', { id: state.edition.coa }) .get('coa', { id: coaId })
.then((res) => { .then((res) => {
// If no coa is found here, fake a 404 error so the error action can pick it up // If no coa is found here, fake a 404 error so the error action can pick it up
return (res && res.coa) ? res : Promise.reject({ json: { status: 404 } }); return (res && res.coa) ? res : Promise.reject({ json: { status: 404 } });
@ -20,10 +20,11 @@ const CoaSource = {
error: EditionActions.errorCoa error: EditionActions.errorCoa
}, },
performCreateCoa: { performCreateCoaForEdition: {
remote(state) { remote(state, editionId) {
return requests.post('coa_create', {body: { bitcoin_id: state.edition.bitcoin_id }}); return requests.post('coa_create', { body: { bitcoin_id: editionId } });
}, },
success: EditionActions.successFetchCoa, success: EditionActions.successFetchCoa,
error: EditionActions.errorCoa error: EditionActions.errorCoa
} }

View File

@ -7,8 +7,8 @@ import EditionActions from '../actions/edition_actions';
const EditionSource = { const EditionSource = {
lookupEdition: { lookupEdition: {
remote(state) { remote(state, editionId) {
return requests.get('edition', { bitcoin_id: state.editionMeta.idToFetch }); return requests.get('edition', { bitcoin_id: editionId });
}, },
success: EditionActions.successFetchEdition, success: EditionActions.successFetchEdition,

View File

@ -13,12 +13,14 @@ const UserSource = {
}, },
local(state) { local(state) {
return state.currentUser && state.currentUser.email ? state : {}; return !Object.keys(state.currentUser).length ? state : {};
}, },
success: UserActions.successFetchCurrentUser, success: UserActions.successFetchCurrentUser,
error: UserActions.errorCurrentUser, error: UserActions.errorCurrentUser,
shouldFetch(state) {
return state.userMeta.invalidateCache || state.currentUser && !state.currentUser.email; shouldFetch(state, invalidateCache) {
return invalidateCache || !Object.keys(state.currentUser).length;
} }
}, },
@ -26,6 +28,7 @@ const UserSource = {
remote() { remote() {
return requests.get(ApiUrls.users_logout); return requests.get(ApiUrls.users_logout);
}, },
success: UserActions.successLogoutCurrentUser, success: UserActions.successLogoutCurrentUser,
error: UserActions.errorCurrentUser error: UserActions.errorCurrentUser
} }

View File

@ -10,13 +10,16 @@ const WebhookSource = {
remote() { remote() {
return requests.get('webhooks'); return requests.get('webhooks');
}, },
local(state) { local(state) {
return state.webhooks && !Object.keys(state.webhooks).length ? state : {}; return !Object.keys(state.webhooks).length ? state : {};
}, },
success: WebhookActions.successFetchWebhooks, success: WebhookActions.successFetchWebhooks,
error: WebhookActions.errorWebhooks, error: WebhookActions.errorWebhooks,
shouldFetch(state) {
return state.webhookMeta.invalidateCache || state.webhooks && !Object.keys(state.webhooks).length; shouldFetch(state, invalidateCache) {
return invalidateCache || !Object.keys(state.webhooks).length;
} }
}, },
@ -24,20 +27,24 @@ const WebhookSource = {
remote() { remote() {
return requests.get('webhooks_events'); return requests.get('webhooks_events');
}, },
local(state) { local(state) {
return state.webhookEvents && !Object.keys(state.webhookEvents).length ? state : {}; return !Object.keys(state.webhookEvents).length ? state : {};
}, },
success: WebhookActions.successFetchWebhookEvents, success: WebhookActions.successFetchWebhookEvents,
error: WebhookActions.errorWebhookEvents, error: WebhookActions.errorWebhookEvents,
shouldFetch(state) {
return state.webhookEventsMeta.invalidateCache || state.webhookEvents && !Object.keys(state.webhookEvents).length; shouldFetch(state, invalidateCache) {
return invalidateCache || !Object.keys(state.webhookEvents).length;
} }
}, },
performRemoveWebhook: { performRemoveWebhook: {
remote(state) { remote(state, webhookId) {
return requests.delete('webhook', {'webhook_id': state.webhookMeta.idToDelete }); return requests.delete('webhook', { 'webhook_id': webhookId });
}, },
success: WebhookActions.successRemoveWebhook, success: WebhookActions.successRemoveWebhook,
error: WebhookActions.errorWebhooks error: WebhookActions.errorWebhooks
} }

View File

@ -9,15 +9,18 @@ import { getSubdomain } from '../utils/general_utils';
const WhitelabelSource = { const WhitelabelSource = {
lookupWhitelabel: { lookupWhitelabel: {
remote() { remote() {
return requests.get('whitelabel_settings', {'subdomain': getSubdomain()}); return requests.get('whitelabel_settings', { 'subdomain': getSubdomain() });
}, },
local(state) { local(state) {
return Object.keys(state.whitelabel).length > 0 ? state : {}; return Object.keys(state.whitelabel).length ? state : {};
}, },
success: WhitelabelActions.successFetchWhitelabel, success: WhitelabelActions.successFetchWhitelabel,
error: WhitelabelActions.errorWhitelabel, error: WhitelabelActions.errorWhitelabel,
shouldFetch(state) {
return state.whitelabelMeta.invalidateCache || Object.keys(state.whitelabel).length === 0; shouldFetch(state, invalidateCache) {
return invalidateCache || !Object.keys(state.whitelabel).length;
} }
} }
}; };

View File

@ -14,8 +14,7 @@ class EditionStore {
constructor() { constructor() {
this.edition = {}; this.edition = {};
this.editionMeta = { this.editionMeta = {
err: null, err: null
idToFetch: null
}; };
this.coaMeta = { this.coaMeta = {
err: null err: null
@ -25,26 +24,27 @@ class EditionStore {
this.registerAsync(mergeOptions(EditionSource, CoaSource)); this.registerAsync(mergeOptions(EditionSource, CoaSource));
} }
onFetchEdition(idToFetch) { onFetchEdition(editionId) {
this.editionMeta.idToFetch = idToFetch; this.getInstance().lookupEdition(editionId);
this.getInstance().lookupEdition();
} }
onSuccessFetchEdition({ edition }) { onSuccessFetchEdition({ edition }) {
if (edition) { if (edition) {
this.edition = edition; this.edition = edition;
this.editionMeta.err = null; this.editionMeta.err = null;
this.editionMeta.idToFetch = null;
if (this.edition.coa && this.edition.acl.acl_coa && // Also fetch coa if allowed
typeof this.edition.coa.constructor !== Object) { if (edition.acl.acl_coa) {
this.getInstance().lookupCoa(); if (edition.coa && typeof edition.coa.constructor !== Object) {
} else if (!this.edition.coa && this.edition.acl.acl_coa) { this.getInstance().lookupCoa(edition.coa);
this.getInstance().performCreateCoa(); } else if (!edition.coa) {
this.getInstance().performCreateCoaForEdition(edition.bitcoin_id);
}
} }
} else { } else {
this.editionMeta.err = new Error('Problem fetching the edition'); this.editionMeta.err = new Error('Problem fetching the edition');
console.logGlobal(this.editionMeta.err);
} }
} }
@ -54,6 +54,7 @@ class EditionStore {
this.coaMeta.err = null; this.coaMeta.err = null;
} else { } else {
this.coaMeta.err = new Error('Problem generating/fetching the COA'); this.coaMeta.err = new Error('Problem generating/fetching the COA');
console.logGlobal(this.coaMeta.err);
} }
} }
@ -69,14 +70,16 @@ class EditionStore {
} }
onErrorEdition(err) { onErrorEdition(err) {
console.logGlobal(err);
this.editionMeta.err = err; this.editionMeta.err = err;
} }
onErrorCoa(err) { onErrorCoa(err) {
// On 404s, create a new COA as the COA has not been made yet // On 404s, create a new COA as the COA has not been made yet
if (err && err.json && err.json.status === 404) { if (err && err.json && err.json.status === 404) {
this.getInstance().performCreateCoa(); this.getInstance().performCreateCoaForEdition(this.edition.bitcoin_id);
} else { } else {
console.logGlobal(err);
this.coaMeta.err = err; this.coaMeta.err = err;
} }
} }

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
import { altUser } from '../alt'; import { altUser } from '../alt';
import UserActions from '../actions/user_actions'; import UserActions from '../actions/user_actions';
import UserSource from '../sources/user_source'; import UserSource from '../sources/user_source';
@ -10,7 +11,6 @@ class UserStore {
constructor() { constructor() {
this.currentUser = {}; this.currentUser = {};
this.userMeta = { this.userMeta = {
invalidateCache: false,
err: null err: null
}; };
@ -19,15 +19,12 @@ class UserStore {
} }
onFetchCurrentUser(invalidateCache) { onFetchCurrentUser(invalidateCache) {
this.userMeta.invalidateCache = invalidateCache; if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser(invalidateCache);
if(!this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser();
} }
} }
onSuccessFetchCurrentUser({users: [user = {}]}) { onSuccessFetchCurrentUser({ users: [ user = {} ] }) {
this.userMeta.invalidateCache = false;
this.userMeta.err = null; this.userMeta.err = null;
this.currentUser = user; this.currentUser = user;
} }
@ -37,6 +34,7 @@ class UserStore {
} }
onSuccessLogoutCurrentUser() { onSuccessLogoutCurrentUser() {
this.userMeta.err = null;
this.currentUser = {}; this.currentUser = {};
} }

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
import { alt } from '../alt'; import { alt } from '../alt';
import WebhookActions from '../actions/webhook_actions'; import WebhookActions from '../actions/webhook_actions';
import WebhookSource from '../sources/webhook_source'; import WebhookSource from '../sources/webhook_source';
@ -10,12 +11,9 @@ class WebhookStore {
this.webhooks = []; this.webhooks = [];
this.webhookEvents = []; this.webhookEvents = [];
this.webhookMeta = { this.webhookMeta = {
invalidateCache: false, err: null
err: null,
idToDelete: null
}; };
this.webhookEventsMeta = { this.webhookEventsMeta = {
invalidateCache: false,
err: null err: null
}; };
@ -24,54 +22,46 @@ class WebhookStore {
} }
onFetchWebhooks(invalidateCache) { onFetchWebhooks(invalidateCache) {
this.webhookMeta.invalidateCache = invalidateCache; if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWebhooks(); this.getInstance().lookupWebhooks(invalidateCache);
}
} }
onSuccessFetchWebhooks({ webhooks = [] }) { onSuccessFetchWebhooks({ webhooks = [] }) {
this.webhookMeta.invalidateCache = false;
this.webhookMeta.err = null; this.webhookMeta.err = null;
this.webhooks = webhooks; this.webhooks = webhooks;
this.webhookEventsMeta.invalidateCache = true; this.onFetchWebhookEvents(true);
this.getInstance().lookupWebhookEvents();
} }
onFetchWebhookEvents(invalidateCache) { onFetchWebhookEvents(invalidateCache) {
this.webhookEventsMeta.invalidateCache = invalidateCache; if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWebhookEvents(); this.getInstance().lookupWebhookEvents(invalidateCache);
}
} }
onSuccessFetchWebhookEvents({ events }) { onSuccessFetchWebhookEvents({ events }) {
this.webhookEventsMeta.invalidateCache = false;
this.webhookEventsMeta.err = null; this.webhookEventsMeta.err = null;
// remove all events that have already been used. // remove all events that have already been used.
const usedEvents = this.webhooks const usedEvents = this.webhooks
.reduce((tempUsedEvents, webhook) => { .reduce((tempUsedEvents, webhook) => {
tempUsedEvents.push(webhook.event.split('.')[0]); tempUsedEvents.push(webhook.event.split('.')[0]);
return tempUsedEvents; return tempUsedEvents;
}, []); }, []);
this.webhookEvents = events.filter((event) => { this.webhookEvents = events.filter((event) => {
return usedEvents.indexOf(event) === -1; return usedEvents.indexOf(event) === -1;
}); });
} }
onRemoveWebhook(id) { onRemoveWebhook(webhookId) {
this.webhookMeta.invalidateCache = true; this.getInstance().performRemoveWebhook(webhookId);
this.webhookMeta.idToDelete = id;
if(!this.getInstance().isLoading()) {
this.getInstance().performRemoveWebhook();
}
} }
onSuccessRemoveWebhook() { onSuccessRemoveWebhook() {
this.webhookMeta.idToDelete = null; this.getInstance().lookupWebhooks(true);
if(!this.getInstance().isLoading()) {
this.getInstance().lookupWebhooks();
}
} }
onErrorWebhooks(err) { onErrorWebhooks(err) {

View File

@ -1,7 +1,9 @@
'use strict'; 'use strict';
import { altWhitelabel } from '../alt'; import { altWhitelabel } from '../alt';
import WhitelabelActions from '../actions/whitelabel_actions'; import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelSource from '../sources/whitelabel_source'; import WhitelabelSource from '../sources/whitelabel_source';
@ -9,7 +11,6 @@ class WhitelabelStore {
constructor() { constructor() {
this.whitelabel = {}; this.whitelabel = {};
this.whitelabelMeta = { this.whitelabelMeta = {
invalidateCache: false,
err: null err: null
}; };
@ -18,15 +19,12 @@ class WhitelabelStore {
} }
onFetchWhitelabel(invalidateCache) { onFetchWhitelabel(invalidateCache) {
this.whitelabelMeta.invalidateCache = invalidateCache; if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWhitelabel(invalidateCache);
if(!this.getInstance().isLoading()) {
this.getInstance().lookupWhitelabel();
} }
} }
onSuccessFetchWhitelabel({ whitelabel = {} }) { onSuccessFetchWhitelabel({ whitelabel = {} }) {
this.whitelabelMeta.invalidateCache = false;
this.whitelabelMeta.err = null; this.whitelabelMeta.err = null;
this.whitelabel = whitelabel; this.whitelabel = whitelabel;
} }

10
js/utils/store_utils.js Normal file
View File

@ -0,0 +1,10 @@
'use strict'
export function onChangeOnce(component, store) {
const onChange = (state) => {
component.setState(state);
store.unlisten(onChange);
};
store.listen(onChange);
}