-
Notifications
You must be signed in to change notification settings - Fork 3
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 #24 from dailydrip/redux-loop
Add redux-loop
- Loading branch information
Showing
10 changed files
with
152 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,56 @@ | ||
export const FETCH_TOPICS = 'FETCH_TOPICS' | ||
export const FETCH_DRIPS = 'FETCH_DRIPS' | ||
export const SET_TOPICS = 'SET_TOPICS' | ||
export const SET_DRIPS = 'SET_DRIPS' | ||
export const SELECT_TOPIC = 'SELECT_TOPIC' | ||
export const NO_OP = 'NO_OP' | ||
|
||
const setTopics = (topics) => { | ||
return { | ||
type: FETCH_TOPICS, | ||
type: SET_TOPICS, | ||
topics, | ||
} | ||
} | ||
|
||
const setDrips = (topicId, drips) => { | ||
return { | ||
type: FETCH_DRIPS, | ||
type: SET_DRIPS, | ||
topicId, | ||
drips, | ||
} | ||
} | ||
|
||
const fetchTopics = () => { | ||
return { | ||
type: FETCH_TOPICS | ||
} | ||
} | ||
|
||
const fetchDrips = (topicId) => { | ||
return { | ||
type: FETCH_DRIPS, | ||
topicId | ||
} | ||
} | ||
|
||
|
||
const selectTopic = (topicId) => { | ||
return { | ||
type: SELECT_TOPIC, | ||
topicId, | ||
} | ||
} | ||
|
||
const Actions = { | ||
const noOp = () => { | ||
return { | ||
type: NO_OP | ||
} | ||
} | ||
|
||
export const Actions = { | ||
setTopics, | ||
setDrips, | ||
selectTopic, | ||
fetchTopics, | ||
fetchDrips | ||
} | ||
|
||
export default Actions |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,16 @@ | ||
import { connect } from 'react-redux' | ||
import AppView from './AppView' | ||
import Immutable from 'immutable' | ||
import Actions from '../actions' | ||
import { Actions } from '../actions' | ||
import API from '../api' | ||
|
||
export default connect( | ||
() => { return {} }, | ||
dispatch => { | ||
return { | ||
fetchTopics: () => { | ||
API.getTopics().then((response) => { | ||
const topicsMap = response.data.topics.reduce((acc, topic) => { | ||
return acc.set(topic.id, Immutable.fromJS(topic)) | ||
}, Immutable.Map()) | ||
dispatch(Actions.setTopics(topicsMap)) | ||
}).catch((error) => { | ||
console.log(error) | ||
}) | ||
}, | ||
dispatch(Actions.fetchTopics()) | ||
} | ||
} | ||
} | ||
)(AppView) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,87 @@ | ||
import { createReducer } from 'redux-immutablejs' | ||
import Immutable from 'immutable' | ||
import { FETCH_TOPICS, FETCH_DRIPS } from '../../actions' | ||
|
||
export default createReducer(Immutable.Map(), { | ||
[FETCH_TOPICS]: (state, action) => { | ||
return action.topics.map((topic) => { | ||
// Look in the store and pull in any drips we already know about, since | ||
// we don't have them here but we'd like to keep them for faster startup | ||
// from storage | ||
const existingTopic = state.get(topic.get('id').toString()) | ||
let newTopic | ||
if (existingTopic) { | ||
newTopic = topic.set('drips', existingTopic.get('drips')) | ||
} else { | ||
newTopic = topic | ||
} | ||
return newTopic | ||
import { Actions, SET_TOPICS, SET_DRIPS, FETCH_TOPICS, FETCH_DRIPS } from '../../actions' | ||
import { loop, Effects } from 'redux-loop'; | ||
import API from '../../api' | ||
|
||
|
||
let fetchTopics = () => ( | ||
API.getTopics() | ||
.then((response) => { | ||
return Actions.setTopics( | ||
response.data.topics | ||
.reduce((acc, topic) => { | ||
return acc | ||
.set(topic.id, Immutable.fromJS(topic)) | ||
}, Immutable.Map()) | ||
) | ||
}) | ||
.catch((err) => { | ||
console.error(err) | ||
return Actions.noOp() | ||
}) | ||
}, | ||
[FETCH_DRIPS]: (state, { topicId, drips }) => { | ||
return state.setIn([topicId, 'drips'], drips) | ||
}, | ||
}) | ||
) | ||
|
||
let fetchDrips = (topicId) => { | ||
return API.getDrips(topicId) | ||
.then((response) => { | ||
return Actions.setDrips( | ||
topicId, | ||
response.data.drips | ||
.reduce((acc, drip) => ( | ||
acc.set(drip.id, Immutable.fromJS(drip)) | ||
), Immutable.Map()) | ||
)}) | ||
.catch((err) => { | ||
console.error(err) | ||
return Actions.noOp() | ||
}) | ||
} | ||
|
||
export default function(state, action){ | ||
switch (action.type) { | ||
case FETCH_TOPICS: | ||
return loop( | ||
state, | ||
Effects.promise(fetchTopics) | ||
) | ||
|
||
case SET_TOPICS: | ||
const topics = action.topics.map((topic) => { | ||
// Look in the store and pull in any drips we already know about, since | ||
// we don't have them here but we'd like to keep them for faster startup | ||
// from storage | ||
const existingTopic = state.get(topic.get('id').toString()) | ||
let newTopic | ||
if (existingTopic) { | ||
newTopic = topic.set('drips', existingTopic.get('drips')) | ||
} else { | ||
newTopic = topic | ||
} | ||
return newTopic | ||
}) | ||
|
||
return loop( | ||
topics, | ||
Effects.none() | ||
) | ||
|
||
case FETCH_DRIPS: | ||
return loop( | ||
state, | ||
Effects.promise(fetchDrips, action.topicId) | ||
) | ||
|
||
case SET_DRIPS: | ||
return loop( | ||
state | ||
.setIn([action.topicId, 'drips'], action.drips), | ||
Effects.none() | ||
) | ||
|
||
default: | ||
return loop( | ||
state, | ||
Effects.none() | ||
) | ||
} | ||
} |