Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from droberts84/feature/redux
Redux Example
- Loading branch information
Showing
11 changed files
with
105 additions
and
101 deletions.
There are no files selected for viewing
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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import { App } from 'components/App'; | ||
import { flux } from 'application/flux' | ||
import store from 'myRedux/store'; | ||
|
||
render(<App flux={flux} />, document.getElementById('root')); | ||
render(<App store={store} />, document.getElementById('root')); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { API } from 'application/api'; | ||
|
||
// action creators | ||
export function addText(textInfo) { | ||
// example of error checking | ||
if ( textInfo.from ) { | ||
return Object.assign({}, textInfo, { type: 'DRAFT_TEXT'} ) | ||
} else { | ||
return {} | ||
} | ||
} | ||
|
||
export function fetchCalls() { | ||
// not making async for simplicity | ||
return { | ||
type: 'FETCH_CALLS', | ||
calls: API.getCalls() | ||
} | ||
} | ||
|
||
export function fetchMessages() { | ||
// not making async for simplicity | ||
return { | ||
type: 'FETCH_MESSAGES', | ||
messages: API.getMessages() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { combineReducers } from 'redux'; | ||
|
||
// sub reducer | ||
function messages(state = [], action) { | ||
switch(action.type) { | ||
case 'DRAFT_TEXT': | ||
return state.concat([{ | ||
id: state.length + 1, | ||
from: action.from, | ||
number: action.number, | ||
status: "draft" | ||
}]); | ||
case 'FETCH_MESSAGES': | ||
return action.messages | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
// sub reducer | ||
function calls(state = [], action) { | ||
switch(action.type) { | ||
case 'FETCH_CALLS': | ||
return action.calls | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
// the main reducer | ||
// it will return a new state based on the state and action | ||
// it does not mutate | ||
// this is a shorcut for | ||
// function communcationsApp(state = {}, action) { | ||
// return { | ||
// messages: messages(state.messages, action), | ||
// calls: calls(state.calls, action) | ||
// } | ||
// } | ||
const communcationsApp = combineReducers({ | ||
messages, | ||
calls | ||
}); | ||
|
||
export default communcationsApp; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createStore } from 'redux'; | ||
import communcationsApp from 'myRedux/reducers' | ||
|
||
let store = createStore(communcationsApp); | ||
|
||
store.subscribe(() => | ||
console.log("Current state is : ", store.getState()) | ||
); | ||
|
||
export default store |
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