Repository URL to install this package:
|
Version:
6.0.0 ▾
|
/**
* @todo @@perf @deprecated need to update to not use old context
*/
import React from 'react'
import { ComponentType } from 'react'
import { object } from 'prop-types'
import { oneRouter } from './oneRouter'
/**
* @todo there is no state
* @description provide it all the way down through context
* @withRouter
*/
class OneRouter extends React.Component<{ history: any }, { match: any }> {
static childContextTypes = {
router: object.isRequired,
oneRouter: object,
}
getChildContext() {
return {
oneRouter,
router: {
...this.context.router,
history: this.props.history,
route: {
location: this.props.history.location,
match: this.state.match,
},
},
}
}
}
// PureComponent
// @withRouter
class OneRouterWrap extends React.Component<{ router: any }> {
static contextTypes = {
router: object,
}
// can trigger oneRouter here too
// @michael @bhargavi
// // @todo !!!
// oneRouter.onChange(change => {
// // import {sessionContainer} from 'state/container'
// // const { isRegisteredUser, isGuestUser } = sessionContainer
// // () => isRegisteredUser
// // const restrictedRoutes = ['/']
// // oneRouter.set('restrictedRoutes', restrictedRoutes)
// // if (oneRouter.isRestricted === true) {
// // oneRouter.update('/login')
// // }
// })
componentWillMount() {
const props = this.props
// console.log('PROPS_WILLMOUNT__', JSON.stringify(props, null, 2))
oneRouter.props = props
oneRouter.router = this.context.router || props.router
// oneRouter.history = oneRouter.history.router || oneRouter.history
// const { history, isSSR } = this.props
// if (!isSSR) this.unsubscribeFromHistory = history.listen(this.handleLocationChange)
// this.handleLocationChange(history.location)
}
// componentWillUnmount() {
// if (this.unsubscribeFromHistory) {
// this.unsubscribeFromHistory()
// }
// }
// handleLocationChange = location => {
// this.store.notifyRouteChange(location)
// }
// ===
// componentWillReceiveProps(props) {
// // console.log('___PROPS___', JSON.stringify(props, null, 2))
// props = props || this.props
// oneRouter.props = clone(props)
// oneRouter.router = this.context.router || props.router
// oneRouter.history = oneRouter.history.router || oneRouter.history
// oneRouter.entries()
// }
render() {
return null
}
}
const provideOneRouter = (_props: any) => {
return React.createElement(OneRouterWrap, _props)
}
// not used?
const withOneRouter = (Target: ComponentType) => {
return class OneRouterWrapper extends React.Component {
render() {
const attributes = {
...oneRouter,
...this.props,
}
return <Target {...attributes} />
}
}
}
export {
provideOneRouter as OneRouterContainer,
provideOneRouter,
withOneRouter,
OneRouter,
oneRouter,
OneRouterWrap,
}