set perPage according to screen width

This commit is contained in:
Pig Fang 2020-07-04 17:45:55 +08:00
parent 9856157cec
commit e786fff10d
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect, useRef } from 'react'
import { hot } from 'react-hot-loader/root' import { hot } from 'react-hot-loader/root'
import debounce from 'lodash.debounce' import debounce from 'lodash.debounce'
import useEmitMounted from '@/scripts/hooks/useEmitMounted' import useEmitMounted from '@/scripts/hooks/useEmitMounted'
@ -40,15 +40,28 @@ const Closet: React.FC = () => {
const [skin, setSkin] = useState<Texture | null>(null) const [skin, setSkin] = useState<Texture | null>(null)
const [cape, setCape] = useState<Texture | null>(null) const [cape, setCape] = useState<Texture | null>(null)
const [showModalApply, setShowModalApply] = useState(false) const [showModalApply, setShowModalApply] = useState(false)
const containerRef = useRef<HTMLDivElement | null>(null)
const perPageRef = useRef(6)
useEmitMounted() useEmitMounted()
useEffect(() => {
const element = containerRef.current
/* istanbul ignore next */
if (element) {
const { width } = element.getBoundingClientRect()
if (width >= 500) {
perPageRef.current = Math.floor(width / 235) * 2
}
}
}, [])
useEffect(() => { useEffect(() => {
const getItems = async () => { const getItems = async () => {
setIsLoading(true) setIsLoading(true)
const { data, last_page } = await fetch.get<Paginator<Item>>( const { data, last_page } = await fetch.get<Paginator<Item>>(
urls.user.closet.list(), urls.user.closet.list(),
{ category, q: query, page }, { category, q: query, page, perPage: perPageRef.current },
) )
setItems(data) setItems(data)
@ -142,7 +155,7 @@ const Closet: React.FC = () => {
return ( return (
<> <>
<div className="card card-primary card-tabs"> <div className="card card-primary card-tabs" ref={containerRef}>
<div className="card-header p-0 pt-1 pl-1"> <div className="card-header p-0 pt-1 pl-1">
<div className="d-flex justify-content-between"> <div className="d-flex justify-content-between">
<ul className="nav nav-tabs" role="tablist"> <ul className="nav nav-tabs" role="tablist">
@ -194,7 +207,7 @@ const Closet: React.FC = () => {
<div className="card-body"> <div className="card-body">
{isLoading ? ( {isLoading ? (
<div className="d-flex flex-wrap"> <div className="d-flex flex-wrap">
{new Array(6).fill(null).map((_, i) => ( {new Array(perPageRef.current).fill(null).map((_, i) => (
<LoadingClosetItem key={i} /> <LoadingClosetItem key={i} />
))} ))}
</div> </div>