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:
```javascript
onFetchCurrentUser(invalidateCache) {
this.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) {
this.getInstance().fetchCurrentUser(); // does not call a flux "action" but a method in user_source.js - which is confusing
}
onFetchCurrentUser() {
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
UserStore.onLogoutCurrentUser
UserSource.performLogoutCurrentUser
```
```

View File

@ -7,9 +7,9 @@ import EditionActions from '../actions/edition_actions';
const CoaSource = {
lookupCoa: {
remote(state) {
remote(state, coaId) {
return requests
.get('coa', { id: state.edition.coa })
.get('coa', { id: coaId })
.then((res) => {
// 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 } });
@ -20,10 +20,11 @@ const CoaSource = {
error: EditionActions.errorCoa
},
performCreateCoa: {
remote(state) {
return requests.post('coa_create', {body: { bitcoin_id: state.edition.bitcoin_id }});
performCreateCoaForEdition: {
remote(state, editionId) {
return requests.post('coa_create', { body: { bitcoin_id: editionId } });
},
success: EditionActions.successFetchCoa,
error: EditionActions.errorCoa
}

View File

@ -7,8 +7,8 @@ import EditionActions from '../actions/edition_actions';
const EditionSource = {
lookupEdition: {
remote(state) {
return requests.get('edition', { bitcoin_id: state.editionMeta.idToFetch });
remote(state, editionId) {
return requests.get('edition', { bitcoin_id: editionId });
},
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) {
return state.currentUser && state.currentUser.email ? state : {};
return !Object.keys(state.currentUser).length ? state : {};
},
success: UserActions.successFetchCurrentUser,
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() {
return requests.get(ApiUrls.users_logout);
},
success: UserActions.successLogoutCurrentUser,
error: UserActions.errorCurrentUser
}
};
export default UserSource;
export default UserSource;

View File

@ -10,13 +10,16 @@ const WebhookSource = {
remote() {
return requests.get('webhooks');
},
local(state) {
return state.webhooks && !Object.keys(state.webhooks).length ? state : {};
return !Object.keys(state.webhooks).length ? state : {};
},
success: WebhookActions.successFetchWebhooks,
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() {
return requests.get('webhooks_events');
},
local(state) {
return state.webhookEvents && !Object.keys(state.webhookEvents).length ? state : {};
return !Object.keys(state.webhookEvents).length ? state : {};
},
success: WebhookActions.successFetchWebhookEvents,
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: {
remote(state) {
return requests.delete('webhook', {'webhook_id': state.webhookMeta.idToDelete });
remote(state, webhookId) {
return requests.delete('webhook', { 'webhook_id': webhookId });
},
success: WebhookActions.successRemoveWebhook,
error: WebhookActions.errorWebhooks
}
};
export default WebhookSource;
export default WebhookSource;

View File

@ -9,17 +9,20 @@ import { getSubdomain } from '../utils/general_utils';
const WhitelabelSource = {
lookupWhitelabel: {
remote() {
return requests.get('whitelabel_settings', {'subdomain': getSubdomain()});
return requests.get('whitelabel_settings', { 'subdomain': getSubdomain() });
},
local(state) {
return Object.keys(state.whitelabel).length > 0 ? state : {};
return Object.keys(state.whitelabel).length ? state : {};
},
success: WhitelabelActions.successFetchWhitelabel,
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() {
this.edition = {};
this.editionMeta = {
err: null,
idToFetch: null
err: null
};
this.coaMeta = {
err: null
@ -38,26 +37,30 @@ class EditionStore {
};
}
onFetchEdition(idToFetch) {
this.editionMeta.idToFetch = idToFetch;
onFetchEdition(editionId) {
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 }) {
if (edition) {
this.edition = edition;
this.editionMeta.err = null;
this.editionMeta.idToFetch = null;
if (this.edition.coa && this.edition.acl.acl_coa &&
typeof this.edition.coa.constructor !== Object) {
this.getInstance().lookupCoa();
} else if (!this.edition.coa && this.edition.acl.acl_coa) {
this.getInstance().performCreateCoa();
// Also fetch coa if allowed
if (edition.acl.acl_coa) {
if (edition.coa && typeof edition.coa.constructor !== Object) {
this.getInstance().lookupCoa(edition.coa);
} else if (!edition.coa) {
this.getInstance().performCreateCoaForEdition(edition.bitcoin_id);
}
}
} else {
this.editionMeta.err = new Error('Problem fetching the edition');
console.logGlobal(this.editionMeta.err);
}
}
@ -67,18 +70,21 @@ class EditionStore {
this.coaMeta.err = null;
} else {
this.coaMeta.err = new Error('Problem generating/fetching the COA');
console.logGlobal(this.coaMeta.err);
}
}
onErrorEdition(err) {
console.logGlobal(err);
this.editionMeta.err = err;
}
onErrorCoa(err) {
// On 404s, create a new COA as the COA has not been made yet
if (err && err.json && err.json.status === 404) {
this.getInstance().performCreateCoa();
this.getInstance().performCreateCoaForEdition(this.edition.bitcoin_id);
} else {
console.logGlobal(err);
this.coaMeta.err = err;
}
}

View File

@ -1,6 +1,7 @@
'use strict';
import { altUser } from '../alt';
import UserActions from '../actions/user_actions';
import UserSource from '../sources/user_source';
@ -10,7 +11,6 @@ class UserStore {
constructor() {
this.currentUser = {};
this.userMeta = {
invalidateCache: false,
err: null
};
@ -19,24 +19,30 @@ class UserStore {
}
onFetchCurrentUser(invalidateCache) {
this.userMeta.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser();
if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupCurrentUser(invalidateCache);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessFetchCurrentUser({users: [user = {}]}) {
this.userMeta.invalidateCache = false;
onSuccessFetchCurrentUser({ users: [ user = {} ] }) {
this.userMeta.err = null;
this.currentUser = user;
}
onLogoutCurrentUser() {
this.getInstance().performLogoutCurrentUser();
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessLogoutCurrentUser() {
this.userMeta.err = null;
this.currentUser = {};
}

View File

@ -1,6 +1,7 @@
'use strict';
import { alt } from '../alt';
import WebhookActions from '../actions/webhook_actions';
import WebhookSource from '../sources/webhook_source';
@ -10,12 +11,9 @@ class WebhookStore {
this.webhooks = [];
this.webhookEvents = [];
this.webhookMeta = {
invalidateCache: false,
err: null,
idToDelete: null
err: null
};
this.webhookEventsMeta = {
invalidateCache: false,
err: null
};
@ -24,54 +22,57 @@ class WebhookStore {
}
onFetchWebhooks(invalidateCache) {
this.webhookMeta.invalidateCache = invalidateCache;
this.getInstance().lookupWebhooks();
if (invalidateCache || !this.getInstance().isLoading()) {
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 = [] }) {
this.webhookMeta.invalidateCache = false;
this.webhookMeta.err = null;
this.webhooks = webhooks;
this.webhookEventsMeta.invalidateCache = true;
this.getInstance().lookupWebhookEvents();
this.onFetchWebhookEvents(true);
}
onFetchWebhookEvents(invalidateCache) {
this.webhookEventsMeta.invalidateCache = invalidateCache;
this.getInstance().lookupWebhookEvents();
if (invalidateCache || !this.getInstance().isLoading()) {
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 }) {
this.webhookEventsMeta.invalidateCache = false;
this.webhookEventsMeta.err = null;
// remove all events that have already been used.
const usedEvents = this.webhooks
.reduce((tempUsedEvents, webhook) => {
tempUsedEvents.push(webhook.event.split('.')[0]);
return tempUsedEvents;
}, []);
.reduce((tempUsedEvents, webhook) => {
tempUsedEvents.push(webhook.event.split('.')[0]);
return tempUsedEvents;
}, []);
this.webhookEvents = events.filter((event) => {
return usedEvents.indexOf(event) === -1;
});
}
onRemoveWebhook(id) {
this.webhookMeta.invalidateCache = true;
this.webhookMeta.idToDelete = id;
onRemoveWebhook(webhookId) {
this.getInstance().performRemoveWebhook(webhookId);
if(!this.getInstance().isLoading()) {
this.getInstance().performRemoveWebhook();
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessRemoveWebhook() {
this.webhookMeta.idToDelete = null;
if(!this.getInstance().isLoading()) {
this.getInstance().lookupWebhooks();
}
this.getInstance().lookupWebhooks(true);
}
onErrorWebhooks(err) {

View File

@ -1,7 +1,9 @@
'use strict';
import { altWhitelabel } from '../alt';
import WhitelabelActions from '../actions/whitelabel_actions';
import WhitelabelSource from '../sources/whitelabel_source';
@ -9,7 +11,6 @@ class WhitelabelStore {
constructor() {
this.whitelabel = {};
this.whitelabelMeta = {
invalidateCache: false,
err: null
};
@ -18,15 +19,16 @@ class WhitelabelStore {
}
onFetchWhitelabel(invalidateCache) {
this.whitelabelMeta.invalidateCache = invalidateCache;
if(!this.getInstance().isLoading()) {
this.getInstance().lookupWhitelabel();
if (invalidateCache || !this.getInstance().isLoading()) {
this.getInstance().lookupWhitelabel(invalidateCache);
}
// Prevent alt from sending an empty change event when a request is sent
// off to the source
this.preventDefault();
}
onSuccessFetchWhitelabel({ whitelabel = {} }) {
this.whitelabelMeta.invalidateCache = false;
this.whitelabelMeta.err = null;
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);
}