Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
fix issues found in manual testing integration, modify README, prepar…
Browse files Browse the repository at this point in the history
…e 0.10.0
  • Loading branch information
cellog committed Apr 19, 2017
1 parent f1432ae commit 22c8628
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 129 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,21 @@ index.js:
```javascript
import React from 'react'
import { render } from 'react-dom'
import { createStore, applyMiddleware, combineReducers } from 'redux'
import { Provider, connect } from 'react-redux' // new - import connect
import createRouterStore from 'ion-router' // our router - new line
import makeRouter, { makeRouterMiddleware } from 'ion-router' // our router - new line
import routing from 'ion-router/reducer'

import todoApp from './reducers'
import App from './components/App'

// set up the router and create the store
const store = createRouterStore(connect)(todoApp)
const routerMiddleware = makeRouterMiddleware()
const store = createStore(combineReducers({
todoApp,
routing // router reducer here
}), undefined, applyMiddleware(routerMiddleware)) // router middleware
makeRouter(connect, store) // set up the router


render(
Expand Down Expand Up @@ -662,7 +669,7 @@ so instead of:

```javascript
// set up our router
const store = createRouterStore(connect)(reducers)
makeRouter(connect, store)

exitParams = params => ({
required: params.required,
Expand Down Expand Up @@ -692,14 +699,14 @@ exitParams = params => ({
})

// set up our router
createRouterStore(connect, {
makeRouter(connect, store, [{
name: 'test',
path: '/path/:required(/:optional)',
stateToParams=...,
paramsToState=...,
updateState={...},
exitParams={exitParams},
})(reducers)
}])

```

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
"dependencies": {
"history": "^4.5.1",
"invariant": "^2.2.2",
"prop-types": "^15.5.8",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"redux": "^3.6.0",
"route-parser": "0.0.5"
},
"peerDependencies": {
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0",
"prop-types": "^15.0.0",
"redux": "^2.0.0 || ^3.0.0"
},
"devDependencies": {
"@kadira/storybook": "^2.35.3",
"babel-cli": "^6.18.0",
Expand Down
41 changes: 28 additions & 13 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createPath } from 'history'
import * as actions from './actions'
import * as selectors from './selectors'
import * as enhancers from './enhancers'
import reducer from './reducer'

export const filter = (enhancedRoutes, path) => name => enhancedRoutes[name]['@parser'].match(path)
export const diff = (main, second) => main.filter(name => second.indexOf(name) === -1)
Expand All @@ -17,11 +18,12 @@ export function urlFromState(enhancedRoutes, state) {
const toDispatch = []
const updatedRoutes = {}
let url = false
const currentUrl = createPath(state.routing.location)
state.routing.matchedRoutes.forEach((route) => {
const s = enhancedRoutes[route]
const newParams = s.paramsFromState(state)
const newState = s.stateFromParams(newParams)
if (changed(s.params, newParams)) {
if (changed(s.params, newParams).length) {
updatedRoutes[route] = {
...enhancedRoutes[route],
params: newParams,
Expand Down Expand Up @@ -54,9 +56,9 @@ export function urlFromState(enhancedRoutes, state) {
const {
toDispatch: t
} = matchRoutes(enhancedRoutes, tempState, actions.route({ // eslint-disable-line
pathname: url
pathname: url || currentUrl
}), false)
toDispatch.push(actions.push(url))
if (url && url !== currentUrl) toDispatch.push(actions.push(url))
return {
newEnhancedRoutes: { ...enhancedRoutes, ...updatedRoutes },
toDispatch: [...toDispatch, ...t],
Expand Down Expand Up @@ -166,23 +168,36 @@ export function matchRoutes(enhancedRoutes, state, action, updateParams = true)
}
}

function stateWithRoutes(state, action) {
return {
...state,
routing: reducer(state.routing, action)
}
}

export function makeRoute(enhancedRoutes, state, action) {
const newEnhancedRoutes = enhancers.save(action.payload, enhancedRoutes)
const { toDispatch } = matchRoutes(newEnhancedRoutes, stateWithRoutes(state, action),
actions.route(state.routing.location))
return {
newEnhancedRoutes: enhancers.save(action.payload, enhancedRoutes),
toDispatch: []
newEnhancedRoutes,
toDispatch
}
}

export function batchRoutes(enhancedRoutes, state, action) {
const newEnhancedRoutes = {
...enhancedRoutes,
...action.payload.ids.reduce((routes, name) => ({
...routes,
[name]: enhancers.enhanceRoute(action.payload.routes[name])
}), {})
}
const { toDispatch } = matchRoutes(newEnhancedRoutes, stateWithRoutes(state, action),
actions.route(state.routing.location))
return {
newEnhancedRoutes: {
...enhancedRoutes,
...action.payload.ids.reduce((routes, name) => ({
...routes,
[name]: enhancers.enhanceRoute(action.payload.routes[name])
}), {})
},
toDispatch: []
newEnhancedRoutes,
toDispatch
}
}

Expand Down
60 changes: 15 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import createBrowserHistory from 'history/createBrowserHistory'
import { createPath } from 'history'
import { createStore, applyMiddleware } from 'redux'

import routerReducer from './reducer'
import middleware, { actionHandlers } from './middleware'
import * as actions from './actions'
import * as enhancers from './enhancers'
import { connectLink } from './Link'
import { connectRoutes } from './Routes'
import { connectToggle } from './Toggle'
import middleware from './middleware'

export { middleware as makeRouterMiddleware }

export const options = {
server: false,
Expand All @@ -17,6 +17,10 @@ export const options = {
resolve: false,
}


export { actionHandlers } from './middleware'
export reducer from './reducer'

export const setServer = (val = true) => {
options.server = val
}
Expand Down Expand Up @@ -44,47 +48,13 @@ export function synchronousMakeRoutes(routes, opts = options) {
return action
}

export function routingReducer(reducers = state => ({ ...(state || {}) })) {
return (state, action) => {
if (!state) {
return {
routing: routerReducer(),
...(reducers() || {})
}
}
let newState = state
if (!newState.routing) {
newState = {
...state,
routing: routerReducer()
}
} else {
const routing = routerReducer(newState.routing, action)
if (routing !== newState.routing) {
newState = {
...newState,
routing
}
}
}
return reducers(newState, action)
export default function makeRouter(connect, store, routeDefinitions,
isServer = false, opts = options) {
connectLink(connect)
connectRoutes(connect)
connectToggle(connect)
opts.isServer = isServer // eslint-disable-line
if (routeDefinitions) {
store.dispatch(synchronousMakeRoutes(routeDefinitions, opts))
}
}

export default (
connect, routeDefinitions, history = createBrowserHistory(),
isServer = false, opts = options, handlers = actionHandlers) => (reducer, state, enhancer) => {
connectLink(connect)
connectRoutes(connect)
connectToggle(connect)
options.isServer = isServer
const routerMiddleware = applyMiddleware(middleware(history, opts, handlers))(createStore)
const store = routerMiddleware(routingReducer(reducer), state, enhancer)
if (routeDefinitions) {
store.dispatch(synchronousMakeRoutes(routeDefinitions, opts))
}
return {
...store,
dispatch: store.dispatch
}
}
10 changes: 9 additions & 1 deletion src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function processHandler(handler, routes, state, action) {
}

export default function createMiddleware(history = createBrowserHistory(), opts = options,
handlers = actionHandlers) {
handlers = actionHandlers, debug = false) {
let lastLocation = createPath(history.location)
let activeListener = listen // eslint-disable-line
const myHandlers = {
Expand All @@ -78,6 +78,10 @@ export default function createMiddleware(history = createBrowserHistory(), opts
const ret = next(action)
const info = processHandler(handler, opts.enhancedRoutes, store.getState(), action)
setEnhancedRoutes(info.newEnhancedRoutes, opts)
if (debug && info.toDispatch.length) {
console.info(`ion-router PROCESSING: ${action.type}`)
console.info(`dispatching: `, info.toDispatch)
}
info.toDispatch.forEach(act => store.dispatch(act))
return ret
} finally {
Expand All @@ -91,9 +95,13 @@ export default function createMiddleware(history = createBrowserHistory(), opts
lastLocation = a
store.dispatch(actions.route(location))
})
store.dispatch(actions.route(history.location))
return next => (action) => {
const ret = activeListener(store, next, action)
if (action.type === types.ACTION) {
if (!action.payload.route) {
throw new Error(`ion-router action ${action.payload.verb} must be a string or a location object`)
}
history[action.payload.verb](action.payload.route, action.payload.state)
}
return ret
Expand Down
10 changes: 9 additions & 1 deletion test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ describe('helper functions', () => {
hi: 'there',
boo: 'boo'
})).eqls(['hi', 'boo'])
expect(helpers.changed({
composer: undefined,
piece: undefined,
filter: ''
}, {
composer: undefined,
piece: undefined,
filter: ''
})).eqls([])
})
it('urlFromState', () => {
const options = {}
Expand Down Expand Up @@ -100,7 +109,6 @@ describe('helper functions', () => {
},
toDispatch: [
actions.setParamsAndState('hi', { a: 'foo', test: 'tenth' }, { a: 'foo', test: 'tenth' }),
actions.setParamsAndState('there', { }, { }),
actions.setParamsAndState('three', { bar: 'barb', a: 't' }, { bar: 'barb', a: 't' }),
actions.push('foo/bar/tenth'),
actions.matchRoutes(['hi']),
Expand Down
Loading

0 comments on commit 22c8628

Please sign in to comment.