article page input data refactoring

This commit is contained in:
tmshv committed 2020-02-25 00:14:00 +03:00
1 parent 66b26bc778
commit 21a89c7d66
1 file changed
+11 -12
+11 -12
View File
@@ -4,12 +4,13 @@ import { App } from 'src/components/App'
import { Article } from 'src/components/Article' import { Article } from 'src/components/Article'
import { Html } from 'src/components/Html' import { Html } from 'src/components/Html'
import { Meta } from 'src/components/Meta' import { Meta } from 'src/components/Meta'
import { meta } from 'src/lib/meta' import { meta, MetaBuilder } from 'src/lib/meta'
import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib'
import { NextPage } from 'next' import { NextPage } from 'next'
import { IArticle } from 'src/types' import { IArticle } from 'src/types'
type Props = { type Props = {
title: string
meta: any meta: any
data: IArticle data: IArticle
} }
@@ -25,11 +26,11 @@ const Page: NextPage<Props> = props => {
showAuthor={true} showAuthor={true}
> >
<Head> <Head>
<title>{props.data.title}</title> <title>{props.title}</title>
<Meta meta={props.meta} /> <Meta meta={props.meta} />
</Head> </Head>
<Article <Article
title={props.data.title} title={props.title}
date={props.data.date} date={props.data.date}
tags={props.data.tags} tags={props.data.tags}
shareable={true} shareable={true}
@@ -43,24 +44,22 @@ const Page: NextPage<Props> = props => {
} }
export const unstable_getStaticProps = async (ctx: any) => { export const unstable_getStaticProps = async (ctx: any) => {
const pageUrl = '/'
const id = ctx.params.slug const id = ctx.params.slug
const data = await requestGet(createApiUrl(`/api/articles/${id}`), null) const data = await requestGet<IArticle>(createApiUrl(`/api/articles/${id}`), null)
if (!data) { if (!data) {
return null return null
} }
const image = get(data, 'preview.artifacts.fb', {}) const meta = (new MetaBuilder())
.setImage(data.preview)
.setTitle(data.title)
.build()
return { return {
props: { props: {
title: data.title,
data, data,
meta: meta({ meta,
title: data.title,
image: image.src,
imageWidth: image.width,
imageHeight: image.height,
}),
} }
} }
} }