Skip to content

Instantly share code, notes, and snippets.

@cbfranca
Last active November 21, 2018 16:30
Show Gist options
  • Save cbfranca/53593582489b3e43a794f5c88ff50a8c to your computer and use it in GitHub Desktop.
Save cbfranca/53593582489b3e43a794f5c88ff50a8c to your computer and use it in GitHub Desktop.
/* 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;
}
};
/* 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);
};
/* 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;
};
}
/* 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;
}
};
@jairrc
Copy link

jairrc commented Nov 21, 2018

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment