Last active
November 21, 2018 16:30
-
-
Save cbfranca/53593582489b3e43a794f5c88ff50a8c to your computer and use it in GitHub Desktop.
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
/* core-module/modulescommon/getById.js */ | |
const INITIAL_STATE = { | |
data: {}, | |
loading: false, | |
error: false, | |
}; | |
const REQUEST = 'BY_ID_REQUEST'; | |
const SUCCESS = 'BY_ID_SUCCESS'; | |
const FAILURE = 'BY_ID_FAILURE'; | |
export const request = (prefix, params) => ({ | |
type: `${prefix}_${REQUEST}`, | |
name: prefix, | |
params, | |
}); | |
export const success = prefix => ({ | |
type: `${prefix}_${SUCCESS}`, | |
}); | |
export const failure = prefix => ({ | |
type: `${prefix}_${FAILURE}`, | |
}); | |
export const simpleRequest = (state = INITIAL_STATE, action) => { | |
switch (action.type) { | |
case `${action.name}_${REQUEST}`: | |
return { ...state, loading: true }; | |
case `${action.name}_${SUCCESS}`: | |
return { | |
...state, | |
data: action.data, | |
loading: false, | |
error: false, | |
}; | |
case `${action.name}_${FAILURE}`: | |
return { ...state, loading: false, error: true }; | |
default: | |
return state; | |
} | |
}; |
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
/* core-module/index.js */ | |
import { combineReducers } from 'redux'; | |
import createNamedWrapperReducer from 'core-module/utils/reduxUtils'; | |
import configureStore from 'core-module/config/configureStore'; | |
import rootSaga from 'store/sagas'; | |
import { simpleRequest } from 'core-module/module/common/simpleRequest'; | |
import { getById } from 'core-module/module/common/getById'; | |
export default () => { | |
const appReducer = combineReducers({ | |
createStock: createNamedWrapperReducer(simpleRequest, 'CREATE_STOCK'), | |
getStockById: createNamedWrapperReducer(getById, 'GET_STOCK' | |
}); | |
const rootReducer = (state, action) => appReducer(state, action); | |
return configureStore(rootReducer, rootSaga); | |
}; |
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
/* core-moduke/reduxUtils.js */ | |
export function createNamedWrapperReducer(reducerFunction, reducerName) { | |
return (state, action) => { | |
const { name } = action; | |
if (state === undefined || name === reducerName) { | |
return reducerFunction(state, action); | |
} | |
return state; | |
}; | |
} |
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
/* core-module/modulescommon/simpleRequest.js */ | |
const INITIAL_STATE = { | |
loading: false, | |
error: false, | |
}; | |
const REQUEST = 'REQUEST'; | |
const SUCCESS = 'SUCCESS'; | |
const FAILURE = 'FAILURE'; | |
export const request = (prefix, params) => ({ | |
type: `${prefix}_${REQUEST}`, | |
name: prefix, | |
params, | |
}); | |
export const success = prefix => ({ | |
type: `${prefix}_${SUCCESS}`, | |
}); | |
export const failure = prefix => ({ | |
type: `${prefix}_${FAILURE}`, | |
}); | |
export const simpleRequest = (state = INITIAL_STATE, action) => { | |
switch (action.type) { | |
case `${action.name}_${REQUEST}`: | |
return { ...state, loading: true }; | |
case `${action.name}_${SUCCESS}`: | |
return { ...state, loading: false, error: false }; | |
case `${action.name}_${FAILURE}`: | |
return { ...state, loading: false, error: true }; | |
default: | |
return state; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Olhei seus fontes mas não consegui assimilar direito o funcionamento, dentro da sua ideia. Mas gosto muito dessa iniciativa de enxugar e reaproveitar código. Abraços!