Skip to content

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

Refactor error handling for project mutations

Joshen Lime2af8cacfc23
diff --git a/studio/components/interfaces/BillingV2/Subscription/CostControl/SpendCapSidePanel.tsx b/studio/components/interfaces/BillingV2/Subscription/CostControl/SpendCapSidePanel.tsx
index 8e150368483..06c93eb02cb 100644
--- a/studio/components/interfaces/BillingV2/Subscription/CostControl/SpendCapSidePanel.tsx
+++ b/studio/components/interfaces/BillingV2/Subscription/CostControl/SpendCapSidePanel.tsx
@@ -41,7 +41,6 @@ const SpendCapSidePanel = () => {
const { isDarkMode } = useTheme()
const [showUsageCosts, setShowUsageCosts] = useState(false)
- const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
const [selectedOption, setSelectedOption] = useState<'on' | 'off'>()
const canUpdateSpendCap = useCheckPermissions(
@@ -54,7 +53,16 @@ const SpendCapSidePanel = () => {
const onClose = () => snap.setPanelKey(undefined)
const { data: subscription, isLoading } = useProjectSubscriptionV2Query({ projectRef })
- const { mutateAsync: updateSubscriptionTier } = useProjectSubscriptionUpdateMutation()
+ const { mutateAsync: updateSubscriptionTier, isLoading: isUpdating } =
+ useProjectSubscriptionUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to toggle spend cap: ${error.message}`,
+ })
+ },
+ })
const isFreePlan = subscription?.plan?.id === 'free'
const isSpendCapOn = !subscription?.usage_billing_enabled
@@ -81,26 +89,15 @@ const SpendCapSidePanel = () => {
const onConfirm = async () => {
if (!projectRef) return console.error('Project ref is required')
- try {
- const tier = (
- selectedOption === 'on' ? PRICING_TIER_PRODUCT_IDS.PRO : PRICING_TIER_PRODUCT_IDS.PAYG
- ) as 'tier_pro' | 'tier_payg'
- setIsSubmitting(true)
- await updateSubscriptionTier({ projectRef, tier })
- ui.setNotification({
- category: 'success',
- message: `Successfully ${isTurningOnCap ? 'enabled' : 'disabled'} spend cap`,
- })
- onClose()
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to toggle spend cap: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
- }
+ const tier = (
+ selectedOption === 'on' ? PRICING_TIER_PRODUCT_IDS.PRO : PRICING_TIER_PRODUCT_IDS.PAYG
+ ) as 'tier_pro' | 'tier_payg'
+ await updateSubscriptionTier({ projectRef, tier })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully ${isTurningOnCap ? 'enabled' : 'disabled'} spend cap`,
+ })
+ onClose()
}
const billingMetricCategories: (keyof typeof pricing)[] = [
@@ -114,8 +111,8 @@ const SpendCapSidePanel = () => {
return (
<SidePanel
size="large"
- loading={isLoading || isSubmitting}
- disabled={isFreePlan || isLoading || !hasChanges || isSubmitting || !canUpdateSpendCap}
+ loading={isLoading || isUpdating}
+ disabled={isFreePlan || isLoading || !hasChanges || isUpdating || !canUpdateSpendCap}
visible={visible}
onCancel={onClose}
onConfirm={onConfirm}
diff --git a/studio/components/interfaces/BillingV2/Subscription/Tier/ExitSurveyModal.tsx b/studio/components/interfaces/BillingV2/Subscription/Tier/ExitSurveyModal.tsx
index 2a9e5c84af7..b0445dd0c28 100644
--- a/studio/components/interfaces/BillingV2/Subscription/Tier/ExitSurveyModal.tsx
+++ b/studio/components/interfaces/BillingV2/Subscription/Tier/ExitSurveyModal.tsx
@@ -29,14 +29,22 @@ const ExitSurveyModal = ({ visible, onClose }: ExitSurveyModalProps) => {
const captchaRef = useRef<HCaptcha>(null)
const router = useRouter()
- const [isSubmitting, setIsSubmitting] = useState(false)
const [message, setMessage] = useState('')
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [selectedReasons, dispatchSelectedReasons] = useReducer(reducer, [])
const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate')
const { data: addons } = useProjectAddonsQuery({ projectRef })
- const { mutateAsync: updateSubscriptionTier } = useProjectSubscriptionUpdateMutation()
+ const { mutateAsync: updateSubscriptionTier, isLoading: isUpdating } =
+ useProjectSubscriptionUpdateMutation({
+ onError: (error) => {
+ return ui.setNotification({
+ error,
+ category: 'error',
+ message: `Failed to cancel subscription: ${error.message}`,
+ })
+ },
+ })
const subscriptionAddons = addons?.selected_addons ?? []
const hasComputeInstance = subscriptionAddons.find((addon) => addon.type === 'compute_instance')
@@ -62,22 +70,12 @@ const ExitSurveyModal = ({ visible, onClose }: ExitSurveyModalProps) => {
})
}
- setIsSubmitting(true)
let token = captchaToken
- try {
- if (!token) {
- const captchaResponse = await captchaRef.current?.execute({ async: true })
- token = captchaResponse?.response ?? null
- await downgradeProject()
- }
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to downgrade project: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
+ if (!token) {
+ const captchaResponse = await captchaRef.current?.execute({ async: true })
+ token = captchaResponse?.response ?? null
+ await downgradeProject()
}
}
@@ -86,16 +84,8 @@ const ExitSurveyModal = ({ visible, onClose }: ExitSurveyModalProps) => {
// If compute instance is present within the existing subscription, then a restart will be triggered
if (!projectRef) return console.error('Project ref is required')
- try {
- await updateSubscriptionTier({ projectRef, tier: 'tier_free' })
- resetCaptcha()
- } catch (error: any) {
- return ui.setNotification({
- error,
- category: 'error',
- message: `Failed to cancel subscription: ${error.message}`,
- })
- }
+ await updateSubscriptionTier({ projectRef, tier: 'tier_free' })
+ resetCaptcha()
try {
const feedbackRes = await post(`${API_URL}/feedback/downgrade`, {
@@ -217,8 +207,8 @@ const ExitSurveyModal = ({ visible, onClose }: ExitSurveyModalProps) => {
<ProjectUpdateDisabledTooltip projectUpdateDisabled={projectUpdateDisabled}>
<Button
type="danger"
- loading={isSubmitting}
- disabled={projectUpdateDisabled || isSubmitting}
+ loading={isUpdating}
+ disabled={projectUpdateDisabled || isUpdating}
onClick={onSubmit}
>
Confirm downgrade
diff --git a/studio/components/interfaces/BillingV2/Subscription/Tier/TierUpdateSidePanel.tsx b/studio/components/interfaces/BillingV2/Subscription/Tier/TierUpdateSidePanel.tsx
index 5c6d5675bec..d3050342aa2 100644
--- a/studio/components/interfaces/BillingV2/Subscription/Tier/TierUpdateSidePanel.tsx
+++ b/studio/components/interfaces/BillingV2/Subscription/Tier/TierUpdateSidePanel.tsx
@@ -30,7 +30,6 @@ const TierUpdateSidePanel = () => {
const slug = selectedOrganization?.slug
const { ref: projectRef } = useParams()
- const [isSubmitting, setIsSubmitting] = useState(false)
const [showExitSurvey, setShowExitSurvey] = useState(false)
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<string>()
const [showDowngradeError, setShowDowngradeError] = useState(false)
@@ -49,7 +48,8 @@ const TierUpdateSidePanel = () => {
const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
const { data: addons } = useProjectAddonsQuery({ projectRef })
const { data: membersExceededLimit } = useFreeProjectLimitCheckQuery({ slug })
- const { mutateAsync: updateSubscriptionTier } = useProjectSubscriptionUpdateMutation()
+ const { mutateAsync: updateSubscriptionTier, isLoading: isUpdating } =
+ useProjectSubscriptionUpdateMutation()
const availablePlans = plans ?? []
const subscriptionAddons = addons?.selected_addons ?? []
@@ -93,29 +93,18 @@ const TierUpdateSidePanel = () => {
return ui.setNotification({ category: 'error', message: 'Please select a payment method' })
}
- try {
- setIsSubmitting(true)
- await updateSubscriptionTier({
- projectRef,
- tier: selectedTier,
- paymentMethod: selectedPaymentMethod,
- })
- ui.setNotification({
- category: 'success',
- message: `Successfully updated subscription to ${selectedTierMeta?.name}!`,
- })
- setSelectedTier(undefined)
- onClose()
- window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to update subscription: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
- }
+ await updateSubscriptionTier({
+ projectRef,
+ tier: selectedTier,
+ paymentMethod: selectedPaymentMethod,
+ })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully updated subscription to ${selectedTierMeta?.name}!`,
+ })
+ setSelectedTier(undefined)
+ onClose()
+ window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
}
return (
@@ -332,7 +321,7 @@ const TierUpdateSidePanel = () => {
</Modal>
<Modal
- loading={isSubmitting}
+ loading={isUpdating}
alignFooter="right"
className="!w-[450px]"
visible={selectedTier !== undefined && selectedTier !== 'tier_free'}
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/CostControl/SpendCapSidePanel.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/CostControl/SpendCapSidePanel.tsx
index 3013c81b829..5cf0536ae21 100644
--- a/studio/components/interfaces/Organization/BillingSettingsV2/CostControl/SpendCapSidePanel.tsx
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/CostControl/SpendCapSidePanel.tsx
@@ -41,7 +41,6 @@ const SpendCapSidePanel = () => {
const { isDarkMode } = useTheme()
const [showUsageCosts, setShowUsageCosts] = useState(false)
- const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
const [selectedOption, setSelectedOption] = useState<'on' | 'off'>()
const canUpdateSpendCap = useCheckPermissions(
@@ -54,7 +53,16 @@ const SpendCapSidePanel = () => {
const onClose = () => snap.setPanelKey(undefined)
const { data: subscription, isLoading } = useOrgSubscriptionQuery({ orgSlug: slug })
- const { mutateAsync: updateOrgSubscription } = useOrgSubscriptionUpdateMutation()
+ const { mutateAsync: updateOrgSubscription, isLoading: isUpdating } =
+ useOrgSubscriptionUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Failed to toggle spend cap: ${error.message}`,
+ })
+ },
+ })
const isFreePlan = subscription?.plan?.id === 'free'
const isSpendCapOn = !subscription?.usage_billing_enabled
@@ -82,26 +90,18 @@ const SpendCapSidePanel = () => {
const onConfirm = async () => {
if (!slug) return console.error('Org slug is required')
- try {
- const tier = (
- selectedOption === 'on' ? PRICING_TIER_PRODUCT_IDS.PRO : PRICING_TIER_PRODUCT_IDS.PAYG
- ) as 'tier_pro' | 'tier_payg'
- setIsSubmitting(true)
- await updateOrgSubscription({ slug, tier })
- ui.setNotification({
- category: 'success',
- message: `Successfully ${isTurningOnCap ? 'enabled' : 'disabled'} spend cap`,
- })
- onClose()
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to toggle spend cap: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
- }
+ const tier = (
+ selectedOption === 'on' ? PRICING_TIER_PRODUCT_IDS.PRO : PRICING_TIER_PRODUCT_IDS.PAYG
+ ) as 'tier_pro' | 'tier_payg'
+
+ await updateOrgSubscription({ slug, tier })
+
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully ${isTurningOnCap ? 'enabled' : 'disabled'} spend cap`,
+ })
+
+ onClose()
}
const billingMetricCategories: (keyof typeof pricing)[] = [
@@ -115,8 +115,8 @@ const SpendCapSidePanel = () => {
return (
<SidePanel
size="large"
- loading={isLoading || isSubmitting}
- disabled={isFreePlan || isLoading || !hasChanges || isSubmitting || !canUpdateSpendCap}
+ loading={isLoading || isUpdating}
+ disabled={isFreePlan || isLoading || !hasChanges || isUpdating || !canUpdateSpendCap}
visible={visible}
onCancel={onClose}
onConfirm={onConfirm}
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx
index 9036b975955..f4a56b273d2 100644
--- a/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/PaymentMethods/ChangePaymentMethodModal.tsx
@@ -21,7 +21,14 @@ const ChangePaymentMethodModal = ({
const { slug } = useParams()
const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug })
const { mutateAsync: updateOrgSubscription, isLoading: isUpdating } =
- useOrgSubscriptionUpdateMutation()
+ useOrgSubscriptionUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to change payment method: ${error.message}`,
+ })
+ },
+ })
const onConfirmUpdate = async () => {
if (!slug) return console.error('Slug is required')
@@ -33,23 +40,16 @@ const ChangePaymentMethodModal = ({
? 'tier_payg'
: `tier_${subscription.plan.id}`
- try {
- await updateOrgSubscription({
- slug,
- tier: selectedTier as SubscriptionTier,
- paymentMethod: selectedPaymentMethod.id,
- })
- ui.setNotification({
- category: 'success',
- message: `Successfully changed payment method to the card ending with ${selectedPaymentMethod.card.last4}`,
- })
- onClose()
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to change payment method: ${error.message}`,
- })
- }
+ await updateOrgSubscription({
+ slug,
+ tier: selectedTier as SubscriptionTier,
+ paymentMethod: selectedPaymentMethod.id,
+ })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully changed payment method to the card ending with ${selectedPaymentMethod.card.last4}`,
+ })
+ onClose()
}
return (
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/ExitSurveyModal.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/ExitSurveyModal.tsx
index cea90033518..1103e809c00 100644
--- a/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/ExitSurveyModal.tsx
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/ExitSurveyModal.tsx
@@ -24,13 +24,20 @@ const ExitSurveyModal = ({ visible, subscription, onClose }: ExitSurveyModalProp
const { slug } = useParams()
const captchaRef = useRef<HCaptcha>(null)
- const [isSubmitting, setIsSubmitting] = useState(false)
const [message, setMessage] = useState('')
const [captchaToken, setCaptchaToken] = useState<string | null>(null)
const [selectedReasons, dispatchSelectedReasons] = useReducer(reducer, [])
const subscriptionUpdateDisabled = useFlag('disableProjectCreationAndUpdate')
- const { mutateAsync: updateOrgSubscription } = useOrgSubscriptionUpdateMutation()
+ const { mutateAsync: updateOrgSubscription, isLoading: isUpdating } =
+ useOrgSubscriptionUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ category: 'error',
+ message: `Failed to downgrade project: ${error.message}`,
+ })
+ },
+ })
const projectsWithComputeInstances =
subscription?.project_addons.filter((project) =>
@@ -59,22 +66,12 @@ const ExitSurveyModal = ({ visible, subscription, onClose }: ExitSurveyModalProp
})
}
- setIsSubmitting(true)
let token = captchaToken
- try {
- if (!token) {
- const captchaResponse = await captchaRef.current?.execute({ async: true })
- token = captchaResponse?.response ?? null
- await downgradeOrganization()
- }
- } catch (error: any) {
- ui.setNotification({
- category: 'error',
- message: `Failed to downgrade project: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
+ if (!token) {
+ const captchaResponse = await captchaRef.current?.execute({ async: true })
+ token = captchaResponse?.response ?? null
+ await downgradeOrganization()
}
}
@@ -222,8 +219,8 @@ const ExitSurveyModal = ({ visible, subscription, onClose }: ExitSurveyModalProp
<ProjectUpdateDisabledTooltip projectUpdateDisabled={subscriptionUpdateDisabled}>
<Button
type="danger"
- loading={isSubmitting}
- disabled={subscriptionUpdateDisabled || isSubmitting}
+ loading={isUpdating}
+ disabled={subscriptionUpdateDisabled || isUpdating}
onClick={onSubmit}
>
Confirm downgrade
diff --git a/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/PlanUpdateSidePanel.tsx b/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/PlanUpdateSidePanel.tsx
index 12f9f420071..490c03c66cc 100644
--- a/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/PlanUpdateSidePanel.tsx
+++ b/studio/components/interfaces/Organization/BillingSettingsV2/Subscription/PlanUpdateSidePanel.tsx
@@ -30,7 +30,6 @@ const PlanUpdateSidePanel = () => {
const selectedOrganization = useSelectedOrganization()
const slug = selectedOrganization?.slug
- const [isSubmitting, setIsSubmitting] = useState(false)
const [showExitSurvey, setShowExitSurvey] = useState(false)
const [selectedPaymentMethod, setSelectedPaymentMethod] = useState<string>()
const [showDowngradeError, setShowDowngradeError] = useState(false)
@@ -48,7 +47,16 @@ const PlanUpdateSidePanel = () => {
const { data: subscription } = useOrgSubscriptionQuery({ orgSlug: slug })
const { data: plans, isLoading: isLoadingPlans } = useOrgPlansQuery({ orgSlug: slug })
const { data: membersExceededLimit } = useFreeProjectLimitCheckQuery({ slug })
- const { mutateAsync: updateOrgSubscription } = useOrgSubscriptionUpdateMutation()
+ const { mutateAsync: updateOrgSubscription, isLoading: isUpdating } =
+ useOrgSubscriptionUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update subscription: ${error.message}`,
+ })
+ },
+ })
const availablePlans = plans ?? []
const hasMembersExceedingFreeTierLimit = (membersExceededLimit || []).length > 0
@@ -91,29 +99,18 @@ const PlanUpdateSidePanel = () => {
return ui.setNotification({ category: 'error', message: 'Please select a payment method' })
}
- try {
- setIsSubmitting(true)
- await updateOrgSubscription({
- slug,
- tier: selectedTier,
- paymentMethod: selectedPaymentMethod,
- })
- ui.setNotification({
- category: 'success',
- message: `Successfully updated subscription to ${subscriptionPlanMeta?.name}!`,
- })
- setSelectedTier(undefined)
- onClose()
- window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to update subscription: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
- }
+ await updateOrgSubscription({
+ slug,
+ tier: selectedTier,
+ paymentMethod: selectedPaymentMethod,
+ })
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully updated subscription to ${subscriptionPlanMeta?.name}!`,
+ })
+ setSelectedTier(undefined)
+ onClose()
+ window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
}
return (
@@ -286,7 +283,7 @@ const PlanUpdateSidePanel = () => {
/>
<Modal
- loading={isSubmitting}
+ loading={isUpdating}
alignFooter="right"
className="!w-[450px]"
visible={selectedTier !== undefined && selectedTier !== 'tier_free'}
diff --git a/studio/components/interfaces/Settings/Addons/ComputeInstanceSidePanel.tsx b/studio/components/interfaces/Settings/Addons/ComputeInstanceSidePanel.tsx
index 606181f3197..69ff6557d1a 100644
--- a/studio/components/interfaces/Settings/Addons/ComputeInstanceSidePanel.tsx
+++ b/studio/components/interfaces/Settings/Addons/ComputeInstanceSidePanel.tsx
@@ -48,7 +48,6 @@ const ComputeInstanceSidePanel = () => {
const organization = useSelectedOrganization()
const isOrgBilling = !!organization?.subscription_id
- const [isSubmitting, setIsSubmitting] = useState(false)
const [showConfirmationModal, setShowConfirmationModal] = useState(false)
const [selectedCategory, setSelectedCategory] = useState<'micro' | 'optimized'>('micro')
const [selectedOption, setSelectedOption] = useState<string>('ci_micro')
@@ -64,8 +63,25 @@ const ComputeInstanceSidePanel = () => {
const { data: addons, isLoading } = useProjectAddonsQuery({ projectRef })
const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { mutateAsync: updateAddon } = useProjectAddonUpdateMutation()
- const { mutateAsync: removeAddon } = useProjectAddonRemoveMutation()
+ const { mutateAsync: updateAddon, isLoading: isUpdating } = useProjectAddonUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update compute instance: ${error.message}`,
+ })
+ },
+ })
+ const { mutateAsync: removeAddon, isLoading: isRemoving } = useProjectAddonRemoveMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update compute instance: ${error.message}`,
+ })
+ },
+ })
+ const isSubmitting = isUpdating || isRemoving
const projectId = selectedProject?.id
const selectedAddons = addons?.selected_addons ?? []
@@ -109,34 +125,22 @@ const ComputeInstanceSidePanel = () => {
if (!projectRef) return console.error('Project ref is required')
if (!projectId) return console.error('Project ID is required')
- try {
- setIsSubmitting(true)
-
- if (selectedOption === 'ci_micro' && subscriptionCompute !== undefined) {
- await removeAddon({ projectRef, variant: subscriptionCompute.variant.identifier })
- } else {
- await updateAddon({ projectRef, type: 'compute_instance', variant: selectedOption })
- }
-
- ui.setNotification({
- duration: 8000,
- category: 'success',
- message: `Successfully updated compute instance to ${
- selectedCompute?.name || 'Micro'
- }. Your project is currently being restarted to update its instance`,
- })
- setProjectStatus(queryClient, projectRef, PROJECT_STATUS.RESTORING)
- onClose()
- router.push(`/project/${projectRef}`)
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to update compute instance: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
+ if (selectedOption === 'ci_micro' && subscriptionCompute !== undefined) {
+ await removeAddon({ projectRef, variant: subscriptionCompute.variant.identifier })
+ } else {
+ await updateAddon({ projectRef, type: 'compute_instance', variant: selectedOption })
}
+
+ ui.setNotification({
+ duration: 8000,
+ category: 'success',
+ message: `Successfully updated compute instance to ${
+ selectedCompute?.name || 'Micro'
+ }. Your project is currently being restarted to update its instance`,
+ })
+ setProjectStatus(queryClient, projectRef, PROJECT_STATUS.RESTORING)
+ onClose()
+ router.push(`/project/${projectRef}`)
}
return (
diff --git a/studio/components/interfaces/Settings/Addons/CustomDomainSidePanel.tsx b/studio/components/interfaces/Settings/Addons/CustomDomainSidePanel.tsx
index 2198395a037..ad26daa2494 100644
--- a/studio/components/interfaces/Settings/Addons/CustomDomainSidePanel.tsx
+++ b/studio/components/interfaces/Settings/Addons/CustomDomainSidePanel.tsx
@@ -21,7 +21,6 @@ const CustomDomainSidePanel = () => {
const organization = useSelectedOrganization()
const isOrgBilling = !!organization?.subscription_id
- const [isSubmitting, setIsSubmitting] = useState<boolean>(false)
const [selectedOption, setSelectedOption] = useState<string>('cd_none')
const canUpdateCustomDomain = useCheckPermissions(
@@ -35,8 +34,25 @@ const CustomDomainSidePanel = () => {
const { data: addons, isLoading } = useProjectAddonsQuery({ projectRef })
const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { mutateAsync: updateAddon } = useProjectAddonUpdateMutation()
- const { mutateAsync: removeAddon } = useProjectAddonRemoveMutation()
+ const { mutateAsync: updateAddon, isLoading: isUpdating } = useProjectAddonUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update custom domain: ${error.message}`,
+ })
+ },
+ })
+ const { mutateAsync: removeAddon, isLoading: isRemoving } = useProjectAddonRemoveMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update custom domain: ${error.message}`,
+ })
+ },
+ })
+ const isSubmitting = isUpdating || isRemoving
const subscriptionCDOption = (addons?.selected_addons ?? []).find(
(addon) => addon.type === 'custom_domain'
@@ -74,31 +90,19 @@ const CustomDomainSidePanel = () => {
const onConfirm = async () => {
if (!projectRef) return console.error('Project ref is required')
- try {
- setIsSubmitting(true)
-
- if (selectedOption === 'cd_none' && subscriptionCDOption !== undefined) {
- await removeAddon({ projectRef, variant: subscriptionCDOption.variant.identifier })
- } else {
- await updateAddon({ projectRef, type: 'custom_domain', variant: selectedOption })
- }
-
- ui.setNotification({
- category: 'success',
- message: `Successfully ${
- selectedOption === 'cd_none' ? 'disabled' : 'enabled'
- } custom domain`,
- })
- onClose()
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to update custom domain: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
+ if (selectedOption === 'cd_none' && subscriptionCDOption !== undefined) {
+ await removeAddon({ projectRef, variant: subscriptionCDOption.variant.identifier })
+ } else {
+ await updateAddon({ projectRef, type: 'custom_domain', variant: selectedOption })
}
+
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully ${
+ selectedOption === 'cd_none' ? 'disabled' : 'enabled'
+ } custom domain`,
+ })
+ onClose()
}
return (
diff --git a/studio/components/interfaces/Settings/Addons/PITRSidePanel.tsx b/studio/components/interfaces/Settings/Addons/PITRSidePanel.tsx
index b1b00731bcd..c3fb4d5a2fc 100644
--- a/studio/components/interfaces/Settings/Addons/PITRSidePanel.tsx
+++ b/studio/components/interfaces/Settings/Addons/PITRSidePanel.tsx
@@ -44,7 +44,6 @@ const PITRSidePanel = () => {
const organization = useSelectedOrganization()
const isOrgBilling = !!organization?.subscription_id
- const [isSubmitting, setIsSubmitting] = useState(false)
const [selectedCategory, setSelectedCategory] = useState<'on' | 'off'>('off')
const [selectedOption, setSelectedOption] = useState<string>('pitr_0')
@@ -56,8 +55,25 @@ const PITRSidePanel = () => {
const { data: addons, isLoading } = useProjectAddonsQuery({ projectRef })
const { data: subscription } = useProjectSubscriptionV2Query({ projectRef })
- const { mutateAsync: updateAddon } = useProjectAddonUpdateMutation()
- const { mutateAsync: removeAddon } = useProjectAddonRemoveMutation()
+ const { mutateAsync: updateAddon, isLoading: isUpdating } = useProjectAddonUpdateMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update PITR: ${error.message}`,
+ })
+ },
+ })
+ const { mutateAsync: removeAddon, isLoading: isRemoving } = useProjectAddonRemoveMutation({
+ onError: (error) => {
+ ui.setNotification({
+ error,
+ category: 'error',
+ message: `Unable to update PITR: ${error.message}`,
+ })
+ },
+ })
+ const isSubmitting = isUpdating || isRemoving
const selectedAddons = addons?.selected_addons ?? []
const availableAddons = addons?.available_addons ?? []
@@ -96,31 +112,19 @@ const PITRSidePanel = () => {
const onConfirm = async () => {
if (!projectRef) return console.error('Project ref is required')
- try {
- setIsSubmitting(true)
-
- if (selectedOption === 'pitr_0' && subscriptionPitr !== undefined) {
- await removeAddon({ projectRef, variant: subscriptionPitr.variant.identifier })
- } else {
- await updateAddon({ projectRef, type: 'pitr', variant: selectedOption })
- }
-
- ui.setNotification({
- category: 'success',
- message: `Successfully ${
- selectedOption === 'pitr_0' ? 'disabled' : 'updated'
- } point in time recovery duration`,
- })
- onClose()
- } catch (error: any) {
- ui.setNotification({
- error,
- category: 'error',
- message: `Unable to update PITR: ${error.message}`,
- })
- } finally {
- setIsSubmitting(false)
+ if (selectedOption === 'pitr_0' && subscriptionPitr !== undefined) {
+ await removeAddon({ projectRef, variant: subscriptionPitr.variant.identifier })
+ } else {
+ await updateAddon({ projectRef, type: 'pitr', variant: selectedOption })
}
+
+ ui.setNotification({
+ category: 'success',
+ message: `Successfully ${
+ selectedOption === 'pitr_0' ? 'disabled' : 'updated'
+ } point in time recovery duration`,
+ })
+ onClose()
}
return (
diff --git a/studio/data/subscriptions/org-subscription-update-mutation.ts b/studio/data/subscriptions/org-subscription-update-mutation.ts
index d2592bbcc8f..74db5bbba2f 100644
--- a/studio/data/subscriptions/org-subscription-update-mutation.ts
+++ b/studio/data/subscriptions/org-subscription-update-mutation.ts
@@ -1,8 +1,10 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { put } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import { ResponseError, SupaResponseV2 } from 'types/base'
import { subscriptionKeys } from './keys'
-import { SupaResponseV2 } from 'types/base'
export type SubscriptionTier =
| 'tier_free'
@@ -40,14 +42,15 @@ type OrgSubscriptionUpdateData = Awaited<ReturnType<typeof updateOrgSubscription
export const useOrgSubscriptionUpdateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<OrgSubscriptionUpdateData, unknown, OrgSubscriptionUpdateVariables>,
+ UseMutationOptions<OrgSubscriptionUpdateData, ResponseError, OrgSubscriptionUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<OrgSubscriptionUpdateData, unknown, OrgSubscriptionUpdateVariables>(
+ return useMutation<OrgSubscriptionUpdateData, ResponseError, OrgSubscriptionUpdateVariables>(
(vars) => updateOrgSubscription(vars),
{
async onSuccess(data, variables, context) {
@@ -55,6 +58,13 @@ export const useOrgSubscriptionUpdateMutation = ({
await queryClient.invalidateQueries(subscriptionKeys.orgSubscription(slug))
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to update subscription: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/subscriptions/project-addon-remove-mutation.ts b/studio/data/subscriptions/project-addon-remove-mutation.ts
index eda59c2139c..75a930727fe 100644
--- a/studio/data/subscriptions/project-addon-remove-mutation.ts
+++ b/studio/data/subscriptions/project-addon-remove-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { delete_ } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { subscriptionKeys } from './keys'
export type ProjectAddonRemoveVariables = {
@@ -31,14 +34,15 @@ type ProjectAddonRemoveData = Awaited<ReturnType<typeof removeSubscriptionAddon>
export const useProjectAddonRemoveMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<ProjectAddonRemoveData, unknown, ProjectAddonRemoveVariables>,
+ UseMutationOptions<ProjectAddonRemoveData, ResponseError, ProjectAddonRemoveVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<ProjectAddonRemoveData, unknown, ProjectAddonRemoveVariables>(
+ return useMutation<ProjectAddonRemoveData, ResponseError, ProjectAddonRemoveVariables>(
(vars) => removeSubscriptionAddon(vars),
{
async onSuccess(data, variables, context) {
@@ -48,6 +52,13 @@ export const useProjectAddonRemoveMutation = ({
await queryClient.invalidateQueries(subscriptionKeys.addons(projectRef))
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to remove addon: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/subscriptions/project-addon-update-mutation.ts b/studio/data/subscriptions/project-addon-update-mutation.ts
index d154d1e8206..095910e7839 100644
--- a/studio/data/subscriptions/project-addon-update-mutation.ts
+++ b/studio/data/subscriptions/project-addon-update-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { subscriptionKeys } from './keys'
export type ProjectAddonUpdateVariables = {
@@ -35,14 +38,15 @@ type ProjectAddonUpdateData = Awaited<ReturnType<typeof updateSubscriptionAddon>
export const useProjectAddonUpdateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<ProjectAddonUpdateData, unknown, ProjectAddonUpdateVariables>,
+ UseMutationOptions<ProjectAddonUpdateData, ResponseError, ProjectAddonUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<ProjectAddonUpdateData, unknown, ProjectAddonUpdateVariables>(
+ return useMutation<ProjectAddonUpdateData, ResponseError, ProjectAddonUpdateVariables>(
(vars) => updateSubscriptionAddon(vars),
{
async onSuccess(data, variables, context) {
@@ -53,6 +57,13 @@ export const useProjectAddonUpdateMutation = ({
])
await onSuccess?.(data, variables, context)
},
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to update addon: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
...options,
}
)
diff --git a/studio/data/subscriptions/project-subscription-update-mutation.ts b/studio/data/subscriptions/project-subscription-update-mutation.ts
index 69c5b853522..e3f100de6b9 100644
--- a/studio/data/subscriptions/project-subscription-update-mutation.ts
+++ b/studio/data/subscriptions/project-subscription-update-mutation.ts
@@ -1,6 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'react-hot-toast'
+
import { put } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
+import { ResponseError } from 'types'
import { subscriptionKeys } from './keys'
export type SubscriptionTier =
@@ -44,25 +47,38 @@ type ProjectSubscriptionUpdateData = Awaited<ReturnType<typeof updateSubscriptio
export const useProjectSubscriptionUpdateMutation = ({
onSuccess,
+ onError,
...options
}: Omit<
- UseMutationOptions<ProjectSubscriptionUpdateData, unknown, ProjectSubscriptionUpdateVariables>,
+ UseMutationOptions<
+ ProjectSubscriptionUpdateData,
+ ResponseError,
+ ProjectSubscriptionUpdateVariables
+ >,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
- return useMutation<ProjectSubscriptionUpdateData, unknown, ProjectSubscriptionUpdateVariables>(
- (vars) => updateSubscriptionTier(vars),
- {
- async onSuccess(data, variables, context) {
- const { projectRef } = variables
- await Promise.all([
- queryClient.invalidateQueries(subscriptionKeys.subscriptionV2(projectRef)),
- queryClient.invalidateQueries(subscriptionKeys.addons(projectRef)),
- ])
- await onSuccess?.(data, variables, context)
- },
- ...options,
- }
- )
+ return useMutation<
+ ProjectSubscriptionUpdateData,
+ ResponseError,
+ ProjectSubscriptionUpdateVariables
+ >((vars) => updateSubscriptionTier(vars), {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await Promise.all([
+ queryClient.invalidateQueries(subscriptionKeys.subscriptionV2(projectRef)),
+ queryClient.invalidateQueries(subscriptionKeys.addons(projectRef)),
+ ])
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to update subscription: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ })
}