Skip to content

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

midway converting invoices to RQ

Joshen Limb3cb658c114a
diff --git a/studio/components/interfaces/BillingV2/Invoices.tsx b/studio/components/interfaces/BillingV2/Invoices.tsx
index 38a7737a1fa..05744355b2e 100644
--- a/studio/components/interfaces/BillingV2/Invoices.tsx
+++ b/studio/components/interfaces/BillingV2/Invoices.tsx
@@ -1,35 +1,39 @@
+import { useParams } from 'common'
import Link from 'next/link'
import { useEffect, useState } from 'react'
import { Button, IconChevronLeft, IconChevronRight, IconDownload, IconFileText, Loading } from 'ui'
import Table from 'components/to-be-cleaned/Table'
+import { getProjectInvoice } from 'data/invoices/project-invoice-query'
+import { useProjectInvoicesQuery } from 'data/invoices/project-invoices-query'
import { useStore } from 'hooks'
-import { get, head } from 'lib/common/fetch'
+import { head } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import InvoiceStatusBadge from './InvoiceStatusBadge'
-import { Invoice, InvoiceStatus } from './Invoices.types'
+import { InvoiceStatus } from './Invoices.types'
const PAGE_LIMIT = 10
-interface InvoicesProps {
- projectRef: string
-}
-
-const Invoices = ({ projectRef }: InvoicesProps) => {
+const Invoices = () => {
const { ui } = useStore()
- const [loading, setLoading] = useState<any>(false)
+ const { ref: projectRef } = useParams()
const [page, setPage] = useState(1)
const [count, setCount] = useState(0)
- const [invoices, setInvoices] = useState<Invoice[]>([])
const offset = (page - 1) * PAGE_LIMIT
+ const { data, isLoading } = 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 page = 1
const fetchInvoiceCount = async () => {
const res = await head(`${API_URL}/projects/${projectRef}/invoices`, ['X-Total-Count'])
@@ -42,8 +46,6 @@ const Invoices = ({ projectRef }: InvoicesProps) => {
}
}
- setPage(page)
- fetchInvoices(page)
fetchInvoiceCount()
return () => {
@@ -51,42 +53,21 @@ const Invoices = ({ projectRef }: InvoicesProps) => {
}
}, [projectRef])
- const fetchInvoices = async (page: number) => {
- setLoading(true)
- setPage(page)
-
- const offset = (page - 1) * PAGE_LIMIT
- const invoices = await get(
- `${API_URL}/projects/${projectRef}/invoices?offset=${offset}&limit=${PAGE_LIMIT}`
- )
-
- if (invoices.error) {
- ui.setNotification({
- category: 'error',
- message: `Failed to fetch invoices: ${invoices.error.message}`,
- })
- } else {
- setInvoices(invoices)
- }
-
- setLoading(false)
- }
-
- const fetchInvoice = async (invoiceId: string) => {
- const invoice = await get(`${API_URL}/stripe/invoices/${invoiceId}`)
- if (invoice?.invoice_pdf) {
- window.open(invoice.invoice_pdf, '_blank')
- } else {
+ const fetchInvoice = async (id: string) => {
+ try {
+ const invoice = await getProjectInvoice({ id })
+ if (invoice?.invoice_pdf) window.open(invoice.invoice_pdf, '_blank')
+ } catch (error: any) {
ui.setNotification({
category: 'info',
- message: `Failed to fetch the selected invoice: ${invoice.error.message}`,
+ message: `Failed to fetch the selected invoice: ${error.message}`,
})
}
}
return (
<div className="container my-4 max-w-4xl space-y-1">
- <Loading active={loading}>
+ <Loading active={isLoading}>
<Table
head={[
<Table.th key="header-icon"></Table.th>,
@@ -103,7 +84,7 @@ const Invoices = ({ projectRef }: InvoicesProps) => {
<Table.tr>
<Table.td colSpan={6} className="p-3 py-12 text-center">
<p className="text-scale-1000">
- {loading ? 'Checking for invoices' : 'No invoices for this project yet'}
+ {isLoading ? 'Checking for invoices' : 'No invoices for this project yet'}
</p>
</Table.td>
</Table.tr>
@@ -163,14 +144,14 @@ const Invoices = ({ projectRef }: InvoicesProps) => {
type="default"
size="tiny"
disabled={page === 1}
- onClick={async () => await fetchInvoices(page - 1)}
+ onClick={async () => setPage(page - 1)}
/>
<Button
icon={<IconChevronRight />}
type="default"
size="tiny"
disabled={page * PAGE_LIMIT >= count}
- onClick={async () => await fetchInvoices(page + 1)}
+ onClick={async () => setPage(page + 1)}
/>
</div>
</div>
diff --git a/studio/data/invoices/keys.ts b/studio/data/invoices/keys.ts
index f2ba9be09a5..b515f9788aa 100644
--- a/studio/data/invoices/keys.ts
+++ b/studio/data/invoices/keys.ts
@@ -1,5 +1,9 @@
export const invoicesKeys = {
overdueInvoices: () => ['invoices', 'overdue'] as const,
+ invoice: (id: string | undefined) => ['invoices', id] as const,
+ projectInvoices: (projectRef: string | undefined) => ['invoices', projectRef] as const,
+ projectInvoicesCount: (projectRef: string | undefined) =>
+ ['invoices', projectRef, 'count'] as const,
upcomingPreview: (projectRef: string | undefined) =>
['invoices', projectRef, 'upcoming-preview'] as const,
orgUpcomingPreview: (projectRef: string | undefined) =>
diff --git a/studio/data/invoices/project-invoice-query.ts b/studio/data/invoices/project-invoice-query.ts
new file mode 100644
index 00000000000..157dda1176c
--- /dev/null
+++ b/studio/data/invoices/project-invoice-query.ts
@@ -0,0 +1,39 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { invoicesKeys } from './keys'
+
+export type ProjectInvoiceVariables = {
+ id?: string
+}
+
+export async function getProjectInvoice({ id }: ProjectInvoiceVariables, signal?: AbortSignal) {
+ if (!id) throw new Error('Invoice ID is required')
+
+ const { data, error } = await get(`/platform/stripe/invoices/{id}`, {
+ params: { path: { id } },
+ signal,
+ })
+
+ if (error) throw error
+ return data
+}
+
+export type ProjectInvoiceData = Awaited<ReturnType<typeof getProjectInvoice>>
+export type ProjectInvoiceError = ResponseError
+
+export const useProjectInvoiceQuery = <TData = ProjectInvoiceData>(
+ { id }: ProjectInvoiceVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<ProjectInvoiceData, ProjectInvoiceError, TData> = {}
+) =>
+ useQuery<ProjectInvoiceData, ProjectInvoiceError, TData>(
+ invoicesKeys.invoice(id),
+ ({ signal }) => getProjectInvoice({ id }, signal),
+ {
+ enabled: enabled && typeof id !== 'undefined',
+ ...options,
+ }
+ )
diff --git a/studio/data/invoices/project-invoices-count-query.ts b/studio/data/invoices/project-invoices-count-query.ts
new file mode 100644
index 00000000000..4f405d248f1
--- /dev/null
+++ b/studio/data/invoices/project-invoices-count-query.ts
@@ -0,0 +1,44 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { head } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { invoicesKeys } from './keys'
+
+export type ProjectInvoicesCountVariables = {
+ projectRef?: string
+}
+
+export async function getProjectInvoicesCount(
+ { projectRef }: ProjectInvoicesCountVariables,
+ signal?: AbortSignal
+) {
+ if (!projectRef) throw new Error('Project ref is required')
+
+ const { data, error } = 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
+}
+
+export type ProjectInvoicesCountData = Awaited<ReturnType<typeof getProjectInvoicesCount>>
+export type ProjectInvoicesCountError = ResponseError
+
+export const useProjectInvoicesCountQuery = <TData = ProjectInvoicesCountData>(
+ { projectRef }: ProjectInvoicesCountVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<ProjectInvoicesCountData, ProjectInvoicesCountError, TData> = {}
+) =>
+ useQuery<ProjectInvoicesCountData, ProjectInvoicesCountError, TData>(
+ invoicesKeys.projectInvoicesCount(projectRef),
+ ({ signal }) => getProjectInvoicesCount({ projectRef }, signal),
+ {
+ enabled: enabled && typeof projectRef !== 'undefined',
+ ...options,
+ }
+ )
diff --git a/studio/data/invoices/project-invoices-query.ts b/studio/data/invoices/project-invoices-query.ts
new file mode 100644
index 00000000000..fe697397be5
--- /dev/null
+++ b/studio/data/invoices/project-invoices-query.ts
@@ -0,0 +1,51 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { get } from 'data/fetchers'
+import { ResponseError } from 'types'
+import { invoicesKeys } from './keys'
+
+export type ProjectInvoicesVariables = {
+ projectRef?: string
+ offset: number
+ limit: number
+}
+
+export async function getProjectInvoices(
+ { projectRef, offset, limit }: ProjectInvoicesVariables,
+ signal?: AbortSignal
+) {
+ if (!projectRef) throw new Error('Project ref is required')
+
+ const { data, error } = await get(`/platform/projects/{ref}/invoices`, {
+ params: {
+ path: { ref: projectRef },
+ query: { offset: offset.toString(), limit: limit.toString() },
+ },
+ signal,
+ })
+
+ if (error) throw error
+ return data
+}
+
+export type ProjectInvoicesData = Awaited<ReturnType<typeof getProjectInvoices>>
+export type ProjectInvoicesError = ResponseError
+
+export const useProjectInvoicesQuery = <TData = ProjectInvoicesData>(
+ { projectRef, offset, limit }: ProjectInvoicesVariables,
+ {
+ enabled = true,
+ ...options
+ }: UseQueryOptions<ProjectInvoicesData, ProjectInvoicesError, TData> = {}
+) =>
+ useQuery<ProjectInvoicesData, ProjectInvoicesError, TData>(
+ invoicesKeys.projectInvoices(projectRef),
+ ({ signal }) => getProjectInvoices({ projectRef, offset, limit }, signal),
+ {
+ enabled:
+ enabled &&
+ typeof projectRef !== 'undefined' &&
+ typeof limit !== 'undefined' &&
+ typeof offset !== 'undefined',
+ ...options,
+ }
+ )
diff --git a/studio/pages/project/[ref]/settings/billing/invoices.tsx b/studio/pages/project/[ref]/settings/billing/invoices.tsx
index e44997f57bc..4d5cbbbb70a 100644
--- a/studio/pages/project/[ref]/settings/billing/invoices.tsx
+++ b/studio/pages/project/[ref]/settings/billing/invoices.tsx
@@ -38,7 +38,6 @@ export default ProjectBilling
const Settings = () => {
const selectedOrganization = useSelectedOrganization()
- const { project: selectedProject } = useProjectContext()
const orgSlug = selectedOrganization?.slug ?? ''
return (
@@ -58,7 +57,7 @@ const Settings = () => {
</Link>
</div>
- <Invoices projectRef={selectedProject?.ref ?? ''} />
+ <Invoices />
</div>
</div>
)