@@ -8,6 +8,8 @@ import { getDeploymentId } from "@lowcoder-ee/redux/selectors/configSelectors";
88import { fetchOrgUsersAction } from "redux/reduxActions/orgActions" ;
99import { getOrgUsers } from "redux/selectors/orgSelectors" ;
1010import { 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" ;
1113
1214// Interfaces
1315export interface CustomerAddress {
@@ -384,51 +386,16 @@ export const InitializeSubscription = () => {
384386 const [ customer , setCustomer ] = useState < StripeCustomer | null > ( null ) ;
385387 const [ isCreatingCustomer , setIsCreatingCustomer ] = useState < boolean > ( false ) ; // Track customer creation
386388 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 ) ;
390389 const [ checkoutLinkDataLoaded , setCheckoutLinkDataLoaded ] = useState < boolean > ( false ) ;
391390 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 ) ;
428392
429393 const user = useSelector ( getUser ) ;
430394 const currentUser = useSelector ( getCurrentUser ) ;
431395 const deploymentId = useSelector ( getDeploymentId ) ;
396+ const subscriptions = useSelector ( getSubscriptions ) ;
397+ const subscriptionDataLoaded = useSelector ( getFetchSubscriptionsFinished ) ;
398+ const subscriptionDataError = useSelector ( getSubscriptionsError )
432399 const currentOrg = user . orgs . find ( org => org . id === user . currentOrgId ) ;
433400 const orgID = user . currentOrgId ;
434401 const domain = window . location . protocol + "//" + window . location . hostname + ( window . location . port ? ':' + window . location . port : '' ) ;
@@ -479,22 +446,6 @@ export const InitializeSubscription = () => {
479446 }
480447 } , [ deploymentId ] ) ;
481448
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-
498449 useEffect ( ( ) => {
499450 const prepareCheckout = async ( ) => {
500451 if ( subscriptionDataLoaded && userCount > 0 ) { // Ensure user count is available
@@ -504,15 +455,15 @@ export const InitializeSubscription = () => {
504455 const updatedProducts = await Promise . all (
505456 products . map ( async ( product ) => {
506457 const matchingSubscription = subscriptions . find (
507- ( sub ) => sub . plan . id === "price_" + product . accessLink
458+ ( sub ) => sub . price === product . accessLink
508459 ) ;
509460
510461 if ( matchingSubscription ) {
511462 return {
512463 ...product ,
513464 activeSubscription : true ,
514465 checkoutLinkDataLoaded : true ,
515- subscriptionId : matchingSubscription . id . substring ( 4 ) ,
466+ subscriptionId : matchingSubscription . id ,
516467 } ;
517468 } else {
518469 // Use the user count to set the quantity for checkout link
@@ -526,16 +477,16 @@ export const InitializeSubscription = () => {
526477 }
527478 } )
528479 ) ;
529-
530480 setProducts ( updatedProducts ) ;
481+ setCheckoutLinkDataError ( true ) ;
531482 } catch ( error ) {
532483 setCheckoutLinkDataError ( true ) ;
533484 }
534485 }
535486 } ;
536487
537488 prepareCheckout ( ) ;
538- } , [ subscriptionDataLoaded , userCount ] ) ;
489+ } , [ subscriptionDataLoaded , customer , userCount ] ) ;
539490
540491 return {
541492 customer,
@@ -551,73 +502,4 @@ export const InitializeSubscription = () => {
551502 } ;
552503} ;
553504
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-
623505export default SubscriptionApi ;
0 commit comments