[id].js 1.2 KB
Newer Older
1
import fetch from 'node-fetch'
L
Lukáš Huvar 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

const Person = ({ data, status }) =>
  status === 200 ? (
    <table>
      <thead>
        <tr>
          <th>Name</th>
          <th>Height</th>
          <th>Mass</th>
          <th>Hair color</th>
          <th>Skin color</th>
          <th>Eye color</th>
          <th>Gender</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>{data.name}</td>
          <td>{data.height}</td>
          <td>{data.mass}</td>
          <td>{data.hair_color}</td>
          <td>{data.skin_color}</td>
          <td>{data.eye_color}</td>
          <td>{data.gender}</td>
        </tr>
      </tbody>
    </table>
  ) : (
    <p>{data.message}</p>
  )

33 34 35 36 37 38 39 40 41
export async function getStaticPaths() {
  const response = await fetch('http://localhost:3000/api/people')
  const data = await response.json()

  const paths = data.map(person => ({
    params: {
      id: person.id,
    },
  }))
L
Lukáš Huvar 已提交
42

43 44 45 46 47
  return { paths, fallback: false }
}

export async function getStaticProps({ params }) {
  const response = await fetch(`http://localhost:3000/api/people/${params.id}`)
L
Lukáš Huvar 已提交
48
  const data = await response.json()
49 50 51 52 53 54 55

  return {
    props: {
      data,
      status: response.status,
    },
  }
L
Lukáš Huvar 已提交
56 57 58
}

export default Person