Last active
May 29, 2018 22:41
-
-
Save santospatrick/77e80e0321797c79376fd33de40faa05 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Redux action to store cultures | |
* @param {Array<Object>} arr | |
* @returns {Object} | |
*/ | |
export const reduxAction = arr => ({ | |
type: 'ADD_CULTURES', | |
cultures: arr | |
}); | |
/** | |
* Angular action that has nothing | |
* to do with redux | |
* @param {Array<Object>} arr | |
* @returns {void} | |
*/ | |
export const angularAction = arr => console.log(JSON.stringify(arr, null, 2)); | |
/** | |
* Function that lives in your converters | |
* logic file to handle array and objects | |
* transformations | |
* @param {Array<Object>} arr | |
*/ | |
export const hasQuantity = arr => arr.filter(item => item.quantity); | |
export const culturesThatHasQuantity = hasQuantity(cultures); | |
/** | |
* Function that lives in your business | |
* logic file/dir/project to handle conditions | |
* @param {Array<Object>} cultures | |
* @param {Function} action | |
* @returns {Function} | |
*/ | |
export const executeAction = (cultures, action) => { | |
return dispatch => { | |
// just an example of condition | |
if (dispatch && cultures.length > 1) { | |
// execute redux | |
return dispatch(action(cultures)); | |
} else if (!dispatch && cultures.length > 1) { | |
// execute angular | |
return action(cultures); | |
} else { | |
// will execute in both projects | |
throw new Error('There are not enough cultures!') | |
} | |
} | |
} | |
/** | |
* Use it in Angular. Note that we have a plus parenthesis at the end | |
* because "executeAction" returns a function and we need to execute it | |
* in this moment. If you dont have the plus parenthesis, the returned | |
* function wont be executed. | |
*/ | |
executeAction(culturesThatHasQuantity, angularAction)(); | |
/** | |
* Use it in Redux. Note that in the second parenthesis we are | |
* passing dispatch now that is the ONLY argument in the returned | |
* function. awesome! 🔥 | |
*/ | |
executeAction(culturesThatHasQuantity, reduxAction)(dispatch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment