index page update; rest module

This commit is contained in:
tmshv committed 2020-03-03 05:26:02 +03:00
1 parent 5e8ee6eec9
commit 087a4b6172
2 files changed
+36 -31

No files matched your search

+10 -31
View File
@@ -4,19 +4,17 @@ import { Meta } from 'src/components/Meta'
import { PageGrid } from 'src/components/PageGrid' import { PageGrid } from 'src/components/PageGrid'
import { HudozkaTitle } from 'src/components/HudozkaTitle' import { HudozkaTitle } from 'src/components/HudozkaTitle'
import { MetaBuilder } from 'src/lib/meta' import { MetaBuilder } from 'src/lib/meta'
import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' import { getPagesCardsByTags } from 'src/rest'
import { NextPage } from 'next' import { NextPage } from 'next'
import { IPage } from 'src/types' import { IPage, IMeta } from 'src/types'
interface IProps { interface IProps {
title: string title: string
meta: any meta: IMeta
prevPage: number items: IPage[]
nextPage: number
items: any[]
} }
const Page: NextPage<IProps> = props => ( const Index: NextPage<IProps> = props => (
<App <App
showAuthor={true} showAuthor={true}
wide={true} wide={true}
@@ -34,35 +32,18 @@ const Page: NextPage<IProps> = props => (
<PageGrid <PageGrid
items={props.items} items={props.items}
prevPage={props.prevPage} prevPage={null}
nextPage={props.nextPage} nextPage={null}
/> />
</App> </App>
) )
export const unstable_getStaticProps = async () => { export const unstable_getStaticProps = async () => {
const page = 1 const items = await getPagesCardsByTags([
// const pageSize = parseInt(process.env.APP_ARTICLES_PAGE_SIZE)
const pageSize = 300
const limit = pageSize
const skip = (page - 1) * pageSize
const tags = [
'event', 'event',
'album', 'album',
'post', 'post',
] ])
const tagQuery = tags
.map(x => `tag=${x}`)
.join('&')
const url = `/api/pages/tags?${tagQuery}&skip=${skip}&limit=${limit}&sortBy=-date`
const res = await requestGet<IResponseItems<IPage>>(createApiUrl(url), null)
const items = (res.items || [])
// const nextPage = res.nextPage
const nextPage = 2
const prevPage = res.prevPage
const title = 'Шлиссельбургская ДХШ' const title = 'Шлиссельбургская ДХШ'
const meta = (new MetaBuilder()) const meta = (new MetaBuilder())
.setTitle(title) .setTitle(title)
@@ -74,12 +55,10 @@ export const unstable_getStaticProps = async () => {
return { return {
props: { props: {
items, items,
nextPage,
prevPage,
title, title,
meta, meta,
} }
} }
} }
export default Page export default Index
+26
View File
@@ -0,0 +1,26 @@
import { createApiUrl, requestGet, IResponseItems } from './next-lib'
import { IPage } from './types'
export type QuerySlice = {
skip: number
limit: number
sortBy: string
}
export async function getPagesByTags(tags: string[], { skip, limit, sortBy }: QuerySlice) {
const tagQuery = tags
.map(x => `tag=${x}`)
.join('&')
const url = `/api/pages/tags?${tagQuery}&skip=${skip}&limit=${limit}&sortBy=${sortBy}`
const res = await requestGet<IResponseItems<IPage>>(createApiUrl(url), null)
return res?.items
}
export async function getPagesCardsByTags(tags: string[]) {
const skip = 0
const limit = 1000
return getPagesByTags(tags, { skip, limit, sortBy: '-date' })
}