-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(per-page): set and load from preferences
- Loading branch information
Showing
3 changed files
with
107 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,93 @@ | ||
import React from 'react'; | ||
import React, { useEffect, useState, useCallback } from 'react'; | ||
import qs from 'qs'; | ||
|
||
import './index.scss'; | ||
import { Link } from 'react-router-dom'; | ||
import { useConfig } from '@payloadcms/config-provider'; | ||
import { usePreferences } from '../../utilities/Preferences'; | ||
import { useSearchParams } from '../../utilities/SearchParams'; | ||
import Popup from '../Popup'; | ||
import Chevron from '../../icons/Chevron'; | ||
|
||
import './index.scss'; | ||
|
||
const baseClass = 'per-page'; | ||
type Props = { | ||
valueOptions: number[]; | ||
collectionSlug: string; | ||
} | ||
|
||
const PerPage: React.FC<Props> = ({ valueOptions }) => ( | ||
<div className={baseClass}> | ||
<Popup | ||
horizontalAlign="center" | ||
button={( | ||
<div> | ||
Per Page: | ||
<Chevron /> | ||
</div> | ||
)} | ||
backgroundColor="#333333" | ||
render={({ close }) => ( | ||
<div> | ||
<ul> | ||
{valueOptions.map((option, i) => ( | ||
<li | ||
className={`${baseClass}-item`} | ||
key={i} | ||
> | ||
<Link | ||
to="/asdf" | ||
onClick={close} | ||
> | ||
{option} | ||
</Link> | ||
|
||
</li> | ||
))} | ||
|
||
</ul> | ||
</div> | ||
)} | ||
/> | ||
</div> | ||
); | ||
const PerPage: React.FC<Props> = ({ collectionSlug }) => { | ||
const preferencesKey = `${collectionSlug}-per-page`; | ||
const { admin: { pagination: { default: defaultPagination, options } } } = useConfig(); | ||
const { getPreference, setPreference } = usePreferences(); | ||
const [, setPerPage] = useState(defaultPagination); | ||
const searchParams = useSearchParams(); | ||
|
||
const updatePerPage = useCallback((perPage) => { | ||
setPerPage(perPage); | ||
setPreference(preferencesKey, perPage); | ||
}, [setPerPage, setPreference, preferencesKey]); | ||
|
||
useEffect(() => { | ||
const asyncGetPreference = async () => { | ||
const perPageFromPreferences = await getPreference<number>(preferencesKey) || defaultPagination; | ||
setPerPage(perPageFromPreferences); | ||
}; | ||
|
||
asyncGetPreference(); | ||
}, [defaultPagination, preferencesKey, getPreference]); | ||
|
||
const closeAndSet = ({ close, option }) => { | ||
console.log(`Setting option: ${option}`); | ||
updatePerPage(option); | ||
close(); | ||
}; | ||
|
||
return ( | ||
<div className={baseClass}> | ||
<Popup | ||
horizontalAlign="center" | ||
button={( | ||
<div> | ||
Per Page: | ||
<Chevron /> | ||
</div> | ||
)} | ||
backgroundColor="#333333" | ||
render={({ close }) => ( | ||
<div> | ||
<ul> | ||
{options.map((option, i) => { | ||
const newParams = { | ||
...searchParams, | ||
limit: option, | ||
}; | ||
|
||
const search = qs.stringify(newParams); | ||
const linkPath = `${collectionSlug}?${search}`; | ||
|
||
return ( | ||
<li | ||
className={`${baseClass}-item`} | ||
key={i} | ||
> | ||
<Link | ||
to={linkPath} | ||
onClick={() => closeAndSet({ close, option })} | ||
> | ||
{option} | ||
</Link> | ||
|
||
</li> | ||
); | ||
})} | ||
; | ||
|
||
</ul> | ||
</div> | ||
)} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default PerPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters