Skip to content

Commit

Permalink
chore(lottery): use getServerProps
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Feb 16, 2020
1 parent b6af604 commit ccae725
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/pages/lottery.tsx
Expand Up @@ -13,6 +13,21 @@ type Props = {
id: string
}

type ServerProps = {
props: Props
}

export async function unstable_getServerProps(): Promise<ServerProps> {
const ids = await getFortunes().catch((): string[] => [])
const id = ids[Math.floor(Math.random() * ids.length)]

if (!id) throw new TypeError("Fortune doesn't exist.")

return {
props: { id }
}
}

const LotteryPage: NextPage<Props> = ({ id }) => {
const router = useRouter()

Expand Down Expand Up @@ -44,13 +59,4 @@ const LotteryPage: NextPage<Props> = ({ id }) => {
)
}

LotteryPage.getInitialProps = async (): Promise<Props> => {
const ids = await getFortunes().catch((): string[] => [])
const id = ids[Math.floor(Math.random() * ids.length)]

if (!id) throw new TypeError("Fortune doesn't exist.")

return { id }
}

export default LotteryPage

0 comments on commit ccae725

Please sign in to comment.