Skip to content

Instantly share code, notes, and snippets.

@bryantee
Created December 21, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryantee/e9720662ba42ed3269d47d0707277316 to your computer and use it in GitHub Desktop.
Save bryantee/e9720662ba42ed3269d47d0707277316 to your computer and use it in GitHub Desktop.
export const ADD_REPO = 'ADD_REPO';
export const addRepo = repository => ({
type: ADD_REPO,
repository
});
export const RATE_REPO = 'RATE_REPO';
export const rateRepo = (repository, rating) => ({
type: RATE_REPO,
repository,
rating
});
$ node js/index.js
/Users/bryanswagerty/code/react-repo-tracker/build/js/index.js:62
(0, _index3.default)([], actions.addRepo('TEST REPO'));
^
TypeError: (0 , _index3.default) is not a function
at Object.defineProperty.value (/Users/bryanswagerty/code/react-repo-tracker/build/js/index.js:62:22)
at __webpack_require__ (/Users/bryanswagerty/code/react-repo-tracker/build/js/index.js:20:30)
at /Users/bryanswagerty/code/react-repo-tracker/build/js/index.js:40:18
at Object.<anonymous> (/Users/bryanswagerty/code/react-repo-tracker/build/js/index.js:43:10)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
at Module.runMain (module.js:607:10)
// require('babel-polyfill');
import * as actions from './actions/index'
import repositoryReducer from './reducers/index'
repositoryReducer([], actions.addRepo('TEST REPO'));
import * as actions from '../actions/index';
// set initial state if nothing existings
const initialRepositoryState = [];
export const repositoryReducer = (state=initialRepositoryState, action) => {
// check action type to proceed, create if action is ADD_REPO
if (action.type === ADD_REPO) {
return [...state, {
name: action.repository,
rating: null
}];
}
// if action is RATE_REPO, then find repository matching and return new state
else if (action.type === RATE_REPO) {
const index = state.findIndex(repository => {
repository.name === action.repository;
});
// if can't find repository in state
if (index === -1) {
throw new Error('Could not find repository');
}
// build the new state array to return
const before = state.slice(0, index);
const after = state.slice(index + 1);
const newRepository = Object.assign({}, state[index], {rating: action.rating});
return [...before, newRepository, ...after];
}
return state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment