Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Update head method from data fetchers and shift retrieval of invoice count to RQ

Joshen Limdaf33cfe741a
diff --git a/studio/components/interfaces/BillingV2/Invoices.tsx b/studio/components/interfaces/BillingV2/Invoices.tsx
index 05744355b2e..34faf9f2b1f 100644
--- a/studio/components/interfaces/BillingV2/Invoices.tsx
+++ b/studio/components/interfaces/BillingV2/Invoices.tsx
@@ -1,14 +1,15 @@
import { useParams } from 'common'
import Link from 'next/link'
-import { useEffect, useState } from 'react'
-import { Button, IconChevronLeft, IconChevronRight, IconDownload, IconFileText, Loading } from 'ui'
+import { useState } from 'react'
+import { Button, IconChevronLeft, IconChevronRight, IconDownload, IconFileText } from 'ui'
import Table from 'components/to-be-cleaned/Table'
+import AlertError from 'components/ui/AlertError'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
import { getProjectInvoice } from 'data/invoices/project-invoice-query'
+import { useProjectInvoicesCountQuery } from 'data/invoices/project-invoices-count-query'
import { useProjectInvoicesQuery } from 'data/invoices/project-invoices-query'
import { useStore } from 'hooks'
-import { head } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
import InvoiceStatusBadge from './InvoiceStatusBadge'
import { InvoiceStatus } from './Invoices.types'
@@ -17,42 +18,17 @@ const PAGE_LIMIT = 10
const Invoices = () => {
const { ui } = useStore()
const { ref: projectRef } = useParams()
-
const [page, setPage] = useState(1)
- const [count, setCount] = useState(0)
const offset = (page - 1) * PAGE_LIMIT
- const { data, isLoading } = useProjectInvoicesQuery({ projectRef, limit: PAGE_LIMIT, offset })
+ const { data: count, isError: isErrorCount } = useProjectInvoicesCountQuery({ projectRef })
+ const { data, error, isLoading, isError, isSuccess } = useProjectInvoicesQuery({
+ projectRef,
+ limit: PAGE_LIMIT,
+ offset,
+ })
const invoices = data || []
- // [JOSHEN TODO] How to extract the X-Total-Count?
- // const { data: invoicesCount, error } = useProjectInvoicesCountQuery({ projectRef })
- // console.log({ invoicesCount, error })
-
- useEffect(() => {
- if (!projectRef) return
-
- setPage(1)
- let cancel = false
-
- const fetchInvoiceCount = async () => {
- const res = await head(`${API_URL}/projects/${projectRef}/invoices`, ['X-Total-Count'])
- if (!cancel) {
- if (res.error) {
- ui.setNotification({ category: 'error', message: res.error.message })
- } else {
- setCount(res['X-Total-Count'])
- }
- }
- }
-
- fetchInvoiceCount()
-
- return () => {
- cancel = true
- }
- }, [projectRef])
-
const fetchInvoice = async (id: string) => {
try {
const invoice = await getProjectInvoice({ id })
@@ -67,7 +43,11 @@ const Invoices = () => {
return (
<div className="container my-4 max-w-4xl space-y-1">
- <Loading active={isLoading}>
+ {isLoading && <GenericSkeletonLoader />}
+
+ {isError && <AlertError error={error} subject="Failed to retrieve project invoices" />}
+
+ {isSuccess && (
<Table
head={[
<Table.th key="header-icon"></Table.th>,
@@ -136,7 +116,11 @@ const Invoices = () => {
<Table.td colSpan={6}>
<div className="flex items-center justify-between">
<p className="text-sm opacity-50">
- Showing {offset + 1} to {offset + invoices.length} out of {count} invoices
+ {isErrorCount
+ ? 'Failed to retrieve total number of invoices'
+ : `Showing ${offset + 1} to ${
+ offset + invoices.length
+ } out of ${count} invoices`}
</p>
<div className="flex items-center space-x-2">
<Button
@@ -150,7 +134,7 @@ const Invoices = () => {
icon={<IconChevronRight />}
type="default"
size="tiny"
- disabled={page * PAGE_LIMIT >= count}
+ disabled={page * PAGE_LIMIT >= (count ?? 0)}
onClick={async () => setPage(page + 1)}
/>
</div>
@@ -161,7 +145,7 @@ const Invoices = () => {
)
}
/>
- </Loading>
+ )}
</div>
)
}
diff --git a/studio/data/fetchers.ts b/studio/data/fetchers.ts
index 61eff3ac84e..1f4a3117460 100644
--- a/studio/data/fetchers.ts
+++ b/studio/data/fetchers.ts
@@ -100,9 +100,11 @@ export const del: typeof _del = async (url, init) => {
export const head: typeof _head = async (url, init) => {
const headers = await constructHeaders(init?.headers)
+ // @ts-ignore
return await _head(url, {
...init,
headers,
+ parseAs: 'text',
})
}
diff --git a/studio/data/invoices/project-invoices-count-query.ts b/studio/data/invoices/project-invoices-count-query.ts
index 4f405d248f1..a3a165816de 100644
--- a/studio/data/invoices/project-invoices-count-query.ts
+++ b/studio/data/invoices/project-invoices-count-query.ts
@@ -13,15 +13,13 @@ export async function getProjectInvoicesCount(
) {
if (!projectRef) throw new Error('Project ref is required')
- const { data, error } = await head(`/platform/projects/{ref}/invoices`, {
+ const res = await head(`/platform/projects/{ref}/invoices`, {
params: { path: { ref: projectRef } },
signal,
})
- // Need to check with Alaister how to extract the header
- console.log({ data })
- if (error) throw error
- return data
+ if (res.error) throw (res as any).error
+ return Number(res.response.headers.get('X-Total-Count'))
}
export type ProjectInvoicesCountData = Awaited<ReturnType<typeof getProjectInvoicesCount>>