Skip to content

Commit efd7f25

Browse files
author
FalkWolsky
committed
Changing to SimpleSubscriptionContextProvider for Homepage
1 parent 15cc8a3 commit efd7f25

File tree

3 files changed

+157
-11
lines changed

3 files changed

+157
-11
lines changed

client/packages/lowcoder/src/pages/ApplicationV2/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import AppEditor from "../editor/AppEditor";
7474
import { set } from "lodash";
7575
import { fetchDeploymentIdAction } from "@lowcoder-ee/redux/reduxActions/configActions";
7676
import { getDeploymentId } from "@lowcoder-ee/redux/selectors/configSelectors";
77-
import { SubscriptionContextProvider } from '@lowcoder-ee/util/context/SubscriptionContext';
77+
import { SimpleSubscriptionContextProvider } from '@lowcoder-ee/util/context/SimpleSubscriptionContext';
7878

7979
const TabLabel = styled.div`
8080
font-weight: 500;
@@ -224,7 +224,7 @@ export default function ApplicationHome() {
224224

225225
return (
226226
<DivStyled>
227-
<SubscriptionContextProvider>
227+
<SimpleSubscriptionContextProvider>
228228
<Layout
229229
sections={[
230230
{
@@ -364,7 +364,7 @@ export default function ApplicationHome() {
364364

365365
]}
366366
/>
367-
</SubscriptionContextProvider>
367+
</SimpleSubscriptionContextProvider>
368368
</DivStyled>
369369
);
370370
}

client/packages/lowcoder/src/pages/setting/subscriptions/index.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { SubscriptionContextProvider } from '@lowcoder-ee/util/context/Subscript
1111

1212
export const Subscription = () => {
1313
return (
14-
<Switch>
15-
<Route path={SUBSCRIPTION_DETAIL} component={SubscriptionDetail} exact />
16-
<Route path={SUBSCRIPTION_INFO} component={SubscriptionInfo} exact />
17-
<Route path={SUBSCRIPTION_SUCCESS} component={SubscriptionSuccess} exact />
18-
<Route path={SUBSCRIPTION_CANCEL} component={SubscriptionCancel} exact />
19-
<Route path={SUBSCRIPTION_ERROR} component={SubscriptionError} exact />
20-
<Route path={SUBSCRIPTION_SETTING} component={SubscriptionSetting} exact />
21-
</Switch>
14+
<SubscriptionContextProvider>
15+
<Switch>
16+
<Route path={SUBSCRIPTION_DETAIL} component={SubscriptionDetail} exact />
17+
<Route path={SUBSCRIPTION_INFO} component={SubscriptionInfo} exact />
18+
<Route path={SUBSCRIPTION_SUCCESS} component={SubscriptionSuccess} exact />
19+
<Route path={SUBSCRIPTION_CANCEL} component={SubscriptionCancel} exact />
20+
<Route path={SUBSCRIPTION_ERROR} component={SubscriptionError} exact />
21+
<Route path={SUBSCRIPTION_SETTING} component={SubscriptionSetting} exact />
22+
</Switch>
23+
</SubscriptionContextProvider>
2224
);
2325
};
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)