Created
August 20, 2019 19:47
-
-
Save MaherBhasvar/8f70eb06222f734745f5d88d570d36f2 to your computer and use it in GitHub Desktop.
Pure redux, just for understanding purpose
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
console.clear() | |
//action creators | |
const newPolicy = (name, amount) => { | |
return { | |
type: "NEW_POLICY", | |
payload: {name: name, amount: amount} | |
} | |
} | |
const deletePolicy = (name) => { | |
return { | |
type: "DELETE_POLICY", | |
payload: {name: name} | |
} | |
} | |
const submitClaim = (name, amount) => { | |
return { | |
type: "SUBMIT_CLAIM", | |
payload: {name: name, amount: amount} | |
} | |
} | |
//reducers | |
const claimHistory = (history = [], action) => { | |
if (action.type == "SUBMIT_CLAIM") { | |
return [...history, action.payload] | |
} | |
return [...history] | |
} | |
const accounting = (cash = 100, action) => { | |
if (action.type == "SUBMIT_CLAIM") { | |
return cash - action.payload.amount | |
} | |
if (action.type == "NEW_POLICY") { | |
return cash + action.payload.amount | |
} | |
return cash | |
} | |
const policies = (policy = [], action) => { | |
if (action.type == "NEW_POLICY") { | |
return [...policy, action.payload.name] | |
} | |
if (action.type == "DELETE_POLICY") { | |
return policy.filter(name => name !== action.payload.name) | |
} | |
return policy | |
} | |
const combineReducers = Redux.combineReducers | |
const createStore = Redux.createStore | |
const departments = combineReducers({ | |
claimHistory: claimHistory, | |
accounting: accounting, | |
policies: policies, | |
}) | |
const store = createStore(departments) | |
//sending action | |
const action = newPolicy('Alex', 100) | |
store.dispatch(action) | |
console.log(store.getState()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment