refactor: update subscription cancellation state management in OrganizationSettings component
This commit is contained in:
@@ -69,7 +69,7 @@ export default function OrganizationSettings() {
|
|||||||
const [subscription, setSubscription] = useState<SubscriptionDetails | null>(null)
|
const [subscription, setSubscription] = useState<SubscriptionDetails | null>(null)
|
||||||
const [isLoadingSubscription, setIsLoadingSubscription] = useState(false)
|
const [isLoadingSubscription, setIsLoadingSubscription] = useState(false)
|
||||||
const [isRedirectingToPortal, setIsRedirectingToPortal] = useState(false)
|
const [isRedirectingToPortal, setIsRedirectingToPortal] = useState(false)
|
||||||
const [isCanceling, setIsCanceling] = useState(false)
|
const [cancelLoadingAction, setCancelLoadingAction] = useState<'period_end' | 'immediate' | null>(null)
|
||||||
const [showCancelPrompt, setShowCancelPrompt] = useState(false)
|
const [showCancelPrompt, setShowCancelPrompt] = useState(false)
|
||||||
const [showChangePlanModal, setShowChangePlanModal] = useState(false)
|
const [showChangePlanModal, setShowChangePlanModal] = useState(false)
|
||||||
const [changePlanTierIndex, setChangePlanTierIndex] = useState(2)
|
const [changePlanTierIndex, setChangePlanTierIndex] = useState(2)
|
||||||
@@ -269,7 +269,7 @@ export default function OrganizationSettings() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const handleCancelSubscription = async (atPeriodEnd: boolean) => {
|
const handleCancelSubscription = async (atPeriodEnd: boolean) => {
|
||||||
setIsCanceling(true)
|
setCancelLoadingAction(atPeriodEnd ? 'period_end' : 'immediate')
|
||||||
try {
|
try {
|
||||||
await cancelSubscription({ at_period_end: atPeriodEnd })
|
await cancelSubscription({ at_period_end: atPeriodEnd })
|
||||||
toast.success(atPeriodEnd ? 'Subscription will cancel at the end of the billing period.' : 'Subscription canceled.')
|
toast.success(atPeriodEnd ? 'Subscription will cancel at the end of the billing period.' : 'Subscription canceled.')
|
||||||
@@ -278,12 +278,12 @@ export default function OrganizationSettings() {
|
|||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
toast.error(getAuthErrorMessage(error) || error.message || 'Failed to cancel subscription')
|
toast.error(getAuthErrorMessage(error) || error.message || 'Failed to cancel subscription')
|
||||||
} finally {
|
} finally {
|
||||||
setIsCanceling(false)
|
setCancelLoadingAction(null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const openChangePlanModal = () => {
|
const openChangePlanModal = () => {
|
||||||
if (subscription?.pageview_limit) {
|
if (subscription?.pageview_limit != null && subscription.pageview_limit > 0) {
|
||||||
setChangePlanTierIndex(getTierIndexForLimit(subscription.pageview_limit))
|
setChangePlanTierIndex(getTierIndexForLimit(subscription.pageview_limit))
|
||||||
} else {
|
} else {
|
||||||
setChangePlanTierIndex(2)
|
setChangePlanTierIndex(2)
|
||||||
@@ -1154,7 +1154,7 @@ export default function OrganizationSettings() {
|
|||||||
<button
|
<button
|
||||||
onClick={() => setShowCancelPrompt(false)}
|
onClick={() => setShowCancelPrompt(false)}
|
||||||
className="text-neutral-500 hover:text-neutral-700 dark:hover:text-neutral-400"
|
className="text-neutral-500 hover:text-neutral-700 dark:hover:text-neutral-400"
|
||||||
disabled={isCanceling}
|
disabled={cancelLoadingAction != null}
|
||||||
>
|
>
|
||||||
<XIcon className="w-5 h-5" />
|
<XIcon className="w-5 h-5" />
|
||||||
</button>
|
</button>
|
||||||
@@ -1165,8 +1165,8 @@ export default function OrganizationSettings() {
|
|||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<Button
|
<Button
|
||||||
onClick={() => handleCancelSubscription(true)}
|
onClick={() => handleCancelSubscription(true)}
|
||||||
disabled={isCanceling}
|
disabled={cancelLoadingAction != null}
|
||||||
isLoading={isCanceling}
|
isLoading={cancelLoadingAction === 'period_end'}
|
||||||
>
|
>
|
||||||
Cancel at period end
|
Cancel at period end
|
||||||
</Button>
|
</Button>
|
||||||
@@ -1174,11 +1174,12 @@ export default function OrganizationSettings() {
|
|||||||
variant="ghost"
|
variant="ghost"
|
||||||
className="text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20"
|
className="text-red-600 hover:bg-red-50 dark:hover:bg-red-900/20"
|
||||||
onClick={() => handleCancelSubscription(false)}
|
onClick={() => handleCancelSubscription(false)}
|
||||||
disabled={isCanceling}
|
disabled={cancelLoadingAction != null}
|
||||||
|
isLoading={cancelLoadingAction === 'immediate'}
|
||||||
>
|
>
|
||||||
Cancel immediately
|
Cancel immediately
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="ghost" onClick={() => setShowCancelPrompt(false)} disabled={isCanceling}>
|
<Button variant="ghost" onClick={() => setShowCancelPrompt(false)} disabled={cancelLoadingAction != null}>
|
||||||
Keep subscription
|
Keep subscription
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user