-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP for moving header to React with shared store
Bugs and TODOs: * Tests are failing
- Loading branch information
Showing
26 changed files
with
253 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// All webpack assets in development will be loaded via webpack dev server | ||
|
||
// turbolinks comes from npm and is listed in webpack.client.base.config.js | ||
|
||
//= require rails_startup |
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
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
17 changes: 17 additions & 0 deletions
17
client/app/bundles/comments/components/Navigationbar/CommentsCount.jsx
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,17 @@ | ||
import React, {PropTypes} from 'react'; | ||
|
||
const href = 'https://github.com/shakacode/react_on_rails/blob/master/README.md#multiple-react-' + | ||
'components-on-a-page-with-one-store'; | ||
const CommentsCount = (props) => ( | ||
<li> | ||
<a id='js-comment-count' href={href}> | ||
Comments: {props.commentsCount} | ||
</a> | ||
</li> | ||
); | ||
|
||
CommentsCount.propTypes = { | ||
commentsCount: PropTypes.number.isRequired, | ||
}; | ||
|
||
export default CommentsCount; |
77 changes: 77 additions & 0 deletions
77
client/app/bundles/comments/components/Navigationbar/Navigationbar.jsx
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,77 @@ | ||
import React, {PropTypes} from 'react'; | ||
import ReactOnRails from 'react-on-rails'; | ||
import classNames from 'classnames'; | ||
import CommentsCount from './CommentsCount'; | ||
import * as paths from '../../constants/paths'; | ||
|
||
const NavigationBar = (props) => { | ||
const { commentsCount, pathname } = props; | ||
|
||
return ( | ||
<nav className="navbar navbar-default" role="navigation"> | ||
<div className="container"> | ||
<div className="navbar-header"> | ||
<button | ||
type="button" | ||
className="navbar-toggle" | ||
data-toggle="collapse" | ||
data-target="#bs-example-navbar-collapse-1" | ||
> | ||
<span className="sr-only">Toggle navigation</span> | ||
<span className="icon-bar"/> | ||
<span className="icon-bar"/> | ||
<span className="icon-bar"/> | ||
</button> | ||
<a className="navbar-brand" href="http://www.shakacode.com">ShakaCode</a> | ||
</div> | ||
<div className="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> | ||
<ul className="nav navbar-nav"> | ||
<li className={classNames({ active: (pathname === paths.ROUTER_PATH) })}> | ||
<a href={paths.ROUTER_PATH}>React Router Demo</a> | ||
</li> | ||
<li className={classNames({ active: (pathname === paths.NO_ROUTER_PATH) })}> | ||
<a href={paths.NO_ROUTER_PATH}>React Demo</a> | ||
</li> | ||
<li className={classNames({ active: (pathname === paths.SIMPLE_REACT_PATH) })}> | ||
<a href={paths.SIMPLE_REACT_PATH}>Simple React</a> | ||
</li> | ||
<li className={classNames({ active: (pathname === paths.RAILS_PATH) })}> | ||
<a href={paths.RAILS_PATH}>Classic Rails</a> | ||
</li> | ||
<li> | ||
<a href={ | ||
'https://github.com/' + | ||
'shakacode/react-webpack-rails-tutorial' | ||
}> | ||
Source on Github | ||
</a> | ||
</li> | ||
<li> | ||
<a href={ | ||
'http://www.railsonmaui.com/' + | ||
'blog/2014/10/03/integrating' + | ||
'-webpack-and-the-es6-transpiler' + | ||
'-into-an-existing-rails-project/' | ||
}>Tutorials</a> | ||
</li> | ||
<li> | ||
<a href={ | ||
'http://forum.shakacode.com/' + | ||
't/fast-rich-client-rails-development' + | ||
'-with-webpack-and-the-es6-transpiler/82/22' | ||
}>Forum</a> | ||
</li> | ||
{commentsCount && CommentsCount({ commentsCount })} | ||
</ul> | ||
</div> | ||
</div> | ||
</nav> | ||
); | ||
}; | ||
|
||
NavigationBar.propTypes = { | ||
commentsCount: PropTypes.number, | ||
pathname: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default NavigationBar; |
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,4 @@ | ||
export const ROUTER_PATH = '/'; | ||
export const NO_ROUTER_PATH = '/no-router'; | ||
export const SIMPLE_REACT_PATH = '/simple'; | ||
export const RAILS_PATH = '/comments'; |
37 changes: 37 additions & 0 deletions
37
client/app/bundles/comments/containers/NavigationBarContainer.jsx
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,37 @@ | ||
import React, { PropTypes } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { bindActionCreators } from 'redux'; | ||
|
||
import NavigationBar from '../components/NavigationBar/NavigationBar'; | ||
import * as commentsActionCreators from '../actions/commentsActionCreators'; | ||
import BaseComponent from 'libs/components/BaseComponent'; | ||
|
||
function stateToProps(state) { | ||
// Which part of the Redux global state does our component want to receive as props? | ||
if (state.$$commentsStore) { | ||
return { | ||
commentsCount: state.$$commentsStore.get('$$comments').size, | ||
pathname: state.railsContext.pathname, | ||
}; | ||
} else { | ||
return { }; | ||
} | ||
} | ||
|
||
class NavigationBarContainer extends BaseComponent { | ||
static propTypes = { | ||
commentsCount: PropTypes.number.isRequired, | ||
pathname: PropTypes.string.isRequired, | ||
}; | ||
|
||
render() { | ||
const { commentsCount, pathname } = this.props; | ||
|
||
return ( | ||
<NavigationBar {...{ commentsCount, pathname }} /> | ||
); | ||
} | ||
} | ||
|
||
// Don't forget to actually use connect! | ||
export default connect(stateToProps)(NavigationBarContainer); |
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,9 +1,12 @@ | ||
import commentsReducer, { $$initialState as $$commentsState } from './commentsReducer'; | ||
import railsContextReducer, { initialState as railsContextState } from './railsContextReducer'; | ||
|
||
export default { | ||
$$commentsStore: commentsReducer, | ||
railsContext: railsContextReducer, | ||
}; | ||
|
||
export const initialStates = { | ||
$$commentsState, | ||
railsContextState, | ||
}; |
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,5 @@ | ||
export const initialState = {}; | ||
|
||
export default function railsContextReducer(state = initialState, action = null) { | ||
return state; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Top level component for client side. | ||
// Compare this to the ./ServerApp.jsx file which is used for server side rendering. | ||
|
||
import React from 'react'; | ||
import ReactOnRails from 'react-on-rails'; | ||
import NavigationBar from '../components/NavigationBar/NavigationBar'; | ||
import NavigationBarContainer from '../containers/NavigationBarContainer'; | ||
import { Provider } from 'react-redux'; | ||
import * as paths from '../constants/paths'; | ||
|
||
/* | ||
* Export a function that returns a ReactComponent, depending on a store named SharedReduxStore. | ||
* This is used for the client rendering hook after the page html is rendered. | ||
* React will see that the state is the same and not do anything. | ||
*/ | ||
export default (_props, railsContext) => { | ||
// This is where we get the existing store. | ||
const stores = ReactOnRails.stores(); | ||
const { pathname } = railsContext; | ||
let store; | ||
if (pathname === paths.ROUTER_PATH) { | ||
store = ReactOnRails.getStore('routerCommentsStore', false); | ||
} else if (pathname === paths.NO_ROUTER_PATH) { | ||
store = ReactOnRails.getStore('commentsStore', false); | ||
} else { | ||
return ( | ||
<NavigationBar {...{ pathname }} /> | ||
); | ||
} | ||
|
||
return ( | ||
<Provider store={store}> | ||
<NavigationBarContainer /> | ||
</Provider> | ||
); | ||
}; |
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
Oops, something went wrong.