|
| 1 | +import { createCheckoutLink, createCustomer, getProducts, searchCustomer } from "@lowcoder-ee/api/subscriptionApi"; |
| 2 | +import { StripeCustomer, SubscriptionProduct, InitSubscriptionProducts, LowcoderSearchCustomer, LowcoderNewCustomer, Subscription } from "@lowcoder-ee/constants/subscriptionConstants"; |
| 3 | +import { getDeploymentId } from "@lowcoder-ee/redux/selectors/configSelectors"; |
| 4 | +import { getFetchSubscriptionsFinished, getSubscriptions, getSubscriptionsError } from "@lowcoder-ee/redux/selectors/subscriptionSelectors"; |
| 5 | +import { getCurrentUser, getUser } from "@lowcoder-ee/redux/selectors/usersSelectors"; |
| 6 | +import { createContext, ReactNode, useContext, useEffect, useState } from "react"; |
| 7 | +import { useSelector } from "react-redux"; |
| 8 | + |
| 9 | +export interface SubscriptionContextType { |
| 10 | + products: SubscriptionProduct[]; |
| 11 | + subscriptionProducts: any[], |
| 12 | + customer?: StripeCustomer; |
| 13 | + isCreatingCustomer: boolean; |
| 14 | + customerDataError: boolean; |
| 15 | + subscriptionDataError?: string; |
| 16 | + checkoutLinkDataError: boolean; |
| 17 | + subscriptionDataLoaded: boolean; |
| 18 | + checkoutLinkDataLoaded: boolean; |
| 19 | + subscriptionProductsLoading: boolean; |
| 20 | + subscriptions: Subscription[], |
| 21 | + admin: "admin" | "member", |
| 22 | +} |
| 23 | + |
| 24 | +const SimpleSubscriptionContext = createContext<SubscriptionContextType>({ |
| 25 | + products: [], |
| 26 | + subscriptionProducts: [], |
| 27 | + customer: undefined, |
| 28 | + isCreatingCustomer: false, |
| 29 | + customerDataError: false, |
| 30 | + subscriptionDataError: undefined, |
| 31 | + checkoutLinkDataError: false, |
| 32 | + subscriptionDataLoaded: false, |
| 33 | + checkoutLinkDataLoaded: false, |
| 34 | + subscriptionProductsLoading: false, |
| 35 | + subscriptions: [], |
| 36 | + admin: "member", |
| 37 | +}); |
| 38 | + |
| 39 | +export const SimpleSubscriptionContextProvider = (props: { |
| 40 | + children: ReactNode, |
| 41 | +}) => { |
| 42 | + const [customer, setCustomer] = useState<StripeCustomer>(); |
| 43 | + const [isCreatingCustomer, setIsCreatingCustomer] = useState<boolean>(false); // Track customer creation |
| 44 | + const [customerDataError, setCustomerDataError] = useState<boolean>(false); |
| 45 | + const [checkoutLinkDataLoaded, setCheckoutLinkDataLoaded] = useState<boolean>(false); |
| 46 | + const [checkoutLinkDataError, setCheckoutLinkDataError] = useState<boolean>(false); |
| 47 | + const [products, setProducts] = useState<SubscriptionProduct[]>(InitSubscriptionProducts); |
| 48 | + const [productsLoaded, setProductsLoaded] = useState<boolean>(false); |
| 49 | + const [subscriptionProducts, setSubscriptionProducts] = useState<any[]>([]); |
| 50 | + const [subscriptionProductsLoading, setSubscriptionProductsLoading] = useState<boolean>(false); |
| 51 | + |
| 52 | + const user = useSelector(getUser); |
| 53 | + const currentUser = useSelector(getCurrentUser); |
| 54 | + const deploymentId = useSelector(getDeploymentId); |
| 55 | + const subscriptions = useSelector(getSubscriptions); |
| 56 | + const subscriptionDataLoaded = useSelector(getFetchSubscriptionsFinished); |
| 57 | + const subscriptionDataError = useSelector(getSubscriptionsError); |
| 58 | + |
| 59 | + const currentOrg = user.orgs.find(org => org.id === user.currentOrgId); |
| 60 | + const orgID = user.currentOrgId; |
| 61 | + const domain = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : ''); |
| 62 | + const admin = user.orgRoleMap.get(orgID) === "admin" ? "admin" : "member"; |
| 63 | + |
| 64 | + const subscriptionSearchCustomer: LowcoderSearchCustomer = { |
| 65 | + hostname: domain, |
| 66 | + hostId: deploymentId, |
| 67 | + email: currentUser.email, |
| 68 | + orgId: orgID, |
| 69 | + userId: user.id, |
| 70 | + }; |
| 71 | + |
| 72 | + const subscriptionNewCustomer: LowcoderNewCustomer = { |
| 73 | + hostname: domain, |
| 74 | + hostId: deploymentId, |
| 75 | + email: currentUser.email, |
| 76 | + orgId: orgID, |
| 77 | + userId: user.id, |
| 78 | + userName: user.username, |
| 79 | + type: admin, |
| 80 | + companyName: currentOrg?.name || "Unknown", |
| 81 | + }; |
| 82 | + |
| 83 | + useEffect(() => { |
| 84 | + const fetchProducts = async () => { |
| 85 | + try { |
| 86 | + const productData = await getProducts(); |
| 87 | + setSubscriptionProducts(productData); |
| 88 | + } catch (err) { |
| 89 | + // setError("Failed to fetch product."); |
| 90 | + console.error("Failed to fetch product.", err); |
| 91 | + } finally { |
| 92 | + setSubscriptionProductsLoading(false); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + if (!Boolean(subscriptionProducts.length)) { |
| 97 | + fetchProducts(); |
| 98 | + } |
| 99 | + }, [subscriptionProducts]); |
| 100 | + |
| 101 | + useEffect(() => { |
| 102 | + const initializeCustomer = async () => { |
| 103 | + try { |
| 104 | + setIsCreatingCustomer(true); |
| 105 | + const existingCustomer = await searchCustomer(subscriptionSearchCustomer); |
| 106 | + if (existingCustomer != null) { |
| 107 | + setCustomer(existingCustomer); |
| 108 | + } else { |
| 109 | + const newCustomer = await createCustomer(subscriptionNewCustomer); |
| 110 | + setCustomer(newCustomer); |
| 111 | + } |
| 112 | + } catch (error) { |
| 113 | + setCustomerDataError(true); |
| 114 | + } finally { |
| 115 | + setIsCreatingCustomer(false); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + if (Boolean(deploymentId) && !customer) { |
| 120 | + initializeCustomer(); |
| 121 | + } |
| 122 | + }, [deploymentId]); |
| 123 | + |
| 124 | + return ( |
| 125 | + <SimpleSubscriptionContext.Provider value={{ |
| 126 | + admin, |
| 127 | + customer, |
| 128 | + products, |
| 129 | + subscriptionProducts, |
| 130 | + isCreatingCustomer, |
| 131 | + customerDataError, |
| 132 | + subscriptions, |
| 133 | + subscriptionDataLoaded, |
| 134 | + subscriptionDataError, |
| 135 | + checkoutLinkDataLoaded, |
| 136 | + checkoutLinkDataError, |
| 137 | + subscriptionProductsLoading, |
| 138 | + }}> |
| 139 | + {props.children} |
| 140 | + </SimpleSubscriptionContext.Provider> |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +export const useSimpleSubscriptionContext = () => useContext(SimpleSubscriptionContext); |
0 commit comments