2017-11-13 19:37:43 +00:00
|
|
|
import React from 'react'
|
|
|
|
import Link from 'next/link'
|
2018-07-21 23:37:59 +00:00
|
|
|
import { of, Subject } from 'rxjs'
|
|
|
|
import { StateObservable } from 'redux-observable'
|
|
|
|
import { connect } from 'react-redux'
|
2017-11-13 19:37:43 +00:00
|
|
|
import CharacterInfo from '../components/CharacterInfo'
|
2018-02-04 11:56:32 +00:00
|
|
|
import { rootEpic } from '../redux/epics'
|
|
|
|
import * as actions from '../redux/actions'
|
2017-11-13 19:37:43 +00:00
|
|
|
|
|
|
|
class Counter extends React.Component {
|
|
|
|
static async getInitialProps ({ store, isServer }) {
|
2018-07-21 23:37:59 +00:00
|
|
|
const state$ = new StateObservable(new Subject(), store.getState())
|
2018-02-04 11:56:32 +00:00
|
|
|
const resultAction = await rootEpic(
|
|
|
|
of(actions.fetchCharacter(isServer)),
|
2018-07-21 23:37:59 +00:00
|
|
|
state$
|
2018-02-04 11:56:32 +00:00
|
|
|
).toPromise() // we need to convert Observable to Promise
|
2017-11-13 19:37:43 +00:00
|
|
|
store.dispatch(resultAction)
|
|
|
|
|
|
|
|
return { isServer }
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
this.props.startFetchingCharacters()
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.props.stopFetchingCharacters()
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h1>Index Page</h1>
|
|
|
|
<CharacterInfo />
|
|
|
|
<br />
|
|
|
|
<nav>
|
|
|
|
<Link href='/other'><a>Navigate to "/other"</a></Link>
|
|
|
|
</nav>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 23:37:59 +00:00
|
|
|
export default connect(
|
2017-11-13 19:37:43 +00:00
|
|
|
null,
|
|
|
|
{
|
2018-02-04 11:56:32 +00:00
|
|
|
startFetchingCharacters: actions.startFetchingCharacters,
|
|
|
|
stopFetchingCharacters: actions.stopFetchingCharacters
|
2018-03-27 18:11:03 +00:00
|
|
|
}
|
2017-11-13 19:37:43 +00:00
|
|
|
)(Counter)
|