Created
July 15, 2015 15:15
-
-
Save DanielHeckrath/32aeeaf111c9de3815cb to your computer and use it in GitHub Desktop.
React app with redux store. Uses custom provider/middleware to intercept actions that aren't meant for the reducers.
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
class App extends React.Component { | |
render() { | |
return ( | |
<ChatProvider store={redux}> | |
{::this.renderChildren} | |
</ChatProvider> | |
); | |
} | |
renderChildren() { | |
return ( | |
<Router {...this.props}> | |
{routes} | |
</Router> | |
); | |
} | |
} |
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
'use strict'; | |
import { Component, PropTypes } from 'react'; | |
import createChatService from './services/chat-service'; | |
import { SEND_DIRECT_MESSAGE, SEND_CHAT_MESSAGE } from './constants/action-types'; | |
const storeShape = PropTypes.shape({ | |
subscribe: PropTypes.func.isRequired, | |
dispatch: PropTypes.func.isRequired, | |
getState: PropTypes.func.isRequired | |
}); | |
function chatMiddleware(chat) { | |
return next => action => { | |
if (action.type === SEND_DIRECT_MESSAGE) { | |
const { users, message } = action.payload; | |
chat.sendDirectMessage(users, message); | |
} else if (action.type === SEND_CHAT_MESSAGE) { | |
const { channel, message } = action.payload; | |
chat.sendChatMessage(channel, message); | |
} else { | |
return next(action); | |
} | |
}; | |
} | |
export default class ChatProvider extends Component { | |
static childContextTypes = { | |
store: storeShape.isRequired | |
}; | |
static propTypes = { | |
children: PropTypes.func.isRequired | |
}; | |
constructor(props, context) { | |
super(props, context); | |
this.chat = createChatService(props.store); | |
const dispatch = chatMiddleware(this.chat)(props.store.dispatch); | |
const store = { ...props.store, dispatch }; | |
this.state = { store }; | |
} | |
getChildContext() { | |
return { store: this.state.store }; | |
} | |
render() { | |
const { children } = this.props; | |
return children(); | |
} | |
} |
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
class ChatService { | |
constructor(redux) { | |
this.redux = redux; | |
this.unsubscribe = redux.subscribe(::this.handleChange); | |
this.dispatch = redux.dispatch; | |
this.getState = redux.getState; | |
// initial state check | |
this.handleChange(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment