2018-12-17 16:34:32 +00:00
|
|
|
import { Component } from 'react'
|
2018-02-03 16:11:47 +00:00
|
|
|
import Link from 'next/link'
|
2018-05-13 22:00:03 +00:00
|
|
|
import fetch from 'isomorphic-unfetch'
|
2018-02-03 16:11:47 +00:00
|
|
|
|
|
|
|
export default class extends Component {
|
|
|
|
static async getInitialProps ({ req, query }) {
|
|
|
|
const isServer = !!req
|
|
|
|
|
|
|
|
console.log('getInitialProps called:', isServer ? 'server' : 'client')
|
|
|
|
|
|
|
|
if (isServer) {
|
|
|
|
// When being rendered server-side, we have access to our data in query that we put there in routes/item.js,
|
|
|
|
// saving us an http call. Note that if we were to try to require('../operations/get-item') here,
|
|
|
|
// it would result in a webpack error.
|
|
|
|
return { item: query.itemData }
|
|
|
|
} else {
|
|
|
|
// On the client, we should fetch the data remotely
|
2018-12-17 16:34:32 +00:00
|
|
|
const res = await fetch('/_data/item', {
|
|
|
|
headers: { Accept: 'application/json' }
|
|
|
|
})
|
2018-02-03 16:11:47 +00:00
|
|
|
const json = await res.json()
|
|
|
|
return { item: json }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<div className='item'>
|
2018-12-17 16:34:32 +00:00
|
|
|
<div>
|
|
|
|
<Link href='/'>
|
|
|
|
<a>Back Home</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2018-02-03 16:11:47 +00:00
|
|
|
<h1>{this.props.item.title}</h1>
|
2018-12-17 16:34:32 +00:00
|
|
|
<h2>
|
|
|
|
{this.props.item.subtitle} - {this.props.item.seller}
|
|
|
|
</h2>
|
2018-02-03 16:11:47 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|