Skip to content

Commit

Permalink
feat(actions): create 'makeActionCreator' function
Browse files Browse the repository at this point in the history
generate action creators to reduce boilerplate
  • Loading branch information
aneurysmjs committed Jan 7, 2018
1 parent f5a47b5 commit 70d89d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
30 changes: 6 additions & 24 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,30 @@
*/

import * as types from '../constants/ActionTypes';
import makeActionCreator from './makeActionCreator';
import api from 'api';

/**
*
* @param {String} searchTerm
* @return {Object.<Action>} action
*/
export function setSearchTerm(searchTerm) {
return {
type: types.SET_SEARCH_TERM,
searchTerm
};
}
export const setSearchTerm = makeActionCreator(types.SET_SEARCH_TERM, 'searchTerm');


/**
*
* @param {String} selectedCountry
* @return {Object.<Action>} action
*/
export function setSelectedCountry(selectedCountry) {
return {
type: types.SET_SELECTED_COUNTRY,
selectedCountry
};
}
export const setSelectedCountry = makeActionCreator(types.SET_SELECTED_COUNTRY, 'selectedCountry');

/**
*
* @param {Array.<Object>} movies
* @return {Object.<Action>}
*/
export function setMovies(movies) {
return {
type: types.SET_MOVIES,
movies
};
}
export const setMovies = makeActionCreator(types.SET_MOVIES, 'movies');

/**
*
Expand Down Expand Up @@ -69,12 +56,7 @@ export function getMovies(url) {
* @param {Array.<Object>} countries
* @return {Object.<Action>}
*/
export function setCountries(countries) {
return {
type: types.SET_COUNTRIES,
countries
};
}
export const setCountries = makeActionCreator(types.SET_COUNTRIES, 'countries');

/**
*
Expand Down
24 changes: 24 additions & 0 deletions src/actions/makeActionCreator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @module actions/makeActionCreator
*/

/**
* Makes an action creator function to reduce boilerplate
*
* @param {String} type
* @param argNames
* @return {Function} the action creator itself
*/
export default function makeActionCreator(type, ...argNames) {

return function (...args) {
let action = { type };
argNames.forEach((arg, index) => {
action[argNames[index]] = args[index];
});

return action;

};

}

0 comments on commit 70d89d5

Please sign in to comment.