import {Suspense} from "react"
import {
Link,
BlitzPage,
usePaginatedQuery,
useRouterQuery,
useRouter,
} from "blitz"
import getProjects from "app/products/queries/getProjects"
const ITEMS_PER_PAGE = 3
const Projects = () => {
const router = useRouter()
const {page = 0} = useRouterQuery()
const [{projects, hasMore}] = usePaginatedQuery(getProjects, {
skip: ITEMS_PER_PAGE * Number(page),
take: ITEMS_PER_PAGE,
})
const goToPreviousPage = () =>
router.push({query: {page: Number(page) - 1}})
const goToNextPage = () =>
router.push({query: {page: Number(page) + 1}})
return (
<div>
{projects.map((project) => (
<p key={project.id}>
<Link
href="/projects/[handle]"
as={`/projects/${project.handle}`}
>
<a>{project.name}</a>
</Link>
</p>
))}
<button disabled={page === 0} onClick={goToPreviousPage}>
Previous
</button>
<button disabled={!hasMore} onClick={goToNextPage}>
Next
</button>
</div>
)
}
And here's the query to work with that:
export default async function getProjects(
{where, orderBy, cursor, take, skip}: GetProjectsInput,
ctx: Record<any, any> = {},
) {
const projects = await db.project.findMany({
where,
orderBy,
take,
skip,
})
const count = await db.project.count()
const hasMore = skip! + take! < count
return {
projects,
hasMore,
}
}
const [
queryResult,
{
latestData,
isFetching,
failureCount,
refetch,
}
] = useQuery(queryResolver, queryInputArguments, options)
queryResolver:
A Blitz query resolverqueryInputArguments
queryResolver
options
The options are identical to the options for the useQuery hook
[queryResult, queryExtras]
queryResult: Any
undefined
.queryExtras: Object
latestData: Any
undefined
.undefined
isFetching: Boolean
true
if the query is currently fetching, including
background fetching.failureCount: Integer
0
when the query succeeds.refetch: Function({ force, throwOnError }) => void
force: true
option and
refetch it regardless of it's freshnessthrowOnError: true
optionsetQueryData()
- Function(newData, opts) => void
newData
can be an object of new data or a function that receives the
old data and returns the new datarefetch()
to
ensure the data is correct. Disable refetch by passing an options
object {refetch: false}
as the second argument.setQueryData()