page grid update: load less data
This commit is contained in:
1 parent
1318328fe7
commit
e800de1f92
5 files changed
+100
-89
No files matched your search
+2
-4
@@ -6,12 +6,12 @@ import { HudozkaTitle } from 'src/components/HudozkaTitle'
|
|||||||
import { MetaBuilder } from 'src/lib/meta'
|
import { MetaBuilder } from 'src/lib/meta'
|
||||||
import { getPagesCardsByTags } from 'src/rest'
|
import { getPagesCardsByTags } from 'src/rest'
|
||||||
import { NextPage } from 'next'
|
import { NextPage } from 'next'
|
||||||
import { IPage, IMeta } from 'src/types'
|
import { IMeta, PageCardData } from 'src/types'
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
title: string
|
title: string
|
||||||
meta: IMeta
|
meta: IMeta
|
||||||
items: IPage[]
|
items: PageCardData[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const Index: NextPage<IProps> = props => (
|
const Index: NextPage<IProps> = props => (
|
||||||
@@ -32,8 +32,6 @@ const Index: NextPage<IProps> = props => (
|
|||||||
|
|
||||||
<PageGrid
|
<PageGrid
|
||||||
items={props.items}
|
items={props.items}
|
||||||
prevPage={null}
|
|
||||||
nextPage={null}
|
|
||||||
/>
|
/>
|
||||||
</App>
|
</App>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { ImageDefinition, IPage, ImageSize } from 'src/types'
|
import { ImageSize, PageCardData } from 'src/types'
|
||||||
import { Pager } from '../Pager'
|
|
||||||
import { CardGrid } from '../CardGrid'
|
import { CardGrid } from '../CardGrid'
|
||||||
import { Card, CardLayout } from '../Card'
|
import { Card, CardLayout } from '../Card'
|
||||||
import { Date } from './Date'
|
import { Date } from './Date'
|
||||||
@@ -7,87 +6,56 @@ import { Block } from '../Block'
|
|||||||
import { Spacer } from '../Spacer'
|
import { Spacer } from '../Spacer'
|
||||||
import { imageSrcSet } from 'src/lib/image'
|
import { imageSrcSet } from 'src/lib/image'
|
||||||
|
|
||||||
function getImage(image?: ImageDefinition) {
|
|
||||||
const sizes = [ImageSize.large, ImageSize.medium, ImageSize.small]
|
|
||||||
if (!image) {
|
|
||||||
const defaultSrc = process.env.APP_CARD_DEFAULT_IMAGE
|
|
||||||
return {
|
|
||||||
src: defaultSrc,
|
|
||||||
srcSet: imageSrcSet(defaultSrc, sizes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const src = image.artifacts.large.src
|
|
||||||
return {
|
|
||||||
src,
|
|
||||||
srcSet: imageSrcSet(src, sizes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PageGridProps = {
|
export type PageGridProps = {
|
||||||
items: IPage[]
|
items: PageCardData[]
|
||||||
nextPage: number | null
|
|
||||||
prevPage: number | null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PageGrid: React.FC<PageGridProps> = ({ items, nextPage, prevPage, ...props }) => {
|
export const PageGrid: React.FC<PageGridProps> = props => {
|
||||||
const nextHref = nextPage ? `/articles/${nextPage}` : null
|
|
||||||
let prevHref = prevPage ? `/articles/${prevPage}` : null
|
|
||||||
if (prevPage === 1) {
|
|
||||||
prevHref = '/'
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<CardGrid
|
||||||
<CardGrid
|
style={{
|
||||||
style={{
|
marginBottom: 'var(--size-m)',
|
||||||
marginBottom: 'var(--size-m)',
|
}}
|
||||||
}}
|
>
|
||||||
>
|
{props.items.map((item, i) => {
|
||||||
{items.map((item, i) => {
|
const gridColumn = item.featured ? 'span 2' : 'auto'
|
||||||
const gridColumn = item.featured ? 'span 2' : 'auto'
|
const layout: CardLayout = item.featured ? 'featured' : 'simple'
|
||||||
const layout: CardLayout = item.featured ? 'featured' : 'simple'
|
const src = item.coverSrc
|
||||||
const { src, srcSet } = getImage(item.preview)
|
const srcSet = imageSrcSet(item.coverSrc, [ImageSize.large, ImageSize.medium, ImageSize.small])
|
||||||
|
|
||||||
const content = !item.featured
|
const content = !item.featured
|
||||||
? (
|
? (
|
||||||
<Block
|
<Block
|
||||||
direction={'vertical'}
|
direction={'vertical'}
|
||||||
>
|
|
||||||
{item.title}
|
|
||||||
<Spacer />
|
|
||||||
<Date style={{
|
|
||||||
marginTop: 'var(--size-m)',
|
|
||||||
}}>{item.date}</Date>
|
|
||||||
</Block>
|
|
||||||
) : (
|
|
||||||
item.title
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Card
|
|
||||||
key={i}
|
|
||||||
href={item.url}
|
|
||||||
img={{
|
|
||||||
alt: item.title,
|
|
||||||
src,
|
|
||||||
srcSet,
|
|
||||||
}}
|
|
||||||
layout={layout}
|
|
||||||
style={{
|
|
||||||
gridColumn,
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{content}
|
{item.title}
|
||||||
</Card>
|
<Spacer />
|
||||||
|
<Date style={{
|
||||||
|
marginTop: 'var(--size-m)',
|
||||||
|
}}>{item.date}</Date>
|
||||||
|
</Block>
|
||||||
|
) : (
|
||||||
|
item.title
|
||||||
)
|
)
|
||||||
})}
|
|
||||||
</CardGrid>
|
|
||||||
|
|
||||||
<Pager
|
return (
|
||||||
nextHref={nextHref}
|
<Card
|
||||||
prevHref={prevHref}
|
key={i}
|
||||||
/>
|
href={item.url}
|
||||||
</>
|
img={{
|
||||||
|
alt: item.title,
|
||||||
|
src,
|
||||||
|
srcSet,
|
||||||
|
}}
|
||||||
|
layout={layout}
|
||||||
|
style={{
|
||||||
|
gridColumn,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</Card>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
</CardGrid>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,13 @@
|
|||||||
export function isPartOfPath(part: string, path: string): boolean {
|
export function isPartOfPath(part: string, path: string): boolean {
|
||||||
return path.startsWith(part)
|
return path.startsWith(part)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function queryList(key: string, values: string[]): string {
|
||||||
|
if (values.length === 0) {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
.map(value => `${key}=${value}`)
|
||||||
|
.join('&')
|
||||||
|
}
|
||||||
+36
-9
@@ -1,26 +1,53 @@
|
|||||||
import { createApiUrl, requestGet, IResponseItems } from './next-lib'
|
import { createApiUrl, requestGet, IResponseItems } from './next-lib'
|
||||||
import { IPage } from './types'
|
import { IPage, PageCardData } from './types'
|
||||||
|
import { queryList } from './lib/url'
|
||||||
|
|
||||||
export type QuerySlice = {
|
export type QuerySlice = {
|
||||||
skip: number
|
skip: number
|
||||||
limit: number
|
limit: number
|
||||||
sortBy: string
|
sortBy: string
|
||||||
|
fields?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPagesByTags(tags: string[], { skip, limit, sortBy }: QuerySlice) {
|
export async function getPagesByTags<T = IPage>(tags: string[], { skip, limit, sortBy, fields }: QuerySlice) {
|
||||||
const tagQuery = tags
|
const tag = queryList('tag', tags)
|
||||||
.map(x => `tag=${x}`)
|
const field = queryList('field', fields ?? [])
|
||||||
.join('&')
|
|
||||||
|
|
||||||
const url = `/api/pages/tags?${tagQuery}&skip=${skip}&limit=${limit}&sortBy=${sortBy}`
|
const url = `/api/pages/tags?${tag}&${field}&skip=${skip}&limit=${limit}&sortBy=${sortBy}`
|
||||||
const res = await requestGet<IResponseItems<IPage>>(createApiUrl(url), null)
|
const res = await requestGet<IResponseItems<T>>(createApiUrl(url), null)
|
||||||
|
|
||||||
return res?.items
|
return res?.items
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getPagesCardsByTags(tags: string[]) {
|
export async function getPagesCardsByTags(tags: string[]): Promise<PageCardData[]> {
|
||||||
|
type ResponseItem = {
|
||||||
|
url: string
|
||||||
|
title: string
|
||||||
|
featured: boolean
|
||||||
|
date: string
|
||||||
|
coverSrc: string
|
||||||
|
}
|
||||||
|
|
||||||
const skip = 0
|
const skip = 0
|
||||||
const limit = 1000
|
const limit = 1000
|
||||||
|
const fields = [
|
||||||
|
'url',
|
||||||
|
'title',
|
||||||
|
'featured',
|
||||||
|
'date',
|
||||||
|
'coverSrc'
|
||||||
|
]
|
||||||
|
|
||||||
return getPagesByTags(tags, { skip, limit, sortBy: '-date' })
|
const items = await getPagesByTags<ResponseItem>(tags, { fields, skip, limit, sortBy: '-date' })
|
||||||
|
if (!Array.isArray(items)) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
return items.map(item => ({
|
||||||
|
url: item.url,
|
||||||
|
title: item.title,
|
||||||
|
featured: item.featured,
|
||||||
|
date: new Date(item.date),
|
||||||
|
coverSrc: item.coverSrc,
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,14 @@ export interface IBreadcumbsPart {
|
|||||||
href: string
|
href: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PageCardData = {
|
||||||
|
url: string
|
||||||
|
title: string
|
||||||
|
featured: boolean
|
||||||
|
date: Date
|
||||||
|
coverSrc: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface IPage {
|
export interface IPage {
|
||||||
id: string
|
id: string
|
||||||
title: string
|
title: string
|
||||||
|
|||||||
Reference in new issue
Block a user