@@ -174,6 +174,42 @@ export const usePlansContext = () => {
174
174
[ ctx . subscriptions ] ,
175
175
) ;
176
176
177
+ // returns all subscriptions for a plan that are active or upcoming
178
+ const activeAndUpcomingSubscriptions = useCallback (
179
+ ( plan : CommercePlanResource ) => {
180
+ return ctx . subscriptions
181
+ . filter ( subscription => subscription . plan . id === plan . id )
182
+ . filter ( subscription => subscription . status === 'active' || subscription . status === 'upcoming' ) ;
183
+ } ,
184
+ [ ctx . subscriptions ] ,
185
+ ) ;
186
+
187
+ // return the active or upcoming subscription for a plan based on the plan period, if there is no subscription for the plan period, return the first subscription
188
+ const activeOrUpcomingSubscriptionWithPlanPeriod = useCallback (
189
+ ( plan : CommercePlanResource , planPeriod : CommerceSubscriptionPlanPeriod = 'month' ) => {
190
+ const plansSubscriptions = activeAndUpcomingSubscriptions ( plan ) ;
191
+ // Handle multiple subscriptions for the same plan
192
+ if ( plansSubscriptions . length > 1 ) {
193
+ const subscriptionBaseOnPanPeriod = plansSubscriptions . find ( subscription => {
194
+ return subscription . planPeriod === planPeriod ;
195
+ } ) ;
196
+
197
+ if ( subscriptionBaseOnPanPeriod ) {
198
+ return subscriptionBaseOnPanPeriod ;
199
+ }
200
+
201
+ return plansSubscriptions [ 0 ] ;
202
+ }
203
+
204
+ if ( plansSubscriptions . length === 1 ) {
205
+ return plansSubscriptions [ 0 ] ;
206
+ }
207
+
208
+ return undefined ;
209
+ } ,
210
+ [ activeAndUpcomingSubscriptions ] ,
211
+ ) ;
212
+
177
213
const canManageSubscription = useCallback (
178
214
( { plan, subscription : sub } : { plan ?: CommercePlanResource ; subscription ?: CommerceSubscriptionResource } ) => {
179
215
const subscription = sub ?? ( plan ? activeOrUpcomingSubscription ( plan ) : undefined ) ;
@@ -209,7 +245,8 @@ export const usePlansContext = () => {
209
245
colorScheme : 'secondary' | 'primary' ;
210
246
isDisabled : boolean ;
211
247
} => {
212
- const subscription = sub ?? ( plan ? activeOrUpcomingSubscription ( plan ) : undefined ) ;
248
+ const subscription =
249
+ sub ?? ( plan ? activeOrUpcomingSubscriptionWithPlanPeriod ( plan , selectedPlanPeriod ) : undefined ) ;
213
250
let _selectedPlanPeriod = selectedPlanPeriod ;
214
251
if ( _selectedPlanPeriod === 'annual' && sub ?. plan . annualMonthlyAmount === 0 ) {
215
252
_selectedPlanPeriod = 'month' ;
@@ -220,8 +257,8 @@ export const usePlansContext = () => {
220
257
const getLocalizationKey = ( ) => {
221
258
// Handle subscription cases
222
259
if ( subscription ) {
223
- if ( selectedPlanPeriod !== subscription . planPeriod && subscription . canceledAt ) {
224
- if ( selectedPlanPeriod === 'month' ) {
260
+ if ( _selectedPlanPeriod !== subscription . planPeriod && subscription . canceledAt ) {
261
+ if ( _selectedPlanPeriod === 'month' ) {
225
262
return localizationKeys ( 'commerce.switchToMonthly' ) ;
226
263
}
227
264
@@ -234,8 +271,8 @@ export const usePlansContext = () => {
234
271
return localizationKeys ( 'commerce.reSubscribe' ) ;
235
272
}
236
273
237
- if ( selectedPlanPeriod !== subscription . planPeriod ) {
238
- if ( selectedPlanPeriod === 'month' ) {
274
+ if ( _selectedPlanPeriod !== subscription . planPeriod ) {
275
+ if ( _selectedPlanPeriod === 'month' ) {
239
276
return localizationKeys ( 'commerce.switchToMonthly' ) ;
240
277
}
241
278
@@ -264,7 +301,7 @@ export const usePlansContext = () => {
264
301
isDisabled : ! canManageBilling ,
265
302
} ;
266
303
} ,
267
- [ activeOrUpcomingSubscription , canManageBilling , ctx . subscriptions ] ,
304
+ [ activeOrUpcomingSubscriptionWithPlanPeriod , canManageBilling , ctx . subscriptions ] ,
268
305
) ;
269
306
270
307
const captionForSubscription = useCallback ( ( subscription : CommerceSubscriptionResource ) => {
@@ -280,7 +317,7 @@ export const usePlansContext = () => {
280
317
// handle the selection of a plan, either by opening the subscription details or checkout
281
318
const handleSelectPlan = useCallback (
282
319
( { plan, planPeriod, onSubscriptionChange, mode = 'mounted' , event } : HandleSelectPlanProps ) => {
283
- const subscription = activeOrUpcomingSubscription ( plan ) ;
320
+ const subscription = activeOrUpcomingSubscriptionWithPlanPeriod ( plan , planPeriod ) ;
284
321
285
322
const portalRoot = getClosestProfileScrollBox ( mode , event ) ;
286
323
@@ -324,6 +361,8 @@ export const usePlansContext = () => {
324
361
...ctx ,
325
362
componentName,
326
363
activeOrUpcomingSubscription,
364
+ activeAndUpcomingSubscriptions,
365
+ activeOrUpcomingSubscriptionBasedOnPlanPeriod : activeOrUpcomingSubscriptionWithPlanPeriod ,
327
366
isDefaultPlanImplicitlyActiveOrUpcoming,
328
367
handleSelectPlan,
329
368
buttonPropsForPlan,
0 commit comments