Skip to content

Commit

Permalink
Add sortby handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kanatagu committed Dec 4, 2023
1 parent 042fb42 commit b2f4934
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
23 changes: 12 additions & 11 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ import { PrimaryButton } from '@/components/button'
import { Loading } from '@/components/loading'
import { Header, Footer } from '@/components/navigation'
import { TripSearch, TripSort, TripCard } from '@/trip/components'
import { OrderByDirection, useTripsCollectionQuery } from '@generated/api'
import { useTripsCollectionQuery, TripsOrderBy } from '@generated/api'

export default function Top({
searchParams
}: {
searchParams: { offset: string }
}) {
export default function Top({ searchParams }: { searchParams: { q: string } }) {
const router = useRouter()
const bg = useColorModeValue('white', 'gray.800')
const color = useColorModeValue('black', 'gray.300')

const { data, loading, fetchMore } = useTripsCollectionQuery({
const { data, loading, fetchMore, refetch } = useTripsCollectionQuery({
variables: {
user_id: 1,
orderBy: [{ date_from: OrderByDirection.DescNullsFirst }],
first: 6,
first: 2,
after: null
}
})

const handleSortBy = (sortObj: TripsOrderBy) => {
refetch({
orderBy: sortObj
})
}

const handleLoadMore = () => {
fetchMore({
variables: {
Expand All @@ -55,7 +56,7 @@ export default function Top({
}
})
}

console.log('searchParams', searchParams)
console.log('data', data)

return (
Expand Down Expand Up @@ -93,7 +94,7 @@ export default function Top({
<TripSearch />
</GridItem>
<GridItem>
<TripSort />
<TripSort sortBy={handleSortBy} />
</GridItem>
</Grid>
{loading || !data?.tripsCollection ? (
Expand Down
25 changes: 21 additions & 4 deletions app/trip/components/trip-sort.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Menu, MenuButton, MenuList, MenuItem, Flex } from '@chakra-ui/react'
import { MdOutlineSort, MdArrowDropDown } from 'react-icons/md'
import { useIsMobile } from '@/hooks'
import { OrderByDirection, TripsOrderBy } from '@generated/api'

export const TripSort = () => {
type TripSortProps = {
sortBy: (arg: TripsOrderBy) => void
}
export const TripSort = ({ sortBy }: TripSortProps) => {
const { isMobile } = useIsMobile()

return (
Expand Down Expand Up @@ -38,9 +42,22 @@ export const TripSort = () => {
</MenuButton>

<MenuList>
<MenuItem>Date</MenuItem>
<MenuItem>Created</MenuItem>
<MenuItem>Title</MenuItem>
<MenuItem
onClick={() => sortBy({ date_from: OrderByDirection.DescNullsLast })}
>
Date From
</MenuItem>

<MenuItem
onClick={() => sortBy({ id: OrderByDirection.DescNullsLast })}
>
Created Date
</MenuItem>
<MenuItem
onClick={() => sortBy({ title: OrderByDirection.AscNullsLast })}
>
Title
</MenuItem>
</MenuList>
</Menu>
)
Expand Down

0 comments on commit b2f4934

Please sign in to comment.