Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 137a64b

Browse files
committed
add redux-simple-router
1 parent 39fb382 commit 137a64b

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

package.json

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,57 @@
2020
"serve-favicon": "^2.3.0"
2121
},
2222
"devDependencies": {
23-
"bootstrap": "^3.3.5",
24-
"bootstrap-sass": "^3.3.5",
25-
"classnames": "^2.1.2",
26-
"extract-text-webpack-plugin": "^0.9.1",
27-
"font-awesome": "^4.5.0",
28-
"history": "^1.13.1",
29-
"html-webpack-plugin": "^1.7.0",
30-
"isomorphic-fetch": "^2.2.0",
31-
"react": "^0.14.0",
32-
"react-addons-css-transition-group": "^0.14.3",
33-
"react-addons-linked-state-mixin": "^0.14.3",
34-
"react-alert": "^1.0.13",
35-
"react-bootstrap": "^0.28.1",
36-
"react-cookie": "^0.4.2",
37-
"react-dom": "^0.14.0",
38-
"react-mixin": "^2.0.2",
39-
"react-redux": "^4.0.0",
40-
"react-router": "^1.0.0",
41-
"redux": "^3.0.0",
42-
"redux-router": "^1.0.0-beta5",
43-
"redux-thunk": "^1.0.0",
4423
"babel-core": "^6.3.15",
4524
"babel-loader": "^6.2.0",
4625
"babel-plugin-react-transform": "^2.0.0-beta1",
4726
"babel-preset-es2015": "^6.3.13",
4827
"babel-preset-react": "^6.3.13",
4928
"babel-preset-stage-0": "^6.3.13",
29+
"bootstrap": "^3.3.5",
30+
"bootstrap-sass": "^3.3.5",
31+
"classnames": "^2.1.2",
5032
"compass-mixins": "^0.12.7",
5133
"css-loader": "^0.23.0",
5234
"del": "^2.2.0",
5335
"expect": "^1.8.0",
5436
"express": "^4.13.3",
37+
"extract-text-webpack-plugin": "^0.9.1",
38+
"font-awesome": "^4.5.0",
5539
"gulp": "^3.9.0",
5640
"gulp-env": "^0.4.0",
5741
"gulp-nodemon": "^2.0.6",
5842
"gulp-sequence": "^0.4.4",
5943
"gulp-util": "^3.0.7",
44+
"history": "^1.13.1",
45+
"html-webpack-plugin": "^1.7.0",
6046
"image-webpack-loader": "^1.6.2",
47+
"isomorphic-fetch": "^2.2.0",
6148
"jsdom": "^5.6.1",
6249
"mocha": "^2.2.5",
6350
"node-libs-browser": "^0.5.2",
6451
"node-sass": "^3.4.2",
6552
"open": "0.0.5",
53+
"react": "^0.14.0",
54+
"react-addons-css-transition-group": "^0.14.3",
55+
"react-addons-linked-state-mixin": "^0.14.3",
6656
"react-addons-test-utils": "^0.14.0",
57+
"react-alert": "^1.0.13",
58+
"react-bootstrap": "^0.28.1",
59+
"react-cookie": "^0.4.2",
60+
"react-dom": "^0.14.0",
61+
"react-mixin": "^2.0.2",
62+
"react-redux": "^4.0.0",
63+
"react-router": "^1.0.0",
6764
"react-transform-catch-errors": "^1.0.0",
6865
"react-transform-hmr": "^1.0.1",
6966
"redbox-react": "^1.2.0",
67+
"redux": "^3.0.0",
7068
"redux-devtools": "^3.0.0-beta-3",
7169
"redux-devtools-dock-monitor": "^1.0.0-beta-3",
7270
"redux-devtools-log-monitor": "^1.0.0-beta-3",
71+
"redux-router": "^1.0.0-beta5",
72+
"redux-simple-router": "^1.0.2",
73+
"redux-thunk": "^1.0.0",
7374
"sass-loader": "^3.1.2",
7475
"style-loader": "^0.12.3",
7576
"url-loader": "^0.5.7",

src/actions/auth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {GET_CAPTCHAURL,LOGIN_SUCCESS,LOGIN_FAILURE,USERINFO_SUCCESS,LOGOUT_USER,USERINFO_FAILURE,UPDATE_USER_FAILURE,UPDATE_USER_SUCCESS} from './ActionTypes'
22
import {API_ROOT} from '../config'
33
import fetch from 'isomorphic-fetch'
4-
import { pushState } from 'redux-router'
4+
import { pushPath } from 'redux-simple-router'
55
import cookie from 'react-cookie'
66

77
//登录
@@ -37,7 +37,7 @@ export function localLogin(userInfo) {
3737
//获取用户信息
3838
dispatch(getUserInfo(json.token))
3939
dispatch(loginSuccess(json.token))
40-
dispatch(pushState(null, '/'))
40+
dispatch(pushPath('/'))
4141
}).catch( err =>{
4242
//登录异常
4343
return dispatch(loginFailure(err))
@@ -90,7 +90,7 @@ export function logout() {
9090
return dispatch => {
9191
cookie.remove('token')
9292
dispatch({type: LOGOUT_USER})
93-
dispatch(pushState(null, '/'))
93+
dispatch(pushPath('/'))
9494
}
9595
}
9696
//修改用户资料

src/components/apps/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { Component, PropTypes } from 'react'
22
import { bindActionCreators } from 'redux'
33
import { connect } from 'react-redux'
44
import * as Actions from '../../actions/auth'
5-
import {pushState} from 'redux-router'
65

76
export default class MobileApps extends Component {
87
constructor(props){

src/components/login/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { Component, PropTypes } from 'react'
22
import { bindActionCreators } from 'redux'
33
import { connect } from 'react-redux'
44
import * as Actions from '../../actions/auth'
5-
import {pushState} from 'redux-router'
65
import SNSLogin from './snsLogin'
76

87
class Login extends Component {
@@ -33,6 +32,8 @@ class Login extends Component {
3332
const { auth } = nextProps
3433
if(auth.errMsg){
3534
msg.error(auth.errMsg)
35+
}else if(!this.props.token && auth.token){
36+
msg.success('登录成功.')
3637
}
3738
}
3839

@@ -107,8 +108,7 @@ function mapStateToProps(state) {
107108

108109
function mapDispatchToProps(dispatch) {
109110
return {
110-
actions: bindActionCreators(Actions, dispatch),
111-
pushState: bindActionCreators(pushState, dispatch)
111+
actions: bindActionCreators(Actions, dispatch)
112112
}
113113
}
114114

src/components/settings/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { Component, PropTypes } from 'react'
22
import { bindActionCreators } from 'redux'
33
import { connect } from 'react-redux'
44
import * as Actions from '../../actions/auth'
5-
import {pushState} from 'redux-router'
65

76
class Settings extends Component {
87
constructor(props){
@@ -17,7 +16,14 @@ class Settings extends Component {
1716
const {actions} = this.props
1817
actions.updateUser({nickname:nickname.value})
1918
}
20-
19+
componentWillReceiveProps(nextProps){
20+
const { auth } = nextProps
21+
if(auth.errMsg){
22+
msg.error(auth.errMsg)
23+
}else if(auth.user !== this.props.auth.user){
24+
msg.success('修改成功.')
25+
}
26+
}
2127
render() {
2228
const { actions,auth } = this.props
2329
return (
@@ -60,8 +66,7 @@ function mapStateToProps(state) {
6066

6167
function mapDispatchToProps(dispatch) {
6268
return {
63-
actions: bindActionCreators(Actions, dispatch),
64-
pushState: bindActionCreators(pushState, dispatch)
69+
actions: bindActionCreators(Actions, dispatch)
6570
}
6671
}
6772

src/containers/Root.dev.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React,{Component} from 'react'
22
import DevTools from './DevTools'
33
import {Provider} from 'react-redux'
4-
import {ReduxRouter} from 'redux-router'
5-
import {Route,IndexRoute} from 'react-router'
4+
//import {ReduxRouter} from 'redux-router'
5+
6+
import { Router,Route,IndexRoute} from 'react-router'
67
import Home from './Home'
78
import Main from '../components/main'
89
import Article from '../components/article'
@@ -13,19 +14,19 @@ import {redirectToBack,redirectToLogin} from '../utils/authService'
1314

1415
export default class Root extends Component{
1516
render(){
16-
const {store} = this.props
17+
const {store,history} = this.props
1718
return (
1819
<Provider store={store}>
1920
<div>
20-
<ReduxRouter>
21+
<Router history={history}>
2122
<Route path="/" component={Home}>
2223
<IndexRoute component={Main}/>
2324
<Article path="/article/:id" component={Article} />
2425
<Login path="/login" component={Login} onEnter={redirectToBack} />
2526
<Settings path="/settings" component={Settings} onEnter={redirectToLogin} />
2627
<MobileApps path="/apps" component={MobileApps} />
2728
</Route>
28-
</ReduxRouter>
29+
</Router>
2930
<DevTools />
3031
</div>
3132
</Provider>

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'babel-core/register'
22
import React from 'react'
33
import { render } from 'react-dom'
4-
import { Provider } from 'react-redux'
4+
import { createHistory } from 'history'
5+
import { syncReduxAndRouter, routeReducer } from 'redux-simple-router'
6+
57
import configureStore from './store/configureStore'
68
import Root from './containers/Root'
79

@@ -10,8 +12,10 @@ import './assets/styles/index.scss'
1012

1113

1214
const store = configureStore()
15+
const history = createHistory()
16+
syncReduxAndRouter(history, store)
1317

1418
render(
15-
<Root store={store} />,
19+
<Root store={store} history={history} />,
1620
document.getElementById('root')
1721
)

src/reducers/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { combineReducers } from 'redux'
2-
import { routerStateReducer } from 'redux-router'
2+
//import { routerStateReducer } from 'redux-router'
3+
import { routeReducer } from 'redux-simple-router'
4+
35
import {CHANGE_STYLE_MODE,GET_INDEX_IMG,CHANGE_OPTIONS,GET_CAPTCHAURL} from '../actions/ActionTypes'
46
import tagList from './tag'
57
import {articleList, articleDetail,prenextArticle} from './article'
@@ -46,7 +48,8 @@ const rootReducer = combineReducers({
4648
options,
4749
captchaUrl,
4850
auth,
49-
router: routerStateReducer
51+
routing: routeReducer
52+
//router: routerStateReducer
5053
})
5154

5255
export default rootReducer

src/routes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react'
2+
import { Route, IndexRoute, Redirect } from 'react-router'
3+
4+
import Home from './containers/Home'
5+
import Main from './components/main'
6+
import Article from './components/article'
7+
import Login from './components/login'
8+
import Settings from './components/settings'
9+
import MobileApps from './components/apps'
10+
import {redirectToBack,redirectToLogin} from './utils/authService'
11+
12+
export default (
13+
<Route path="/" component={Home}>
14+
<IndexRoute component={Main}/>
15+
<Article path="/article/:id" component={Article} />
16+
<Login path="/login" component={Login} onEnter={redirectToBack} />
17+
<Settings path="/settings" component={Settings} onEnter={redirectToLogin} />
18+
<MobileApps path="/apps" component={MobileApps} />
19+
</Route>
20+
)

src/store/configureStore.dev.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { createStore,compose,applyMiddleware } from 'redux'
22
import rootReducer from '../reducers'
33
import thunkMiddleware from 'redux-thunk'
44

5-
import {reduxReactRouter} from 'redux-router'
6-
import createHistory from 'history/lib/createBrowserHistory'
5+
//import {reduxReactRouter} from 'redux-router'
6+
//import createHistory from 'history/lib/createBrowserHistory'
77
//开发调试工具.
88
import {persistState} from 'redux-devtools'
99
import DevTools from '../containers/DevTools'
1010
//使用chrome 扩展来做调试工具.
1111
// window.devToolsExtension ? window.devToolsExtension() : f => f
1212
const finalCreateStore = compose(
1313
applyMiddleware(thunkMiddleware),
14-
reduxReactRouter({ createHistory }),
14+
//reduxReactRouter({ createHistory }),
1515
DevTools.instrument(),
1616
persistState(
1717
window.location.href.match(

0 commit comments

Comments
 (0)