Created
December 21, 2016 20:28
-
-
Save bryantee/e9720662ba42ed3269d47d0707277316 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
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 | |
}); |
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
$ 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) |
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
// require('babel-polyfill'); | |
import * as actions from './actions/index' | |
import repositoryReducer from './reducers/index' | |
repositoryReducer([], actions.addRepo('TEST REPO')); |
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
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