Last active
November 7, 2017 09:13
-
-
Save jaimeagudo/3fa32dfd05a581860385300cae3697fc to your computer and use it in GitHub Desktop.
Redux action creators with "backbone.js syntax"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const restActionTypes = ['REQUEST', 'ADD_REQUEST', 'SUCCESS', 'REFRESH', 'FAILURE', 'SELECT', 'RESET', 'SAVE', 'PLATFORM_REQUEST', 'PLATFORM_SUCCESS', 'PLATFORM_FAILURE', 'PAYMENT_RESULT'] | |
const authActionTypes = ['START', 'SUCCESS', 'FAILURE', 'EXPIRED', 'END', 'CACHE_REQUEST', 'CACHE_SUCCESS', 'CACHE_FAILURE', 'DECODED'] | |
const fcmActionTypes = ['TOKEN_REFRESHED', 'TOKEN_SAVE', 'TOKEN_SAVED', 'NOTIFICATION_RECEIVED', 'TOPICS_RECEIVED', 'OPTIONAL_TOPICS_PREFS_LOAD', /* 'OPTIONAL_TOPICS_PREFS_SAVE', unused for now */ 'OPTIONAL_TOPICS_PREFS_UPDATED', 'BACKGROUND_DATA_RECEIVED'] | |
const errorActionTypes = ['HTTP', 'AUTH', 'INTERNAL'] | |
// Helper to build objects like the one below and prevent name clashes and typos | |
// { FAILURE:"USER_FAILURE" | |
// REQUEST:"USER_REQUEST" | |
// SELECT:"USER_SELECT" | |
// SUCCESS:"USER_SUCCESS"} | |
function createActionTypes (actionTypes, base) { | |
return actionTypes.reduce((acc, type) => { | |
acc[type] = `${base}_${type}` | |
return acc | |
}, {}) | |
} | |
export const USER_AT = createActionTypes(restActionTypes, 'USER') | |
export const STATION_AT = createActionTypes(restActionTypes, 'STATIONS') | |
export const AUTH_AT = createActionTypes(authActionTypes, 'AUTH') | |
export const ERROR_AT = createActionTypes(fcmActionTypes, 'ERROR') | |
function action (type, payload = {}) { | |
return {type, ...payload} | |
} | |
export const analyticActions = { | |
user: (user) => action(ANALYTICS_AT.USER, {user}) | |
} | |
export const errorActions = { | |
internal: (error) => action(ERROR_AT.INTERNAL, { error }), | |
http: (error) => action(ERROR_AT.HTTP, { error }), | |
auth: (error) => action(ERROR_AT.AUTH, { error }) | |
} | |
export const userActions = { | |
load: () => action(USER_AT.REQUEST), | |
success: (response) => action(USER_AT.SUCCESS, response), | |
failure: (error) => action(USER_AT.FAILURE, error) | |
} | |
// reducer-map | |
export const stationsActions = { | |
load: () => action(STATION_AT.REQUEST), | |
success: (response) => action(STATION_AT.SUCCESS, {response}), | |
refresh: (collection) => action(STATION_AT.REFRESH, {collection}), | |
failure: (error) => action(STATION_AT.FAILURE, {error}), | |
select: (model) => action(STATION_AT.SELECT, {model}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment