Skip to content

Commit 8920aa8

Browse files
authored
Merge pull request #15 from instalution/add-perPage-to-settings
Add `perPage` to `settings`
2 parents 902fc78 + 18a3c6b commit 8920aa8

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

components/Pagination/index.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { translations as t } from '../../i18n'
22
import { PaginationProps } from '../../types'
33
import { useRouter } from 'next/router'
44

5-
const Pagination = ({ meta, pageState, locale, location }: PaginationProps) => {
5+
const Pagination = ({ meta, pageState, locale, location, settings }: PaginationProps) => {
66
const { page, setPage } = pageState
77
const router = useRouter()
88

99
return <>
10-
{meta.total > meta.perPage &&
10+
{meta.total > settings.perPage &&
1111
<div className="previous">
1212
<button onClick={() => setPage(prevState => prevState - 1)} disabled={page === 1} title={t[locale].previous}>
1313
@@ -19,9 +19,9 @@ const Pagination = ({ meta, pageState, locale, location }: PaginationProps) => {
1919
{ location === 'header' && '↑' }
2020
</button>
2121
</div>
22-
{meta.total > meta.perPage &&
22+
{meta.total > settings.perPage &&
2323
<div className="next">
24-
<button onClick={() => setPage(prevState => prevState + 1)} disabled={page === Math.ceil(meta.total / meta.perPage)} title={t[locale].next}>
24+
<button onClick={() => setPage(prevState => prevState + 1)} disabled={page === Math.ceil(meta.total / settings.perPage)} title={t[locale].next}>
2525
2626
</button>
2727
</div>

pages/[account].tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const Home: NextPage = () => {
2626
autoHeight: false,
2727
sort: 'filename',
2828
renderingMode: 'grid',
29+
perPage: 36,
2930
columnBreakpoints: {
3031
small: 2,
3132
medium: 4,
@@ -35,7 +36,9 @@ const Home: NextPage = () => {
3536
const [ page, setPage ] = useState<number>(1)
3637
const [ media, setMedia ] = useState<AppApiResponse>({
3738
data: [],
38-
meta: {},
39+
meta: {
40+
total: 0,
41+
},
3942
})
4043

4144
const detectBreakpoint = (): Breakpoint => {
@@ -122,7 +125,9 @@ const Home: NextPage = () => {
122125
return
123126
}
124127

125-
fetch('/api/files?account=' + account + '&page=' + page + '&sort=' + settings.sort, {
128+
fetch(
129+
'/api/files?account=' + account + '&page=' + page + '&perPage=' + settings.perPage + '&sort=' + settings.sort,
130+
{
126131
method: 'GET',
127132
headers: { 'Content-type': 'application/json' },
128133
})
@@ -165,7 +170,7 @@ const Home: NextPage = () => {
165170
166171
</button>
167172
</div>
168-
<Pagination meta={media.meta} pageState={{ page, setPage }} locale={locale} location="header" />
173+
<Pagination meta={media.meta} pageState={{ page, setPage }} locale={locale} location="header" settings={settings} />
169174
<div className="about">
170175
<button onClick={() => window.open('https://github.com/instalution/frontend')} title={t[locale].about}>
171176
?
@@ -189,7 +194,7 @@ const Home: NextPage = () => {
189194
))}
190195
</div>
191196
<footer className="app-bar">
192-
<Pagination meta={media.meta} pageState={{ page, setPage }} locale={locale} location="footer" />
197+
<Pagination meta={media.meta} pageState={{ page, setPage }} locale={locale} location="footer" settings={settings} />
193198
</footer>
194199
</main>
195200
<Modal openState={[ settingsModalOpen, setSettingsModalOpen ]}>

pages/api/files.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import type { NextApiRequest, NextApiResponse } from 'next'
22
import useConfig from 'next/config'
33
import { AppApiResponse } from '../../types'
44

5-
// @TODO: Make this configurable
6-
const perPage = 36
7-
85
const glob = require('glob')
96
const fs = require('fs')
107

@@ -19,13 +16,22 @@ export default function Handler(
1916
const {serverRuntimeConfig} = useConfig()
2017
const query = req.query
2118
// `account` should be present because it's defined in the route
22-
const { account, page, sort } = query
19+
const { account, page, perPage, sort } = query
2320
const pageNumber: number = parseInt(page as string)
2421

2522
if (!sort) {
2623
throw new Error('No sort specified')
2724
}
2825

26+
if (!perPage) {
27+
throw new Error('No perPage specified')
28+
}
29+
30+
const perPageInt = parseInt(perPage as string)
31+
if (perPageInt < 1) {
32+
throw new Error('Invalid perPage specified')
33+
}
34+
2935
if (!['random', 'filename'].includes(sort.toString())) {
3036
throw new Error('Unexpected value for sort')
3137
}
@@ -49,8 +55,8 @@ export default function Handler(
4955
new RegExp('^' + escapeRegExp(publicFolder)), '')
5056
}
5157

52-
if (files.length > perPage) {
53-
files = files.slice((pageNumber - 1) * perPage, perPage * pageNumber)
58+
if (files.length > perPageInt) {
59+
files = files.slice((pageNumber - 1) * perPageInt, perPageInt * pageNumber)
5460
}
5561

5662
if (sort === 'random') {
@@ -62,7 +68,6 @@ export default function Handler(
6268
data: files,
6369
meta: {
6470
total: totalFiles,
65-
perPage,
6671
}
6772
})
6873
}

types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export type Breakpoint = 'small' | 'medium' | 'large'
44

55
export interface AppApiResponseMeta {
66
total: number
7-
perPage: number
87
}
98

109
export interface AppApiResponse {
@@ -29,6 +28,7 @@ export interface Settings {
2928
sort: string
3029
columnBreakpoints: {[index in Breakpoint]: number}
3130
renderingMode: 'grid' | 'columns'
31+
perPage: number
3232
}
3333

3434
export interface PageState {
@@ -49,4 +49,5 @@ export interface PaginationProps {
4949
pageState: PageState
5050
locale: string
5151
location: 'header' | 'footer'
52+
settings: Settings
5253
}

0 commit comments

Comments
 (0)