@@ -8,6 +8,8 @@ import { getDeploymentId } from "@lowcoder-ee/redux/selectors/configSelectors";
8
8
import { fetchOrgUsersAction } from "redux/reduxActions/orgActions" ;
9
9
import { getOrgUsers } from "redux/selectors/orgSelectors" ;
10
10
import { AppState } from "@lowcoder-ee/redux/reducers" ;
11
+ import { SubscriptionProducts } from "@lowcoder-ee/constants/subscriptionConstants" ;
12
+ import { getFetchSubscriptionsFinished , getSubscriptions , getSubscriptionsError } from "@lowcoder-ee/redux/selectors/subscriptionSelectors" ;
11
13
12
14
// Interfaces
13
15
export interface CustomerAddress {
@@ -384,51 +386,16 @@ export const InitializeSubscription = () => {
384
386
const [ customer , setCustomer ] = useState < StripeCustomer | null > ( null ) ;
385
387
const [ isCreatingCustomer , setIsCreatingCustomer ] = useState < boolean > ( false ) ; // Track customer creation
386
388
const [ customerDataError , setCustomerDataError ] = useState < boolean > ( false ) ;
387
- const [ subscriptions , setSubscriptions ] = useState < SubscriptionItem [ ] > ( [ ] ) ;
388
- const [ subscriptionDataLoaded , setSubscriptionDataLoaded ] = useState < boolean > ( false ) ;
389
- const [ subscriptionDataError , setSubscriptionDataError ] = useState < boolean > ( false ) ;
390
389
const [ checkoutLinkDataLoaded , setCheckoutLinkDataLoaded ] = useState < boolean > ( false ) ;
391
390
const [ checkoutLinkDataError , setCheckoutLinkDataError ] = useState < boolean > ( false ) ;
392
- const [ products , setProducts ] = useState < Product [ ] > ( [
393
- {
394
- pricingType : "Monthly, per User" ,
395
- activeSubscription : false ,
396
- accessLink : "1PhH38DDlQgecLSfSukEgIeV" ,
397
- product : "QW8L3WPMiNjQjI" ,
398
- subscriptionId : "" ,
399
- checkoutLink : "" ,
400
- checkoutLinkDataLoaded : false ,
401
- type : "org" ,
402
- quantity_entity : "orgUser" ,
403
- } ,
404
- {
405
- pricingType : "Monthly, per User" ,
406
- activeSubscription : false ,
407
- accessLink : "1Pf65wDDlQgecLSf6OFlbsD5" ,
408
- product : "QW8MpIBHxieKXd" ,
409
- checkoutLink : "" ,
410
- checkoutLinkDataLoaded : false ,
411
- subscriptionId : "" ,
412
- type : "user" ,
413
- quantity_entity : "singleItem" ,
414
- } ,
415
- {
416
- pricingType : "Monthly, per User" ,
417
- activeSubscription : false ,
418
- accessLink : "1PttHIDDlQgecLSf0XP27tXt" ,
419
- product : "QlQ7cdOh8Lv4dy" ,
420
- subscriptionId : "" ,
421
- checkoutLink : "" ,
422
- checkoutLinkDataLoaded : false ,
423
- type : "org" ,
424
- quantity_entity : "singleItem" ,
425
- } ,
426
- ] ) ;
427
-
391
+ const [ products , setProducts ] = useState < Product [ ] > ( SubscriptionProducts ) ;
428
392
429
393
const user = useSelector ( getUser ) ;
430
394
const currentUser = useSelector ( getCurrentUser ) ;
431
395
const deploymentId = useSelector ( getDeploymentId ) ;
396
+ const subscriptions = useSelector ( getSubscriptions ) ;
397
+ const subscriptionDataLoaded = useSelector ( getFetchSubscriptionsFinished ) ;
398
+ const subscriptionDataError = useSelector ( getSubscriptionsError )
432
399
const currentOrg = user . orgs . find ( org => org . id === user . currentOrgId ) ;
433
400
const orgID = user . currentOrgId ;
434
401
const domain = window . location . protocol + "//" + window . location . hostname + ( window . location . port ? ':' + window . location . port : '' ) ;
@@ -479,22 +446,6 @@ export const InitializeSubscription = () => {
479
446
}
480
447
} , [ deploymentId ] ) ;
481
448
482
- useEffect ( ( ) => {
483
- const fetchSubscriptions = async ( ) => {
484
- if ( customer ) {
485
- try {
486
- const subs = await searchSubscriptions ( customer . id ) ;
487
- setSubscriptions ( subs ) ;
488
- setSubscriptionDataLoaded ( true ) ;
489
- } catch ( error ) {
490
- setSubscriptionDataError ( true ) ;
491
- }
492
- }
493
- } ;
494
-
495
- fetchSubscriptions ( ) ;
496
- } , [ customer ] ) ;
497
-
498
449
useEffect ( ( ) => {
499
450
const prepareCheckout = async ( ) => {
500
451
if ( subscriptionDataLoaded && userCount > 0 ) { // Ensure user count is available
@@ -504,15 +455,15 @@ export const InitializeSubscription = () => {
504
455
const updatedProducts = await Promise . all (
505
456
products . map ( async ( product ) => {
506
457
const matchingSubscription = subscriptions . find (
507
- ( sub ) => sub . plan . id === "price_" + product . accessLink
458
+ ( sub ) => sub . price === product . accessLink
508
459
) ;
509
460
510
461
if ( matchingSubscription ) {
511
462
return {
512
463
...product ,
513
464
activeSubscription : true ,
514
465
checkoutLinkDataLoaded : true ,
515
- subscriptionId : matchingSubscription . id . substring ( 4 ) ,
466
+ subscriptionId : matchingSubscription . id ,
516
467
} ;
517
468
} else {
518
469
// Use the user count to set the quantity for checkout link
@@ -526,16 +477,16 @@ export const InitializeSubscription = () => {
526
477
}
527
478
} )
528
479
) ;
529
-
530
480
setProducts ( updatedProducts ) ;
481
+ setCheckoutLinkDataError ( true ) ;
531
482
} catch ( error ) {
532
483
setCheckoutLinkDataError ( true ) ;
533
484
}
534
485
}
535
486
} ;
536
487
537
488
prepareCheckout ( ) ;
538
- } , [ subscriptionDataLoaded , userCount ] ) ;
489
+ } , [ subscriptionDataLoaded , customer , userCount ] ) ;
539
490
540
491
return {
541
492
customer,
@@ -551,73 +502,4 @@ export const InitializeSubscription = () => {
551
502
} ;
552
503
} ;
553
504
554
- export enum SubscriptionProducts {
555
- SUPPORT = "QW8L3WPMiNjQjI" ,
556
- MEDIAPACKAGE = 'QW8MpIBHxieKXd' ,
557
- AZUREAPIS = 'premium' ,
558
- GOOGLEAPIS = 'enterprise' ,
559
- AWSAPIS = 'enterprise-global' ,
560
- PRIVATECLOUD = 'private-cloud' ,
561
- MATRIXCLOUD = 'matrix-cloud' ,
562
- AGORATOKENSERVER = 'agora-tokenserver' ,
563
- SIGNALSERVER = 'signal-server' ,
564
- DATABASE = 'database' ,
565
- STORAGE = 'storage' ,
566
- IOSAPP = 'ios-app' ,
567
- ANDROIDAPP = 'android-app' ,
568
- AUDITLOG = 'audit-log' ,
569
- APPLOG = 'app-log' ,
570
- ENVIRONMENTS = 'environments' ,
571
- GITREPOS = 'git-repos' ,
572
- }
573
-
574
- export const CheckSubscriptions = ( ) => {
575
- const [ subscriptions , setSubscriptions ] = useState < Subscription [ ] > ( [ ] ) ;
576
- const [ subscriptionDataLoaded , setSubscriptionDataLoaded ] = useState < boolean > ( false ) ;
577
- const [ subscriptionDataError , setSubscriptionDataError ] = useState < boolean > ( false ) ;
578
- const [ loading , setLoading ] = useState < boolean > ( true ) ;
579
-
580
- const user = useSelector ( getUser ) ;
581
- const currentUser = useSelector ( getCurrentUser ) ;
582
- const deploymentId = useSelector ( getDeploymentId ) ;
583
- const orgID = user . currentOrgId ;
584
- const domain = window . location . protocol + "//" + window . location . hostname + ( window . location . port ? ':' + window . location . port : '' ) ;
585
-
586
- const subscriptionSearchCustomer : LowcoderSearchCustomer = {
587
- hostname : domain ,
588
- hostId : deploymentId ,
589
- email : currentUser . email ,
590
- orgId : orgID ,
591
- userId : user . id ,
592
- } ;
593
-
594
- useEffect ( ( ) => {
595
- const fetchCustomerAndSubscriptions = async ( ) => {
596
- try {
597
- const subs = await searchCustomersSubscriptions ( subscriptionSearchCustomer ) ;
598
- setSubscriptions ( subs ) ;
599
- setSubscriptionDataLoaded ( true ) ;
600
- } catch ( error ) {
601
- setSubscriptionDataError ( true ) ;
602
- } finally {
603
- setLoading ( false ) ;
604
- }
605
- } ;
606
- if (
607
- Boolean ( currentUser . email )
608
- && Boolean ( orgID )
609
- && Boolean ( user . id )
610
- && Boolean ( deploymentId )
611
- )
612
- fetchCustomerAndSubscriptions ( ) ;
613
- } , [ subscriptionSearchCustomer ] ) ;
614
-
615
- return {
616
- subscriptions,
617
- subscriptionDataLoaded,
618
- subscriptionDataError,
619
- loading,
620
- } ;
621
- } ;
622
-
623
505
export default SubscriptionApi ;
0 commit comments