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

Merge branch 'use-parameters-in-calls-to-sources' into AD-1558-piece-and-edition-detail-show-stale-data

Merge with update to the pattern used in sources
This commit is contained in:
Brett Sun 2016-01-15 14:20:18 +01:00
commit c6071d8ab4
11 changed files with 119 additions and 84 deletions

View File

@ -22,12 +22,8 @@ refreshApplicationToken
we cannot repeat this for a sources' methods as patterns like this would emerge in the stores: we cannot repeat this for a sources' methods as patterns like this would emerge in the stores:
```javascript ```javascript
onFetchCurrentUser(invalidateCache) { onFetchCurrentUser() {
this.invalidateCache = invalidateCache; this.getInstance().fetchCurrentUser(); // does not call a flux "action" but a method in user_source.js - which is confusing
if(!this.getInstance().isLoading()) {
this.getInstance().fetchCurrentUser(); // does not call a flux "action" but a method in user_source.js - which is confusing
}
} }
``` ```
@ -67,4 +63,4 @@ lookupCurrentUser: {
UserActions.logoutCurrentUser UserActions.logoutCurrentUser
UserStore.onLogoutCurrentUser UserStore.onLogoutCurrentUser
UserSource.performLogoutCurrentUser UserSource.performLogoutCurrentUser
``` ```

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,
@ -16,4 +16,4 @@ const EditionSource = {
} }
}; };
export default EditionSource; export default EditionSource;

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,9 +28,10 @@ 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
} }
}; };
export default UserSource; export default UserSource;

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,23 +27,27 @@ 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
} }
}; };
export default WebhookSource; export default WebhookSource;

View File

@ -9,17 +9,20 @@ 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;
} }
} }
}; };
export default WhitelabelSource; export default WhitelabelSource;

View File

@ -24,8 +24,7 @@ class EditionStore {
getInitialState() { getInitialState() {
this.edition = {}; this.edition = {};
this.editionMeta = { this.editionMeta = {
err: null, err: null
idToFetch: null
}; };
this.coaMeta = { this.coaMeta = {
err: null err: null
@ -38,26 +37,30 @@ class EditionStore {
}; };
} }
onFetchEdition(idToFetch) { onFetchEdition(editionId) {
this.editionMeta.idToFetch = idToFetch; this.getInstance().lookupEdition(editionId);
this.getInstance().lookupEdition(); // Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
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);
} }
} }
@ -67,18 +70,21 @@ 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);
} }
} }
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,24 +19,30 @@ 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();
} }
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
onSuccessFetchCurrentUser({users: [user = {}]}) { onSuccessFetchCurrentUser({ users: [ user = {} ] }) {
this.userMeta.invalidateCache = false;
this.userMeta.err = null; this.userMeta.err = null;
this.currentUser = user; this.currentUser = user;
} }
onLogoutCurrentUser() { onLogoutCurrentUser() {
this.getInstance().performLogoutCurrentUser(); this.getInstance().performLogoutCurrentUser();
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
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,57 @@ class WebhookStore {
} }
onFetchWebhooks(invalidateCache) { onFetchWebhooks(invalidateCache) {
this.webhookMeta.invalidateCache = invalidateCache; if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWebhooks(); this.getInstance().lookupWebhooks(invalidateCache);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
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);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
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()) { // Prevent alt from sending an empty change event when a request is sent
this.getInstance().performRemoveWebhook(); // off to the source
} this.preventDefault();
} }
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,16 @@ 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();
} }
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
} }
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);
}