fix skin library history state

This commit is contained in:
Pig Fang 2020-03-27 12:24:11 +08:00
parent 6bd7e4744f
commit 8f2f57ffc8
2 changed files with 8 additions and 10 deletions

View File

@ -28,9 +28,8 @@ const SkinLibrary: React.FC = () => {
const currentUid = useBlessingExtra<number | null>('currentUid', null)
useEffect(() => {
const parseSearch = (query: string | URLSearchParams) => {
const search =
typeof query === 'string' ? new URLSearchParams(query) : query
const parseSearch = (query: string) => {
const search = new URLSearchParams(query)
const filter = search.get('filter') ?? ''
setFilter(
@ -51,9 +50,7 @@ const SkinLibrary: React.FC = () => {
parseSearch(location.search)
const handler = (event: PopStateEvent) => {
parseSearch(event.state as URLSearchParams)
}
const handler = (event: PopStateEvent) => parseSearch(event.state)
window.addEventListener('popstate', handler)
return () => {
@ -75,7 +72,7 @@ const SkinLibrary: React.FC = () => {
}
search.append('sort', sort)
search.append('page', page.toString())
window.history.pushState(search, '', `?${search}`)
window.history.pushState(search.toString(), '', `?${search}`)
const result = await fetch.get<Paginator<LibraryItem>>(
'/skinlib/list',

View File

@ -139,9 +139,10 @@ test('browser goes back', async () => {
fireEvent.click(getByText('Steve'))
await wait()
const state: URLSearchParams = window.history.state
state.set('filter', 'skin')
const event = new PopStateEvent('popstate', { state })
const state: string = window.history.state
const event = new PopStateEvent('popstate', {
state: state.replace('steve', 'skin'),
})
window.dispatchEvent(event)
await wait()