2021-04-22 22:14:32 +00:00
|
|
|
import { useQuery } from '@apollo/client'
|
|
|
|
import Item, { ItemSkeleton } from './item'
|
2021-04-14 23:56:29 +00:00
|
|
|
import styles from './items.module.css'
|
2021-06-22 17:47:49 +00:00
|
|
|
import { MORE_ITEMS } from '../fragments/items'
|
2021-09-06 22:36:08 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2021-09-30 15:46:58 +00:00
|
|
|
import MoreFooter from './more-footer'
|
2021-04-14 23:56:29 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
export default function Items ({ variables, rank, items, cursor }) {
|
2021-09-06 22:36:08 +00:00
|
|
|
const router = useRouter()
|
2021-09-30 15:46:58 +00:00
|
|
|
const { data, fetchMore } = useQuery(MORE_ITEMS, {
|
2021-09-06 22:36:08 +00:00
|
|
|
variables,
|
|
|
|
fetchPolicy: router.query.cache ? 'cache-first' : undefined
|
2021-06-22 17:47:49 +00:00
|
|
|
})
|
2021-09-30 15:46:58 +00:00
|
|
|
|
|
|
|
if (!data && !items) {
|
2021-06-24 23:58:58 +00:00
|
|
|
return <ItemsSkeleton rank={rank} />
|
2021-06-22 17:47:49 +00:00
|
|
|
}
|
2021-04-22 22:14:32 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
if (data) {
|
|
|
|
({ moreItems: { items, cursor } } = data)
|
|
|
|
}
|
|
|
|
|
2021-06-22 17:47:49 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-04-22 22:14:32 +00:00
|
|
|
<div className={styles.grid}>
|
2021-06-22 17:47:49 +00:00
|
|
|
{items.map((item, i) => (
|
|
|
|
<Item item={item} rank={rank && i + 1} key={item.id} />
|
2021-04-22 22:14:32 +00:00
|
|
|
))}
|
|
|
|
</div>
|
2021-09-30 15:46:58 +00:00
|
|
|
<MoreFooter
|
|
|
|
cursor={cursor} fetchMore={fetchMore}
|
|
|
|
Skeleton={() => <ItemsSkeleton rank={rank} startRank={items.length} />}
|
|
|
|
/>
|
2021-06-22 17:47:49 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-24 23:58:58 +00:00
|
|
|
function ItemsSkeleton ({ rank, startRank = 0 }) {
|
2021-06-22 17:47:49 +00:00
|
|
|
const items = new Array(21).fill(null)
|
2021-04-22 22:14:32 +00:00
|
|
|
|
2021-04-14 23:56:29 +00:00
|
|
|
return (
|
|
|
|
<div className={styles.grid}>
|
2021-06-22 17:47:49 +00:00
|
|
|
{items.map((_, i) => (
|
2021-06-24 23:58:58 +00:00
|
|
|
<ItemSkeleton rank={rank && i + startRank + 1} key={i + startRank} />
|
2021-04-14 23:56:29 +00:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|