Skip to content

Commit

Permalink
[#1] - Refactored Header and ToolBar container.
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Aug 11, 2016
1 parent 7370994 commit a08d767
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 50 deletions.
21 changes: 6 additions & 15 deletions src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'

import { toggleContent } from '../../redux/actions'

import './Header.less'
import Navigation from './Navigation'
import ToolBar from './ToolBar'
import MusicPlayer from './MusicPlayer'
import { ToolBarContainer } from '../../containers'

const menuList = [
{ name: 'Hello', link: '/hello' },
Expand All @@ -16,30 +13,24 @@ const menuList = [
{ name: 'Zhihu', link: '/pages/zhihu' }
]

const Header = ({ musicList, pathname, dispatch }) => (
const Header = ({ musicList, pathname, toggleContent }) => (
<header id="header">
<div className="logo">
<span onClick={() => dispatch(toggleContent())} title="立青作品">
<span onClick={() => toggleContent()} title="立青作品">
<img alt="avatar" src="//o7mw3gkkh.qnssl.com/images/2016/1465649945502.png"/>
</span>
</div>
<Navigation menuList={menuList} selectedUrl={pathname}/>
<MusicPlayer songs={musicList}/>
<ToolBar />
<ToolBarContainer />
</header>
)

Header.propTypes = {
musicList: PropTypes.array.isRequired,
pathname: PropTypes.string.isRequired,
dispatch: PropTypes.func.isRequired
toggleContent: PropTypes.func.isRequired
}
Header.defaultProps = {}

function mapStateToProps(state) {
return {
pathname: state.routing.locationBeforeTransitions.pathname
}
}

export default connect(mapStateToProps)(Header)
export default Header
11 changes: 2 additions & 9 deletions src/components/Header/ToolBar.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { withRouter, browserHistory } from 'react-router'
import { browserHistory } from 'react-router'

class ToolBar extends Component {
constructor(props) {
Expand Down Expand Up @@ -42,10 +41,4 @@ ToolBar.propTypes = {
}
ToolBar.defaultProps = {}

function mapStateToProps(state) {
return {
posts: state.articleSummary.paginator
}
}

export default connect(mapStateToProps)(withRouter(ToolBar))
export default ToolBar
2 changes: 2 additions & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export ToolBar from './Header/ToolBar'
export Header from './Header/Header'
25 changes: 9 additions & 16 deletions src/containers/AppContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import './AppContainer.less'
import Header from '../components/Header/Header'
import { HeaderContainer } from '../containers'
import * as actionCreators from '../redux/actions'

class AppContainer extends Component {
Expand All @@ -12,30 +12,23 @@ class AppContainer extends Component {
}

render() {
console.info('AppContainer', this.props)
return (
<div className="main-app">
<Header musicList={this.props.musicList}/>
<HeaderContainer />
{this.props.children}
</div>
)
}
}

const { func, object } = PropTypes
AppContainer.propTypes = {
musicList: PropTypes.array.isRequired,
fetchMusicList: PropTypes.func.isRequired,
children: PropTypes.object.isRequired
fetchMusicList: func.isRequired,
children: object.isRequired
}
AppContainer.defaultProps = {}

function mapStateToProps(state) {
return {
musicList: state.musicList
}
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(actionCreators, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(AppContainer)
export default connect(
(dispatch) => bindActionCreators(actionCreators, dispatch)
)(AppContainer)
8 changes: 4 additions & 4 deletions src/containers/BlogContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fetchArticleSummary } from '../redux/actions'
import SideBar from '../components/Blog/SideBar'
import './BlogContainer.less'

class BlogPage extends Component {
class BlogContainer extends Component {
componentDidMount() {
this.props.dispatch(fetchArticleSummary())
}
Expand Down Expand Up @@ -36,13 +36,13 @@ class BlogPage extends Component {
}
}

BlogPage.propTypes = {
BlogContainer.propTypes = {
categories: PropTypes.array.isRequired,
showContent: PropTypes.bool.isRequired,
dispatch: PropTypes.func.isRequired,
children: PropTypes.object.isRequired
}
BlogPage.defaultProps = {}
BlogContainer.defaultProps = {}

function mapStateToProps(state) {
return {
Expand All @@ -51,4 +51,4 @@ function mapStateToProps(state) {
}
}

export default connect(mapStateToProps)(BlogPage)
export default connect(mapStateToProps)(BlogContainer)
21 changes: 21 additions & 0 deletions src/containers/HeaderContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import { Header } from '../components'
import * as actionCreators from '../redux/actions'

function mapStateToProps(state) {
return {
...state,
pathname: state.routing.locationBeforeTransitions.pathname
}
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(actionCreators, dispatch)
}

export default connect(
mapStateToProps,
mapDispatchToProps
)(Header)
36 changes: 36 additions & 0 deletions src/containers/ToolBarContainer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component, PropTypes } from 'react'
import { connect } from 'react-redux'
import { withRouter } from 'react-router'
import { ToolBar } from '../components'

class ToolBarContainer extends Component {
constructor(props) {
super(props)
console.info('ToolBarContainer', props)
}

render() {
return (
<ToolBar
posts={this.props.posts}
router={this.props.router}
/>
)
}
}

ToolBarContainer.propTypes = {
posts: PropTypes.array.isRequired,
router: React.PropTypes.shape({
push: React.PropTypes.func.isRequired
}).isRequired
}
ToolBarContainer.defaultProps = {}

function mapStateToProps(state) {
return { posts: state.articleSummary.paginator }
}

export default connect(
mapStateToProps
)(withRouter(ToolBarContainer))
6 changes: 6 additions & 0 deletions src/containers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export AppContainer from './AppContainer'
export BlogContainer from './BlogContainer'
export ToolBarContainer from './ToolBarContainer'
export HeaderContainer from './HeaderContainer'
export Root from './Root'
export DevTools from './DevTools'
11 changes: 5 additions & 6 deletions src/redux/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ export const FETCH_ARTICLE_SUMMARY = 'FETCH_ARTICLE_SUMMARY'
export const TOGGLE_SIDEBAR = 'TOGGLE_SIDEBAR'
export const TOGGLE_CONTENT = 'TOGGLE_CONTENT'

function shouldFetchArticle(state, id) {
if (state.article.id && state.article.id === id) {
return false
}

return true
// TODO: refactoring to use dux modules, export default reducer but export as for action creators

function shouldFetchArticle(state, id) {
return !(state.article.id && state.article.id === id)
}

function receiveArticle(content, id) {
Expand Down Expand Up @@ -92,7 +91,7 @@ export function fetchMusicList() {
.catch(error => console.info('request error: ', error))
}

export function toggleSideBarAction() {
export function toggleSideBar() {
return {
type: TOGGLE_SIDEBAR
}
Expand Down

0 comments on commit a08d767

Please sign in to comment.