Created
January 14, 2017 20:45
-
-
Save shivabhusal/863ee65dd6a68ed07f4aa216b850024c to your computer and use it in GitHub Desktop.
This looks like mess. Its so difficult to maintain any software with this kind of scattered components.
It is always a better idea to sperate out logical things into well defined directory structure. We will do this in a moment. Checkout the next Pull request
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 {combineReducers, createStore} from 'redux'; | |
const usersReducer = (usersState = [], action) => { | |
const new_state = Object.assign([], usersState); | |
switch (action.type) { | |
case 'UPDATE_USER': { | |
var updateable_record = new_state.find((item) => item.id === action.payload.id); | |
new_state[new_state.indexOf(updateable_record)] = Object.assign({}, updateable_record, action.payload); | |
break; | |
} | |
case 'DELETE_USER': { | |
var updateable_record = new_state.find((item) => item.id === action.payload.id); | |
new_state.splice(new_state.indexOf(updateable_record), 1); | |
// Remember sometime one action can have consequences in multiple Models | |
// For eg: when user is deleted, it should also delete its belongings like articles. | |
// Since, Redux routes the actions_types to every reducers in the chain, so you need to catch this | |
// action_type in articlesReducer as well and execute. | |
break; | |
} | |
} | |
return new_state; | |
}; | |
const articlesReducer = (articlesState = [], action) => { | |
const new_state = Object.assign([], articlesState); | |
switch (action.type) { | |
case 'UPDATE_ARTICLE': { | |
var updateable_record = new_state.find((item) => item.id === action.payload.id); | |
new_state[new_state.indexOf(updateable_record)] = Object.assign({}, updateable_record, action.payload); | |
break; | |
} | |
case 'DELETE_ARTICLE': { | |
var updateable_record = new_state.find((item) => item.id === action.payload.id); | |
new_state.splice(new_state.indexOf(updateable_record), 1); | |
break; | |
} | |
case 'VIEW_ARTICLE': { | |
var updateable_record = new_state.find((item) => item.id === action.payload.id); | |
new_state[new_state.indexOf(updateable_record)] = Object.assign({}, updateable_record, action.payload); | |
updateable_record.viewCount = updateable_record.viewCount + 1; | |
break; | |
} | |
} | |
return new_state; | |
}; | |
const reducers = combineReducers({ | |
users: usersReducer, | |
articles: articlesReducer | |
}); | |
const store = createStore(reducers, { | |
users: [ | |
{id: 1, first_name: 'John', last_name: 'Kelly', age: 34, email: 'john@sedd.com'}, | |
{id: 2, first_name: 'Peter', last_name: 'Kelly', age: 34, email: 'john@sedd.com'}, | |
{id: 3, first_name: 'Ramesh', last_name: 'Chapagain', age: 34, email: 'chaps@sedd.com'}, | |
{id: 4, first_name: 'Dinesh', last_name: 'Banjade', age: 34, email: 'banz@sedd.com'}, | |
], | |
articles: [ | |
{id: 1, author_id: 1, title: 'Beautiful country Nepal', body: "Lorem Ipsom", viewCount: 0}, | |
{id: 2, author_id: 1, title: 'Beautiful country Nepal', body: "Lorem Ipsom", viewCount: 1}, | |
{id: 3, author_id: 3, title: 'When I visited Dharan', body: "Lorem Ipsom", viewCount: 0}, | |
{id: 4, author_id: 4, title: 'My trip to Everest Basecamp', body: "Lorem Ipsom", viewCount: 0} | |
], | |
}); | |
store.subscribe(() => { | |
// I am listening | |
console.log(store.getState()); | |
}); | |
// Logging part | |
store.dispatch({type: 'UPDATE_USER', payload: {id: 1, first_name: 'Dipesh Kishor', last_name: 'Bhattarai'}}); | |
console.log('------------------ Updated USER with id: 1 ---------------------------'); | |
store.dispatch({type: 'DELETE_USER', payload: {id: 3}}); | |
console.log('----------------------------- Deleted User with id: 3 --------------------------------------'); | |
store.dispatch({type: 'UPDATE_ARTICLE', payload: {id: 1, title: 'Blizzard in desert'}}); | |
console.log('------------------ Updated Article ID: 1 ---------------------------'); | |
store.dispatch({type: 'DELETE_ARTICLE', payload: {id: 4}}); | |
console.log('------------------ Deleted Article with id: 4 ---------------------------'); | |
store.dispatch({type: 'VIEW_ARTICLE', payload: {id: 3}}); | |
console.log('------------------ View update for ID: 3 ---------------------------'); | |
// Output in Browser Console | |
// Object {users: Array[4], articles: Array[4]} | |
// ------------------ Updated USER with id: 1 --------------------------- | |
// Object {users: Array[3], articles: Array[4]} | |
// ----------------------------- Deleted User with id: 3 -------------------------------------- | |
// Object {users: Array[3], articles: Array[4]} | |
// ------------------ Updated Article ID: 1 --------------------------- | |
// {users: Array[3], articles: Array[3]} | |
// ------------------ Deleted Article with id: 4 --------------------------- | |
// Object {users: Array[3], articles: Array[3]} | |
// ------------------ View update for ID: 3 --------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment