Use query instead of stae for filter
This commit is contained in:
@@ -1,35 +1,32 @@
|
|||||||
import { Button } from '@theme-ui/components'
|
import React from 'react'
|
||||||
import React, { useCallback } from 'react'
|
import PageLink from './PageLink'
|
||||||
import SquareImageBox from './SquareImageBox'
|
import SquareImageBox from './SquareImageBox'
|
||||||
|
|
||||||
interface FilterButtonProps {
|
interface FilterButtonProps {
|
||||||
active?: boolean
|
active?: boolean
|
||||||
icon?: string
|
icon?: string
|
||||||
label: string
|
label: string
|
||||||
setFilter: (filter: string) => void
|
|
||||||
value: string
|
value: string
|
||||||
m?: string | number
|
m?: string | number
|
||||||
}
|
}
|
||||||
|
|
||||||
const FilterButton = ({
|
const FilterButton = ({
|
||||||
active = false,
|
active = false,
|
||||||
setFilter,
|
|
||||||
icon,
|
icon,
|
||||||
label,
|
label,
|
||||||
value,
|
value,
|
||||||
m,
|
m,
|
||||||
}: FilterButtonProps) => {
|
}: FilterButtonProps) => {
|
||||||
const selectFilter = useCallback(() => {
|
|
||||||
setFilter(value)
|
|
||||||
}, [setFilter, value])
|
|
||||||
return (
|
return (
|
||||||
<Button
|
<PageLink
|
||||||
|
href={{ query: { filter: value } }}
|
||||||
|
shallow={true}
|
||||||
m={m}
|
m={m}
|
||||||
py={1}
|
py={1}
|
||||||
px={2}
|
px={2}
|
||||||
onClick={selectFilter}
|
|
||||||
className={active ? 'active' : ''}
|
className={active ? 'active' : ''}
|
||||||
sx={{ display: 'flex' }}
|
sx={{ display: 'flex' }}
|
||||||
|
variant='buttons.primary'
|
||||||
>
|
>
|
||||||
{icon != null && (
|
{icon != null && (
|
||||||
<SquareImageBox
|
<SquareImageBox
|
||||||
@@ -40,7 +37,7 @@ const FilterButton = ({
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{label}
|
{label}
|
||||||
</Button>
|
</PageLink>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,11 +2,12 @@
|
|||||||
import NextLink, { LinkProps as NextLinkProps } from 'next/link'
|
import NextLink, { LinkProps as NextLinkProps } from 'next/link'
|
||||||
import { Link, LinkProps } from '@theme-ui/components'
|
import { Link, LinkProps } from '@theme-ui/components'
|
||||||
|
|
||||||
type PageLinkProps = LinkProps & NextLinkProps
|
type PageLinkProps = Omit<LinkProps, 'href'> &
|
||||||
|
Pick<NextLinkProps, 'href' | 'shallow'>
|
||||||
|
|
||||||
const PageLink = ({ href, ...otherProps }: PageLinkProps) => {
|
const PageLink = ({ href, shallow, ...otherProps }: PageLinkProps) => {
|
||||||
return (
|
return (
|
||||||
<NextLink href={href} passHref={true}>
|
<NextLink href={href} shallow={shallow} passHref={true}>
|
||||||
<Link {...otherProps} />
|
<Link {...otherProps} />
|
||||||
</NextLink>
|
</NextLink>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ export const theme: Theme = {
|
|||||||
borderWidth: 1,
|
borderWidth: 1,
|
||||||
bg: 'background',
|
bg: 'background',
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
|
textDecoration: 'none',
|
||||||
|
borderRadius: 'default',
|
||||||
'&:hover, &.active': {
|
'&:hover, &.active': {
|
||||||
color: 'background',
|
color: 'background',
|
||||||
bg: 'primary',
|
bg: 'primary',
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Box, Heading, Flex } from '@theme-ui/components'
|
import { Box, Heading, Flex } from '@theme-ui/components'
|
||||||
import { pick } from 'ramda'
|
import { pick } from 'ramda'
|
||||||
import { useMemo, useState } from 'react'
|
import { useMemo } from 'react'
|
||||||
import FilterButton from '../../../components/atoms/FilterButton'
|
import FilterButton from '../../../components/atoms/FilterButton'
|
||||||
import Breadcrumb from '../../../components/organisms/Breadcrumb'
|
import Breadcrumb from '../../../components/organisms/Breadcrumb'
|
||||||
import Honkai3rdNavigator from '../../../components/organisms/Honkai3rdNavigator'
|
import Honkai3rdNavigator from '../../../components/organisms/Honkai3rdNavigator'
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
} from '../../../server/data/honkai3rd/battlesuits'
|
} from '../../../server/data/honkai3rd/battlesuits'
|
||||||
import { battlesuitStrengths } from '../../../lib/safeData'
|
import { battlesuitStrengths } from '../../../lib/safeData'
|
||||||
import BattlesuitCard from '../../../components/molecules/BattlesuitCard'
|
import BattlesuitCard from '../../../components/molecules/BattlesuitCard'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
|
||||||
type BattlesuitListItemData = Pick<
|
type BattlesuitListItemData = Pick<
|
||||||
BattlesuitData,
|
BattlesuitData,
|
||||||
@@ -50,7 +51,15 @@ const valkyrieFilterOptions = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
const BattlesuitListPage = ({ battlesuits }: BattlesuitListPageProps) => {
|
const BattlesuitListPage = ({ battlesuits }: BattlesuitListPageProps) => {
|
||||||
const [filter, setFilter] = useState('all')
|
const { query } = useRouter()
|
||||||
|
|
||||||
|
const filter = useMemo(() => {
|
||||||
|
if (query.filter == null) {
|
||||||
|
return 'all'
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeof query.filter === 'string' ? query.filter : query.filter[0]
|
||||||
|
}, [query])
|
||||||
|
|
||||||
const battlesuitList = useMemo(() => {
|
const battlesuitList = useMemo(() => {
|
||||||
return battlesuits.map((battlesuit) => {
|
return battlesuits.map((battlesuit) => {
|
||||||
@@ -87,7 +96,6 @@ const BattlesuitListPage = ({ battlesuits }: BattlesuitListPageProps) => {
|
|||||||
<FilterButton
|
<FilterButton
|
||||||
key={value}
|
key={value}
|
||||||
active={value === filter}
|
active={value === filter}
|
||||||
setFilter={setFilter}
|
|
||||||
value={value}
|
value={value}
|
||||||
label={label}
|
label={label}
|
||||||
icon={icon}
|
icon={icon}
|
||||||
@@ -104,7 +112,6 @@ const BattlesuitListPage = ({ battlesuits }: BattlesuitListPageProps) => {
|
|||||||
<FilterButton
|
<FilterButton
|
||||||
key={value}
|
key={value}
|
||||||
active={value === filter}
|
active={value === filter}
|
||||||
setFilter={setFilter}
|
|
||||||
value={value}
|
value={value}
|
||||||
label={label}
|
label={label}
|
||||||
icon={icon}
|
icon={icon}
|
||||||
|
|||||||
Reference in New Issue
Block a user