-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handling refetch trips collection
- Loading branch information
Showing
7 changed files
with
152 additions
and
102 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { NetworkStatus } from '@apollo/client' | ||
import { useUserId } from '@/providers/session-provider' | ||
import { useTripsCollectionQuery, TripsOrderBy } from '@generated/api' | ||
|
||
export const useTripsGet = (searchWord?: string) => { | ||
const userId = useUserId() | ||
|
||
const { data, loading, error, fetchMore, refetch, networkStatus } = | ||
useTripsCollectionQuery({ | ||
variables: { | ||
filter: { | ||
user_id: { eq: userId }, | ||
...(searchWord && | ||
searchWord.length && { title: { like: `%${searchWord}%` } }) | ||
}, | ||
first: 12, | ||
after: null | ||
} | ||
}) | ||
|
||
const refetchLoading = networkStatus === NetworkStatus.refetch | ||
|
||
const tripsRefetch = () => { | ||
refetch({ | ||
filter: { | ||
user_id: { eq: userId }, | ||
...(searchWord && | ||
searchWord.length && { title: { like: `%${searchWord}%` } }) | ||
}, | ||
first: 12, | ||
after: null | ||
}) | ||
} | ||
|
||
const handleSortBy = (sortObj: TripsOrderBy) => { | ||
refetch({ | ||
orderBy: sortObj | ||
}) | ||
} | ||
|
||
const handleLoadMore = (endCursor: string | null | undefined) => { | ||
fetchMore({ | ||
variables: { | ||
after: endCursor | ||
}, | ||
updateQuery: (previousQueryResult, { fetchMoreResult }) => { | ||
const newEdges = fetchMoreResult?.tripsCollection?.edges | ||
const pageInfo = fetchMoreResult?.tripsCollection?.pageInfo | ||
const previousCollection = previousQueryResult?.tripsCollection | ||
return newEdges && newEdges.length && pageInfo && previousCollection | ||
? { | ||
tripsCollection: { | ||
__typename: previousCollection.__typename, | ||
edges: [...previousCollection.edges, ...newEdges], | ||
pageInfo | ||
}, | ||
__typename: previousQueryResult.__typename | ||
} | ||
: previousQueryResult | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
tripsData: data?.tripsCollection, | ||
loading, | ||
error, | ||
handleSortBy, | ||
handleLoadMore, | ||
tripsRefetch, | ||
refetchLoading | ||
} | ||
} |