@@ -13,7 +13,6 @@ import { constants } from '@/constants';
13
13
import { useCancelMockedMutation } from '@/hooks/useCancelSubscription' ;
14
14
import { useFleekCheckout } from '@/hooks/useFleekCheckout' ;
15
15
import { usePermissions } from '@/hooks/usePermissions' ;
16
- import { useRouter } from '@/hooks/useRouter' ;
17
16
import { useToast } from '@/hooks/useToast' ;
18
17
import { useBillingContext } from '@/providers/BillingProvider' ;
19
18
import type { BillingPlanType } from '@/types/Billing' ;
@@ -23,11 +22,12 @@ import { dateFormat } from '@/utils/dateFormats';
23
22
24
23
import { CancelPlanModal } from './CancelPlanModal' ;
25
24
import { getDefined } from '@/defined' ;
25
+ import { useCreditsCheckout } from '@/hooks/useCredits' ;
26
26
27
27
export const BillingPlan : React . FC < LoadingProps > = ( { isLoading } ) => {
28
28
const toast = useToast ( ) ;
29
- const router = useRouter ( ) ;
30
- const { subscription , billingPlanType , paymentMethod } = useBillingContext ( ) ;
29
+ const { subscription , billingPlanType , paymentMethod , team } =
30
+ useBillingContext ( ) ;
31
31
32
32
const [ isCancelModalOpen , setIsCancelModalOpen ] = useState ( false ) ;
33
33
@@ -53,7 +53,14 @@ export const BillingPlan: React.FC<LoadingProps> = ({ isLoading }) => {
53
53
// TODO: This fails for some reason
54
54
// router.replace(response.url);
55
55
56
- window . location . href = response . url ;
56
+ if ( response . type === 'CHECKOUT' ) {
57
+ window . location . href = response . content . url ;
58
+
59
+ return ;
60
+ }
61
+
62
+ await Promise . all ( [ subscription . refetch ( ) , team . refetch ( ) ] ) ;
63
+ toast . success ( { message : 'Subscribed successfully!' } ) ;
57
64
} catch ( error ) {
58
65
toast . error ( { error, log : 'Error upgrading plan. Please try again' } ) ;
59
66
}
@@ -71,17 +78,6 @@ export const BillingPlan: React.FC<LoadingProps> = ({ isLoading }) => {
71
78
}
72
79
} ;
73
80
74
- const endPlanDate = useMemo ( ( ) => {
75
- if ( subscription . data ?. endDate ) {
76
- return dateFormat ( {
77
- dateISO : subscription . data . endDate ,
78
- format : DateTime . DATE_FULL ,
79
- } ) ;
80
- }
81
-
82
- return '' ;
83
- } , [ subscription . data ?. endDate ] ) ;
84
-
85
81
const endPeriodDate = useMemo ( ( ) => {
86
82
if ( subscription . data ?. periodEndDate ) {
87
83
return dateFormat ( {
@@ -93,57 +89,19 @@ export const BillingPlan: React.FC<LoadingProps> = ({ isLoading }) => {
93
89
return '' ;
94
90
} , [ subscription . data ?. periodEndDate ] ) ;
95
91
96
- const shouldShowCancellationBanner = useMemo ( ( ) => {
97
- if ( subscription . data ?. endDate ) {
98
- const targetTime = DateTime . fromISO ( subscription . data . endDate ) ;
99
- const currentTime = DateTime . now ( ) ;
100
- const diff = targetTime . diff ( currentTime ) ;
101
-
102
- return Math . floor ( diff . as ( 'months' ) ) <= 1 ;
103
- }
104
-
105
- return null ;
106
- } , [ subscription . data ?. endDate ] ) ;
107
-
108
- const trialEndDate = useMemo ( ( ) => {
109
- if ( subscription . data ?. trialEndDate ) {
110
- return dateFormat ( {
111
- dateISO : subscription . data . trialEndDate ,
112
- format : DateTime . DATE_FULL ,
113
- } ) ;
114
- }
115
-
116
- return '' ;
117
- } , [ subscription . data ?. trialEndDate ] ) ;
118
-
119
92
const { title, description, price } = getPlanData (
120
93
billingPlanType ,
121
94
endPeriodDate ,
122
95
) ;
123
96
124
97
return (
125
- < >
98
+ < Box className = "w-full" >
126
99
< CancelPlanModal
127
100
isOpen = { isCancelModalOpen }
128
101
onOpenChange = { setIsCancelModalOpen }
129
102
onCancelPlan = { handleCancelPlan }
130
103
dueDate = { endPeriodDate }
131
104
/>
132
- { shouldShowCancellationBanner && ! isLoading && (
133
- < AlertBox size = "sm" className = "font-medium" >
134
- Your Pro Plan is expiring. You will be converted to a Free plan on{ ' ' }
135
- { endPlanDate } .
136
- </ AlertBox >
137
- ) }
138
-
139
- { billingPlanType === 'trial' && ! isLoading && (
140
- < AlertBox size = "sm" className = "font-medium" >
141
- Your trial period expires on { trialEndDate } .{ ' ' }
142
- { paymentMethod . data ?. id
143
- ? 'You will be billed after that date.'
144
- : 'Be sure to add your billing info before your trial ends.' }
145
- </ AlertBox >
146
- ) }
147
105
< Billing . HorizontalPlanCard
148
106
isLoading = { isLoading }
149
107
title = { title }
@@ -172,7 +130,7 @@ export const BillingPlan: React.FC<LoadingProps> = ({ isLoading }) => {
172
130
</ Box >
173
131
</ SettingsBox . ActionRow >
174
132
</ Billing . HorizontalPlanCard >
175
- </ >
133
+ </ Box >
176
134
) ;
177
135
} ;
178
136
@@ -192,6 +150,7 @@ const ButtonsContainer: React.FC<ButtonsContainerProps> = ({
192
150
onCancelPlan,
193
151
} ) => {
194
152
const [ isLoading , setIsLoading ] = useState ( false ) ;
153
+ const { handleAddCredits, isCreatingCheckout } = useCreditsCheckout ( ) ;
195
154
const hasManageBillingPermission = usePermissions ( {
196
155
action : [ constants . PERMISSION . BILLING . MANAGE ] ,
197
156
} ) ;
@@ -244,6 +203,13 @@ const ButtonsContainer: React.FC<ButtonsContainerProps> = ({
244
203
{ billingPlanType === 'trial' ? 'Add billing info' : 'Upgrade to Pro' }
245
204
</ Button >
246
205
</ PermissionsTooltip >
206
+ < Button
207
+ size = "sm"
208
+ onClick = { handleAddCredits as ( ) => void }
209
+ loading = { isCreatingCheckout }
210
+ >
211
+ Add credits
212
+ </ Button >
247
213
</ >
248
214
) ;
249
215
} ;
@@ -283,3 +249,60 @@ const getPlanData = (plan: BillingPlanType, endPlanDate?: string): PlanData => {
283
249
} ,
284
250
} [ plan ] ;
285
251
} ;
252
+
253
+ export const Banners = ( { isLoading } : LoadingProps ) => {
254
+ const { subscription, billingPlanType, paymentMethod } = useBillingContext ( ) ;
255
+ const shouldShowCancellationBanner = useMemo ( ( ) => {
256
+ if ( subscription . data ?. endDate ) {
257
+ const targetTime = DateTime . fromISO ( subscription . data . endDate ) ;
258
+ const currentTime = DateTime . now ( ) ;
259
+ const diff = targetTime . diff ( currentTime ) ;
260
+
261
+ return Math . floor ( diff . as ( 'months' ) ) <= 1 ;
262
+ }
263
+
264
+ return null ;
265
+ } , [ subscription . data ?. endDate ] ) ;
266
+
267
+ const trialEndDate = useMemo ( ( ) => {
268
+ if ( subscription . data ?. trialEndDate ) {
269
+ return dateFormat ( {
270
+ dateISO : subscription . data . trialEndDate ,
271
+ format : DateTime . DATE_FULL ,
272
+ } ) ;
273
+ }
274
+
275
+ return '' ;
276
+ } , [ subscription . data ?. trialEndDate ] ) ;
277
+
278
+ const endPlanDate = useMemo ( ( ) => {
279
+ if ( subscription . data ?. endDate ) {
280
+ return dateFormat ( {
281
+ dateISO : subscription . data . endDate ,
282
+ format : DateTime . DATE_FULL ,
283
+ } ) ;
284
+ }
285
+
286
+ return '' ;
287
+ } , [ subscription . data ?. endDate ] ) ;
288
+
289
+ return (
290
+ < >
291
+ { shouldShowCancellationBanner && ! isLoading && (
292
+ < AlertBox size = "sm" className = "font-medium" >
293
+ Your Pro Plan is expiring. You will be converted to a Free plan on{ ' ' }
294
+ { endPlanDate } .
295
+ </ AlertBox >
296
+ ) }
297
+
298
+ { billingPlanType === 'trial' && ! isLoading && (
299
+ < AlertBox size = "sm" className = "font-medium" >
300
+ Your trial period expires on { trialEndDate } .{ ' ' }
301
+ { paymentMethod . data ?. id
302
+ ? 'You will be billed after that date.'
303
+ : 'Be sure to add your billing info before your trial ends.' }
304
+ </ AlertBox >
305
+ ) }
306
+ </ >
307
+ ) ;
308
+ } ;
0 commit comments