From 087a4b6172f2d094d2f20884ed918dc68acb8bc0 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 3 Mar 2020 05:26:02 +0300 Subject: [PATCH] index page update; rest module --- pages/index.tsx | 41 ++++++++++------------------------------- src/rest.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 src/rest.ts diff --git a/pages/index.tsx b/pages/index.tsx index d8f4073a..f62b1522 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -4,19 +4,17 @@ import { Meta } from 'src/components/Meta' import { PageGrid } from 'src/components/PageGrid' import { HudozkaTitle } from 'src/components/HudozkaTitle' import { MetaBuilder } from 'src/lib/meta' -import { createApiUrl, requestGet, IResponseItems } from 'src/next-lib' +import { getPagesCardsByTags } from 'src/rest' import { NextPage } from 'next' -import { IPage } from 'src/types' +import { IPage, IMeta } from 'src/types' interface IProps { title: string - meta: any - prevPage: number - nextPage: number - items: any[] + meta: IMeta + items: IPage[] } -const Page: NextPage = props => ( +const Index: NextPage = props => ( = props => ( ) export const unstable_getStaticProps = async () => { - const page = 1 - // const pageSize = parseInt(process.env.APP_ARTICLES_PAGE_SIZE) - const pageSize = 300 - - const limit = pageSize - const skip = (page - 1) * pageSize - - const tags = [ + const items = await getPagesCardsByTags([ 'event', 'album', '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>(createApiUrl(url), null) - const items = (res.items || []) - // const nextPage = res.nextPage - const nextPage = 2 - const prevPage = res.prevPage + ]) const title = 'Шлиссельбургская ДХШ' const meta = (new MetaBuilder()) .setTitle(title) @@ -74,12 +55,10 @@ export const unstable_getStaticProps = async () => { return { props: { items, - nextPage, - prevPage, title, meta, } } } -export default Page +export default Index diff --git a/src/rest.ts b/src/rest.ts new file mode 100644 index 00000000..8ac761f8 --- /dev/null +++ b/src/rest.ts @@ -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>(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' }) +}