Skip to content

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

Refactor and shift all GET calls in org v1 billing settings to RQ

Joshen Lim28c704fdebb3
diff --git a/studio/components/interfaces/Organization/BillingSettings/BillingAddress/BillingAddress.tsx b/studio/components/interfaces/Organization/BillingSettings/BillingAddress/BillingAddress.tsx
index 510117e4e2c..4a873cf5cac 100644
--- a/studio/components/interfaces/Organization/BillingSettings/BillingAddress/BillingAddress.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/BillingAddress/BillingAddress.tsx
@@ -1,26 +1,33 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
import { isEqual } from 'lodash'
import { useState } from 'react'
import { Button, Form, Input, Listbox } from 'ui'
+import AlertError from 'components/ui/AlertError'
import NoPermission from 'components/ui/NoPermission'
import Panel from 'components/ui/Panel'
-import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import { useOrganizationCustomerProfileQuery } from 'data/organizations/organization-customer-profile-query'
+import { useCheckPermissions, useStore } from 'hooks'
import { patch } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { COUNTRIES } from './BillingAddress.constants'
-export interface BillingAddressProps {
- loading: boolean
- address: { [key: string]: any }
- onAddressUpdated: (address: any) => void
-}
-
-const BillingAddress = ({ loading, address, onAddressUpdated }: BillingAddressProps) => {
+const BillingAddress = () => {
const { ui } = useStore()
- const selectedOrganization = useSelectedOrganization()
- const orgSlug = selectedOrganization?.slug ?? ''
- const { city, country, line1, line2, postal_code, state } = address
+ const { slug } = useParams()
+
+ const {
+ data: customer,
+ error: customerError,
+ refetch: refetchCustomer,
+ isError: isErrorCustomer,
+ isLoading: isLoadingCustomer,
+ isSuccess: isSuccessCustomer,
+ } = useOrganizationCustomerProfileQuery({ slug })
+
+ const { city, country, line1, line2, postal_code, state } = customer?.address ?? {}
const canReadBillingAddress = useCheckPermissions(
PermissionAction.BILLING_READ,
@@ -54,7 +61,7 @@ const BillingAddress = ({ loading, address, onAddressUpdated }: BillingAddressPr
const onSubmit = async (values: any, { setSubmitting }: any) => {
try {
setSubmitting(true)
- const updatedCustomer = await patch(`${API_URL}/organizations/${orgSlug}/customer`, {
+ const updatedCustomer = await patch(`${API_URL}/organizations/${slug}/customer`, {
address: values,
})
if (updatedCustomer.error) throw updatedCustomer.error
@@ -62,7 +69,7 @@ const BillingAddress = ({ loading, address, onAddressUpdated }: BillingAddressPr
category: 'success',
message: 'Successfully updated billing address',
})
- onAddressUpdated(updatedCustomer.address)
+ refetchCustomer()
} catch (error: any) {
ui.setNotification({
category: 'error',
@@ -80,122 +87,125 @@ const BillingAddress = ({ loading, address, onAddressUpdated }: BillingAddressPr
<h4>Billing address</h4>
<p className="text-sm opacity-50">This will be reflected in every invoice</p>
</div>
- <Panel loading={loading}>
- {loading ? (
- <div className="flex flex-col justify-between space-y-2 py-4 px-4">
- <div className="shimmering-loader mx-1 w-2/3 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/2 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/3 rounded py-3" />
- </div>
- ) : !canReadBillingAddress ? (
- <NoPermission resourceText="view this organization's billing address" />
- ) : (
- <Form
- validateOnBlur
- initialValues={initialValues}
- validate={onValidate}
- onSubmit={onSubmit}
- >
- {({ isSubmitting, handleReset }: any) => {
- return (
- <>
- <Panel.Content className="w-3/5 space-y-2">
- <Input
- id="line1"
- name="line1"
- placeholder="Address line 1"
- disabled={!canUpdateBillingAddress}
- />
- <Input
- id="line2"
- name="line2"
- placeholder="Address line 2"
- disabled={!canUpdateBillingAddress}
- />
- <div className="flex items-center space-x-2">
- <Listbox
- className="w-full"
- id="country"
- name="country"
- placeholder="Country"
- disabled={!canUpdateBillingAddress}
- >
- <Listbox.Option label="---" key="empty" value="">
- ---
- </Listbox.Option>
- {COUNTRIES.map((country) => (
- <Listbox.Option
- label={country.name}
- key={country.code}
- value={country.code}
- >
- {country.name}
- </Listbox.Option>
- ))}
- </Listbox>
- <Input
- className="w-full"
- id="postal_code"
- name="postal_code"
- placeholder="Postal code"
- disabled={!canUpdateBillingAddress}
- />
- </div>
- <div className="flex items-center space-x-2">
+
+ {isLoadingCustomer && <GenericSkeletonLoader />}
+
+ {isErrorCustomer && (
+ <AlertError error={customerError} subject="Failed to retrieve customer profile" />
+ )}
+
+ {isSuccessCustomer && (
+ <Panel>
+ {!canReadBillingAddress ? (
+ <NoPermission resourceText="view this organization's billing address" />
+ ) : (
+ <Form
+ validateOnBlur
+ initialValues={initialValues}
+ validate={onValidate}
+ onSubmit={onSubmit}
+ >
+ {({ isSubmitting, handleReset }: any) => {
+ return (
+ <>
+ <Panel.Content className="w-3/5 space-y-2">
<Input
- className="w-full"
- id="city"
- name="city"
- placeholder="City"
+ id="line1"
+ name="line1"
+ placeholder="Address line 1"
disabled={!canUpdateBillingAddress}
/>
<Input
- className="w-full"
- id="state"
- name="state"
- placeholder="State"
+ id="line2"
+ name="line2"
+ placeholder="Address line 2"
disabled={!canUpdateBillingAddress}
/>
- </div>
- </Panel.Content>
- <div className="border-t border-scale-400" />
- <Panel.Content className="flex justify-between">
- {!canUpdateBillingAddress ? (
- <p className="text-sm text-scale-1000">
- You need additional permissions to update this organization's billing
- address
- </p>
- ) : (
- <div />
- )}
- <div className="flex items-center space-x-2">
- <Button
- type="default"
- htmlType="reset"
- disabled={!isDirty || isSubmitting || !canUpdateBillingAddress}
- onClick={() => {
- handleReset()
- setIsDirty(false)
- }}
- >
- Cancel
- </Button>
- <Button
- type="primary"
- htmlType="submit"
- loading={isSubmitting}
- disabled={!isDirty || isSubmitting || !canUpdateBillingAddress}
- >
- Save
- </Button>
- </div>
- </Panel.Content>
- </>
- )
- }}
- </Form>
- )}
- </Panel>
+ <div className="flex items-center space-x-2">
+ <Listbox
+ className="w-full"
+ id="country"
+ name="country"
+ placeholder="Country"
+ disabled={!canUpdateBillingAddress}
+ >
+ <Listbox.Option label="---" key="empty" value="">
+ ---
+ </Listbox.Option>
+ {COUNTRIES.map((country) => (
+ <Listbox.Option
+ label={country.name}
+ key={country.code}
+ value={country.code}
+ >
+ {country.name}
+ </Listbox.Option>
+ ))}
+ </Listbox>
+ <Input
+ className="w-full"
+ id="postal_code"
+ name="postal_code"
+ placeholder="Postal code"
+ disabled={!canUpdateBillingAddress}
+ />
+ </div>
+ <div className="flex items-center space-x-2">
+ <Input
+ className="w-full"
+ id="city"
+ name="city"
+ placeholder="City"
+ disabled={!canUpdateBillingAddress}
+ />
+ <Input
+ className="w-full"
+ id="state"
+ name="state"
+ placeholder="State"
+ disabled={!canUpdateBillingAddress}
+ />
+ </div>
+ </Panel.Content>
+ <div className="border-t border-scale-400" />
+ <Panel.Content className="flex justify-between">
+ {!canUpdateBillingAddress ? (
+ <p className="text-sm text-scale-1000">
+ You need additional permissions to update this organization's billing
+ address
+ </p>
+ ) : (
+ <div />
+ )}
+ <div className="flex items-center space-x-2">
+ <Button
+ type="default"
+ htmlType="reset"
+ disabled={!isDirty || isSubmitting || !canUpdateBillingAddress}
+ onClick={() => {
+ handleReset()
+ setIsDirty(false)
+ }}
+ >
+ Cancel
+ </Button>
+ <Button
+ type="primary"
+ htmlType="submit"
+ loading={isSubmitting}
+ disabled={!isDirty || isSubmitting || !canUpdateBillingAddress}
+ >
+ Save
+ </Button>
+ </div>
+ </Panel.Content>
+ </>
+ )
+ }}
+ </Form>
+ )}
+ </Panel>
+ )}
</div>
)
}
diff --git a/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx b/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
index b453da92d71..5198576d01c 100644
--- a/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/BillingSettings.tsx
@@ -1,12 +1,10 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
import { useParams } from 'common'
-import { useEffect, useState } from 'react'
import { ScaffoldContainerLegacy } from 'components/layouts/Scaffold'
+import { useOrganizationCustomerProfileQuery } from 'data/organizations/organization-customer-profile-query'
import { useProjectsQuery } from 'data/projects/projects-query'
-import { useCheckPermissions, useFlag, useSelectedOrganization, useStore } from 'hooks'
-import { get } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { useCheckPermissions, useFlag, useSelectedOrganization } from 'hooks'
import OrganizationBillingMigrationPanel from '../GeneralSettings/OrganizationBillingMigrationPanel'
import BillingAddress from './BillingAddress/BillingAddress'
import BillingEmail from './BillingEmail'
@@ -16,7 +14,6 @@ import ProjectsSummary from './ProjectsSummary'
import TaxID from './TaxID/TaxID'
const BillingSettings = () => {
- const { ui } = useStore()
const { slug } = useParams()
const organization = useSelectedOrganization()
@@ -24,16 +21,8 @@ const BillingSettings = () => {
const projects =
allProjects?.filter((project) => project.organization_id === organization?.id) ?? []
- const [customer, setCustomer] = useState<any>(null)
- const [isLoadingCustomer, setIsLoadingCustomer] = useState(false)
+ const { data: customer } = useOrganizationCustomerProfileQuery({ slug })
- const [paymentMethods, setPaymentMethods] = useState<any>(null)
- const [isLoadingPaymentMethods, setIsLoadingPaymentMethods] = useState(false)
-
- const [taxIds, setTaxIds] = useState<any>(null)
- const [isLoadingTaxIds, setIsLoadingTaxIds] = useState(false)
-
- const defaultPaymentMethod = customer?.invoice_settings?.default_payment_method ?? ''
const customerBalance = customer && customer.balance ? customer.balance / 100 : 0
const isCredit = customerBalance < 0
const isDebt = customerBalance > 0
@@ -47,88 +36,23 @@ const BillingSettings = () => {
const selectedOrganization = useSelectedOrganization()
const { subscription_id } = selectedOrganization ?? {}
- const getCustomerProfile = async () => {
- try {
- setIsLoadingCustomer(true)
- const customer = await get(`${API_URL}/organizations/${slug}/customer`)
- if (customer.error) throw customer.error
- setCustomer(customer)
- setIsLoadingCustomer(false)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Failed to get organization information: ${error.message}`,
- })
- }
- }
-
- const getPaymentMethods = async () => {
- try {
- setIsLoadingPaymentMethods(true)
- const { data: paymentMethods, error } = await get(`${API_URL}/organizations/${slug}/payments`)
- if (error) throw error
- setPaymentMethods(paymentMethods)
- setIsLoadingPaymentMethods(false)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Failed to get organization payment methods: ${error.message}`,
- })
- }
- }
-
- const getTaxIds = async () => {
- try {
- setIsLoadingTaxIds(true)
- const { data: taxIds, error } = await get(`${API_URL}/organizations/${slug}/tax-ids`)
- if (error) throw error
- setTaxIds(taxIds)
- setIsLoadingTaxIds(false)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Failed to get organization tax IDs: ${error.message}`,
- })
- }
- }
-
- useEffect(() => {
- getCustomerProfile()
- getPaymentMethods()
- getTaxIds()
- }, [slug])
-
return (
<ScaffoldContainerLegacy>
{orgBillingMigrationEnabled && canMigrateOrganization && !subscription_id && (
<OrganizationBillingMigrationPanel />
)}
+
<ProjectsSummary projects={projects} />
+
<CreditBalance balance={balance} isCredit={isCredit} isDebt={isDebt} />
- <PaymentMethods
- loading={isLoadingCustomer || isLoadingPaymentMethods}
- defaultPaymentMethod={defaultPaymentMethod}
- paymentMethods={paymentMethods || []}
- onDefaultMethodUpdated={setCustomer}
- onPaymentMethodsDeleted={() => getPaymentMethods()}
- onPaymentMethodAdded={() => getPaymentMethods()}
- />
+
+ <PaymentMethods />
<BillingEmail />
- <BillingAddress
- loading={isLoadingCustomer}
- address={customer?.address ?? {}}
- onAddressUpdated={(address: any) => setCustomer({ ...customer, address })}
- />
- <TaxID
- loading={isLoadingTaxIds}
- taxIds={taxIds || []}
- onTaxIdsUpdated={(ids: any) => setTaxIds(ids)}
- />
+ <BillingAddress />
+
+ <TaxID />
</ScaffoldContainerLegacy>
)
}
diff --git a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
index 39076b09d92..6b208ea9264 100644
--- a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx
@@ -1,5 +1,6 @@
import * as Tooltip from '@radix-ui/react-tooltip'
import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
import { useState } from 'react'
import {
Alert,
@@ -17,37 +18,38 @@ import {
import { AddNewPaymentMethodModal } from 'components/interfaces/BillingV2'
import NoPermission from 'components/ui/NoPermission'
import Panel from 'components/ui/Panel'
-import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
+import { useOrganizationCustomerProfileQuery } from 'data/organizations/organization-customer-profile-query'
+import { useOrganizationPaymentMethodsQuery } from 'data/organizations/organization-payment-methods-query'
+import { useCheckPermissions, useStore } from 'hooks'
import { delete_, patch } from 'lib/common/fetch'
import { API_URL, BASE_PATH } from 'lib/constants'
import { getURL } from 'lib/helpers'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import AlertError from 'components/ui/AlertError'
-export interface PaymentMethodsProps {
- loading: boolean
- defaultPaymentMethod: string
- paymentMethods: any[]
- onDefaultMethodUpdated: (updatedCustomer: any) => void
- onPaymentMethodsDeleted: () => void
- onPaymentMethodAdded: () => void
-}
-
-const PaymentMethods = ({
- loading,
- defaultPaymentMethod,
- paymentMethods,
- onDefaultMethodUpdated,
- onPaymentMethodsDeleted,
- onPaymentMethodAdded,
-}: PaymentMethodsProps) => {
+const PaymentMethods = () => {
const { ui } = useStore()
- const selectedOrganization = useSelectedOrganization()
- const orgSlug = selectedOrganization?.slug ?? ''
+ const { slug } = useParams()
const [selectedMethodForDefault, setSelectedMethodForDefault] = useState<any>()
const [selectedMethodToDelete, setSelectedMethodToDelete] = useState<any>()
const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false)
const [isUpdatingPaymentMethod, setIsUpdatingPaymentMethod] = useState(false)
+ const { data: customer, refetch: refetchCustomer } = useOrganizationCustomerProfileQuery({ slug })
+
+ const {
+ data,
+ error: paymentMethodsError,
+ refetch: refetchPaymentMethods,
+ isLoading: isLoadingPaymentMethods,
+ isError: isErrorPaymentMethods,
+ isSuccess: isSuccessPaymentMethods,
+ } = useOrganizationPaymentMethodsQuery({ slug })
+ const paymentMethods = data || []
+
+ const defaultPaymentMethod = customer?.invoice_settings?.default_payment_method ?? ''
+
const canReadPaymentMethods = useCheckPermissions(
PermissionAction.BILLING_READ,
'stripe.payment_methods'
@@ -60,13 +62,13 @@ const PaymentMethods = ({
const onConfirmMakeDefaultPaymentMethod = async () => {
try {
setIsUpdatingPaymentMethod(true)
- const updatedCustomer = await patch(`${API_URL}/organizations/${orgSlug}/customer`, {
+ const updatedCustomer = await patch(`${API_URL}/organizations/${slug}/customer`, {
invoice_settings: {
default_payment_method: selectedMethodForDefault.id,
},
})
if (updatedCustomer.error) throw updatedCustomer.error
- onDefaultMethodUpdated(updatedCustomer)
+ refetchCustomer(updatedCustomer)
setSelectedMethodForDefault(undefined)
ui.setNotification({
category: 'success',
@@ -85,11 +87,11 @@ const PaymentMethods = ({
const onConfirmDetachPaymentMethod = async () => {
try {
setIsUpdatingPaymentMethod(true)
- const { error } = await delete_(`${API_URL}/organizations/${orgSlug}/payments`, {
+ const { error } = await delete_(`${API_URL}/organizations/${slug}/payments`, {
card_id: selectedMethodToDelete.id,
})
if (error) throw error
- onPaymentMethodsDeleted()
+ refetchPaymentMethods()
setSelectedMethodToDelete(undefined)
ui.setNotification({ category: 'success', message: 'Successfully deleted payment method' })
} catch (error: any) {
@@ -104,12 +106,12 @@ const PaymentMethods = ({
const onLocalPaymentMethodAdded = () => {
setShowAddPaymentMethodModal(false)
- return onPaymentMethodAdded()
+ return refetchPaymentMethods()
}
return (
<>
- <div className="space-y-2">
+ <div className="space-y-2 -mb-8">
<div>
<h4>Payment methods</h4>
<p className="text-sm opacity-50">
@@ -119,45 +121,45 @@ const PaymentMethods = ({
your account.
</p>
</div>
- {!canReadPaymentMethods ? (
+
+ {!isLoadingPaymentMethods && !canReadPaymentMethods && (
<Panel>
<NoPermission resourceText="view this organization's payment methods" />
</Panel>
- ) : (
+ )}
+
+ {isLoadingPaymentMethods && <GenericSkeletonLoader />}
+
+ {isErrorPaymentMethods && (
+ <AlertError error={paymentMethodsError} subject="Failed to retrieve payment methods" />
+ )}
+
+ {isSuccessPaymentMethods && (
<Panel
- loading={loading}
footer={
- !loading && (
- <div className="flex w-full justify-between">
- {!canUpdatePaymentMethods ? (
- <p className="text-sm text-scale-1000">
- You need additional permissions to manage this organization's payment methods
- </p>
- ) : (
- <div />
- )}
- <div>
- <Button
- key="panel-footer"
- type="default"
- icon={<IconPlus />}
- disabled={!canUpdatePaymentMethods}
- onClick={() => setShowAddPaymentMethodModal(true)}
- >
- Add new card
- </Button>
- </div>
+ <div className="flex w-full justify-between">
+ {!canUpdatePaymentMethods ? (
+ <p className="text-sm text-scale-1000">
+ You need additional permissions to manage this organization's payment methods
+ </p>
+ ) : (
+ <div />
+ )}
+ <div>
+ <Button
+ key="panel-footer"
+ type="default"
+ icon={<IconPlus />}
+ disabled={!canUpdatePaymentMethods}
+ onClick={() => setShowAddPaymentMethodModal(true)}
+ >
+ Add new card
+ </Button>
</div>
- )
+ </div>
}
>
- {loading && paymentMethods.length === 0 ? (
- <div className="flex flex-col justify-between space-y-2 py-4 px-4">
- <div className="shimmering-loader mx-1 w-2/3 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/2 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/3 rounded py-3" />
- </div>
- ) : paymentMethods.length >= 1 ? (
+ {paymentMethods.length >= 1 ? (
<Panel.Content>
<div className="space-y-2">
{paymentMethods.map((paymentMethod: any) => {
@@ -233,7 +235,7 @@ const PaymentMethods = ({
<Button
type="outline"
icon={<IconMoreHorizontal />}
- loading={loading}
+ loading={isLoadingPaymentMethods}
className="hover:border-gray-500"
/>
</Dropdown>
@@ -259,7 +261,7 @@ const PaymentMethods = ({
<AddNewPaymentMethodModal
visible={showAddPaymentMethodModal}
- returnUrl={`${getURL()}/org/${orgSlug}/billing`}
+ returnUrl={`${getURL()}/org/${slug}/billing`}
onCancel={() => setShowAddPaymentMethodModal(false)}
onConfirm={() => onLocalPaymentMethodAdded()}
/>
diff --git a/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx b/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx
index 3566af3da31..51d778ec8fd 100644
--- a/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx
+++ b/studio/components/interfaces/Organization/BillingSettings/TaxID/TaxID.tsx
@@ -1,35 +1,43 @@
import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { useParams } from 'common'
import { isEqual } from 'lodash'
import { useEffect, useState } from 'react'
import { Button, IconPlus, IconX, Input, Listbox } from 'ui'
+import AlertError from 'components/ui/AlertError'
import NoPermission from 'components/ui/NoPermission'
import Panel from 'components/ui/Panel'
-import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks'
+import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'
+import { useOrganizationTaxIDsQuery } from 'data/organizations/organization-tax-ids-query'
+import { TaxIdValue } from 'data/organizations/organization-tax-ids-update-mutation'
+import { useCheckPermissions, useStore } from 'hooks'
import { delete_, post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { uuidv4 } from 'lib/helpers'
import { StripeTaxId, TAX_IDS } from './TaxID.constants'
import { sanitizeTaxID } from './TaxID.utils'
-export interface TaxIDProps {
- loading: boolean
- taxIds: any[]
- onTaxIdsUpdated: (taxIds: any) => void
-}
-
// Stripe recommends to delete tax ids and create new ones to update
// https://stripe.com/docs/billing/customer/tax-ids
-const TaxID = ({ loading, taxIds, onTaxIdsUpdated }: TaxIDProps) => {
+const TaxID = () => {
const { ui } = useStore()
- const selectedOrganization = useSelectedOrganization()
- const slug = selectedOrganization?.slug ?? ''
+ const { slug } = useParams()
+
+ const {
+ data,
+ error: taxIdsError,
+ refetch: refetchTaxIds,
+ isLoading: isLoadingTaxIds,
+ isError: isErrorTaxIds,
+ isSuccess: isSuccessTaxIds,
+ } = useOrganizationTaxIDsQuery({ slug })
+ const taxIds = data || []
const [isSaving, setIsSaving] = useState(false)
const [errors, setErrors] = useState<string[]>([])
- const [taxIdValues, setTaxIdValues] = useState<StripeTaxId[]>(taxIds)
- const formattedTaxIds: StripeTaxId[] = taxIds.map((taxId: StripeTaxId) => {
+ const [taxIdValues, setTaxIdValues] = useState<TaxIdValue[]>([])
+ const formattedTaxIds: StripeTaxId[] = taxIds.map((taxId) => {
return {
id: taxId.id,
type: taxId.type,
@@ -91,7 +99,7 @@ const TaxID = ({ loading, taxIds, onTaxIdsUpdated }: TaxIDProps) => {
setIsSaving(true)
try {
const deletedIds = await Promise.all(
- taxIds.map(async (taxId: StripeTaxId) => {
+ taxIds.map(async (taxId) => {
return await delete_(`${API_URL}/organizations/${slug}/tax-ids`, { id: taxId.id })
})
)
@@ -118,11 +126,8 @@ const TaxID = ({ loading, taxIds, onTaxIdsUpdated }: TaxIDProps) => {
} else {
ui.setNotification({ category: 'success', message: 'Successfully updated tax IDs' })
}
- const idsCreated = newIds
- .filter((taxId: any) => !taxId.result.error)
- .map((taxId: any) => taxId.result)
- onTaxIdsUpdated(idsCreated)
+ refetchTaxIds()
} catch (error: any) {
ui.setNotification({ category: 'error', message: 'Failed to save tax IDs' })
} finally {
@@ -140,15 +145,20 @@ const TaxID = ({ loading, taxIds, onTaxIdsUpdated }: TaxIDProps) => {
</p>
</div>
- {!canReadTaxIds ? (
+ {isLoadingTaxIds && <GenericSkeletonLoader />}
+
+ {!isLoadingTaxIds && !canReadTaxIds && (
<Panel>
<NoPermission resourceText="to view this organization's tax IDs" />
</Panel>
- ) : (
+ )}
+
+ {isErrorTaxIds && <AlertError error={taxIdsError} subject="Failed to retrieve tax IDs" />}
+
+ {isSuccessTaxIds && (
<Panel
- loading={loading}
footer={
- !loading && (
+ !isLoadingTaxIds && (
<div className="flex w-full justify-between">
{!canUpdateTaxIds ? (
<p className="text-sm text-scale-1000">
@@ -180,71 +190,64 @@ const TaxID = ({ loading, taxIds, onTaxIdsUpdated }: TaxIDProps) => {
)
}
>
- {loading && taxIdValues.length === 0 ? (
- <div className="flex flex-col justify-between space-y-2 py-4 px-4">
- <div className="shimmering-loader mx-1 w-2/3 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/2 rounded py-3" />
- <div className="shimmering-loader mx-1 w-1/3 rounded py-3" />
- </div>
- ) : (
- <Panel.Content className="w-8/12 space-y-4">
- {taxIdValues.length >= 1 ? (
- <div className="w-full space-y-2">
- {taxIdValues.map((taxId: StripeTaxId, idx: number) => {
- const selectedTaxId = TAX_IDS.find((option) => option.name === taxId.name)
- return (
- <div key={`tax-id-${idx}`} className="flex items-center space-x-2">
- <Listbox
- value={selectedTaxId?.name}
- onChange={(e: any) => onUpdateTaxId(taxId.id, 'name', e)}
- disabled={!canUpdateTaxIds}
- className="w-[200px]"
- >
- {TAX_IDS.sort((a, b) => a.country.localeCompare(b.country)).map(
- (option) => (
- <Listbox.Option
- key={option.name}
- value={option.name}
- label={option.name}
- >
- {option.country} - {option.name}
- </Listbox.Option>
- )
- )}
- </Listbox>
- <Input
- value={taxId.value}
- placeholder={selectedTaxId?.placeholder ?? ''}
- onChange={(e: any) => onUpdateTaxId(taxId.id, 'value', e.target.value)}
- disabled={!canUpdateTaxIds}
- />
- {canUpdateTaxIds && (
- <Button
- type="text"
- icon={<IconX />}
- onClick={() => onRemoveTaxId(taxId.id)}
- />
+ <Panel.Content className="w-8/12 space-y-4">
+ {taxIdValues.length >= 1 ? (
+ <div className="w-full space-y-2">
+ {taxIdValues.map((taxId: StripeTaxId, idx: number) => {
+ const selectedTaxId = TAX_IDS.find((option) => option.name === taxId.name)
+
+ return (
+ <div key={`tax-id-${idx}`} className="flex items-center space-x-2">
+ <Listbox
+ value={selectedTaxId?.name}
+ onChange={(e: any) => onUpdateTaxId(taxId.id, 'name', e)}
+ disabled={!canUpdateTaxIds}
+ className="w-[200px]"
+ >
+ {TAX_IDS.sort((a, b) => a.country.localeCompare(b.country)).map(
+ (option) => (
+ <Listbox.Option
+ key={option.name}
+ value={option.name}
+ label={option.name}
+ >
+ {option.country} - {option.name}
+ </Listbox.Option>
+ )
)}
- </div>
- )
- })}
- </div>
- ) : (
- <div>
- <p className="flex items-center space-x-2 text-sm text-scale-900">No tax IDs</p>
- </div>
- )}
- {canUpdateTaxIds && (
- <div
- className="flex cursor-pointer items-center space-x-2 opacity-50 transition hover:opacity-100"
- onClick={() => onAddNewTaxId()}
- >
- <IconPlus size={14} />
- <p className="text-sm">Add another ID</p>
- </div>
- )}
- </Panel.Content>
- )}
+ </Listbox>
+ <Input
+ value={taxId.value}
+ placeholder={selectedTaxId?.placeholder ?? ''}
+ onChange={(e: any) => onUpdateTaxId(taxId.id, 'value', e.target.value)}
+ disabled={!canUpdateTaxIds}
+ />
+ {canUpdateTaxIds && (
+ <Button
+ type="text"
+ icon={<IconX />}
+ onClick={() => onRemoveTaxId(taxId.id)}
+ />
+ )}
+ </div>
+ )
+ })}
+ </div>
+ ) : (
+ <div>
+ <p className="flex items-center space-x-2 text-sm text-scale-900">No tax IDs</p>
+ </div>
+ )}
+ {canUpdateTaxIds && (
+ <div
+ className="flex cursor-pointer items-center space-x-2 opacity-50 transition hover:opacity-100"
+ onClick={() => onAddNewTaxId()}
+ >
+ <IconPlus size={14} />
+ <p className="text-sm">Add another ID</p>
+ </div>
+ )}
+ </Panel.Content>
</Panel>
)}
</div>
diff --git a/studio/data/organizations/organization-payment-methods-query.ts b/studio/data/organizations/organization-payment-methods-query.ts
index ca5faad6d9d..46bb30643ad 100644
--- a/studio/data/organizations/organization-payment-methods-query.ts
+++ b/studio/data/organizations/organization-payment-methods-query.ts
@@ -3,6 +3,7 @@ import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { useCallback } from 'react'
import { organizationKeys } from './keys'
+import { ResponseError } from 'types'
export type OrganizationPaymentMethodsVariables = { slug?: string }
export type OrganizationPaymentMethod = {
@@ -46,7 +47,7 @@ export async function getOrganizationPaymentMethods(
export type OrganizationPaymentMethodsData = Awaited<
ReturnType<typeof getOrganizationPaymentMethods>
>
-export type OrganizationPaymentMethodsError = unknown
+export type OrganizationPaymentMethodsError = ResponseError
export const useOrganizationPaymentMethodsQuery = <TData = OrganizationPaymentMethodsData>(
{ slug }: OrganizationPaymentMethodsVariables,