Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace cursor by offset in GetNotifications query #555

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions pages/notification.gql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query GetNotifications($address: Address!, $cursor: Cursor) {
query GetNotifications($address: Address!, $limit: Int!, $offset: Int!) {

Check warning on line 1 in pages/notification.gql

View workflow job for this annotation

GitHub Actions / lint

Operation "GetNotifications" should not have "Get" prefix
notifications(
filter: {
accountAddress: { equalTo: $address }
Expand All @@ -14,15 +14,14 @@
]
}
}
first: 12
first: $limit
orderBy: CREATED_AT_DESC
after: $cursor
offset: $offset
includeWhenCollectionDeleted: YES
includeWhenTradeDeleted: YES
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
id
Expand Down
17 changes: 10 additions & 7 deletions pages/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { FaBell } from '@react-icons/all-files/fa/FaBell'
import { NextPage } from 'next'
import useTranslation from 'next-translate/useTranslation'
import { useCallback, useEffect } from 'react'
import { useCallback, useEffect, useState } from 'react'
import { useCookies } from 'react-cookie'
import Empty from '../components/Empty/Empty'
import Head from '../components/Head'
Expand All @@ -25,20 +25,24 @@ import useLoginRedirect from '../hooks/useLoginRedirect'
import SmallLayout from '../layouts/small'
import { formatError } from '../utils'

const PAGINATION_LIMIT = 12

const NotificationPage: NextPage = ({}) => {
const { t } = useTranslation('templates')
const toast = useToast()
const { address } = useAccount()
useLoginRedirect()
const [_, setCookies] = useCookies()
const [offset, setOffset] = useState(0)

const {
data: notificationData,
fetchMore,
networkStatus,
} = useGetNotificationsQuery({
variables: {
cursor: null,
offset: 0, // the offset change must be done when calling the fetchMore function to concat queries' results
limit: PAGINATION_LIMIT,
address: address || '',
},
notifyOnNetworkStatusChange: true,
Expand All @@ -47,23 +51,22 @@ const NotificationPage: NextPage = ({}) => {

const notifications = notificationData?.notifications?.nodes
const hasNextPage = notificationData?.notifications?.pageInfo.hasNextPage
const endCursor = notificationData?.notifications?.pageInfo.endCursor

const loadMore = useCallback(async () => {
const newOffset = offset + PAGINATION_LIMIT
try {
await fetchMore({
variables: {
cursor: endCursor,
},
variables: { offset: newOffset },
updateQuery: concatToQuery('notifications'),
})
setOffset(newOffset)
} catch (e) {
toast({
title: formatError(e),
status: 'error',
})
}
}, [endCursor, fetchMore, toast])
}, [fetchMore, offset, toast])

useEffect(() => {
if (!address) return
Expand Down
Loading