Skip to content

Commit dd1784b

Browse files
author
FalkWolsky
committed
Getting Workspace User Count
1 parent f9616aa commit dd1784b

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

client/packages/lowcoder/src/api/subscriptionApi.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import Api from "api/api";
22
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";
3-
import { useSelector } from "react-redux";
3+
import { useDispatch, useSelector } from "react-redux";
44
import { getUser, getCurrentUser } from "redux/selectors/usersSelectors";
5-
import { useEffect, useState } from "react";
5+
import { useEffect, useState} from "react";
66
import { calculateFlowCode } from "./apiUtils";
77
import { getDeploymentId } from "@lowcoder-ee/redux/selectors/configSelectors";
8+
import { fetchOrgUsersAction } from "redux/reduxActions/orgActions";
9+
import { getOrgUsers } from "redux/selectors/orgSelectors";
10+
import { AppState } from "@lowcoder-ee/redux/reducers";
811

912
// Interfaces
1013
export interface CustomerAddress {
@@ -355,6 +358,28 @@ export const getCustomerPortalSession = async (customerId: string) => {
355358

356359
// Hooks
357360

361+
export const useOrgUserCount = (orgId: string) => {
362+
const dispatch = useDispatch();
363+
const orgUsers = useSelector((state: AppState) => getOrgUsers(state)); // Use selector to get orgUsers from state
364+
const [userCount, setUserCount] = useState<number>(0);
365+
366+
useEffect(() => {
367+
// Dispatch action to fetch organization users
368+
if (orgId) {
369+
dispatch(fetchOrgUsersAction(orgId));
370+
}
371+
}, [dispatch, orgId]);
372+
373+
useEffect(() => {
374+
// Update user count when orgUsers state changes
375+
if (orgUsers && orgUsers.length > 0) {
376+
setUserCount(orgUsers.length);
377+
}
378+
}, [orgUsers]);
379+
380+
return userCount;
381+
};
382+
358383
export const InitializeSubscription = () => {
359384
const [customer, setCustomer] = useState<StripeCustomer | null>(null);
360385
const [isCreatingCustomer, setIsCreatingCustomer] = useState<boolean>(false); // Track customer creation
@@ -364,7 +389,6 @@ export const InitializeSubscription = () => {
364389
const [subscriptionDataError, setSubscriptionDataError] = useState<boolean>(false);
365390
const [checkoutLinkDataLoaded, setCheckoutLinkDataLoaded] = useState<boolean>(false);
366391
const [checkoutLinkDataError, setCheckoutLinkDataError] = useState<boolean>(false);
367-
368392
const [products, setProducts] = useState<Product[]>([
369393
{
370394
pricingType: "Monthly, per User",
@@ -401,14 +425,17 @@ export const InitializeSubscription = () => {
401425
},
402426
]);
403427

428+
404429
const user = useSelector(getUser);
405430
const currentUser = useSelector(getCurrentUser);
406431
const deploymentId = useSelector(getDeploymentId);
407432
const currentOrg = user.orgs.find(org => org.id === user.currentOrgId);
408433
const orgID = user.currentOrgId;
409434
const domain = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
410-
// const hostIdenticator = "lowcoder-test";
411435
const admin = user.orgRoleMap.get(orgID) === "admin" ? "admin" : "member";
436+
const dispatch = useDispatch();
437+
438+
const userCount = useOrgUserCount(orgID);
412439

413440
const subscriptionSearchCustomer: LowcoderSearchCustomer = {
414441
hostname: domain,
@@ -470,8 +497,10 @@ export const InitializeSubscription = () => {
470497

471498
useEffect(() => {
472499
const prepareCheckout = async () => {
473-
if (subscriptionDataLoaded) {
500+
if (subscriptionDataLoaded && userCount > 0) { // Ensure user count is available
474501
try {
502+
console.log("Total Users in Organization:", userCount);
503+
475504
const updatedProducts = await Promise.all(
476505
products.map(async (product) => {
477506
const matchingSubscription = subscriptions.find(
@@ -486,7 +515,8 @@ export const InitializeSubscription = () => {
486515
subscriptionId: matchingSubscription.id.substring(4),
487516
};
488517
} else {
489-
const checkoutLink = await createCheckoutLink(customer!, product.accessLink, 1);
518+
// Use the user count to set the quantity for checkout link
519+
const checkoutLink = await createCheckoutLink(customer!, product.accessLink, userCount);
490520
return {
491521
...product,
492522
activeSubscription: false,
@@ -505,7 +535,7 @@ export const InitializeSubscription = () => {
505535
};
506536

507537
prepareCheckout();
508-
}, [subscriptionDataLoaded]);
538+
}, [subscriptionDataLoaded, userCount]);
509539

510540
return {
511541
customer,
@@ -552,8 +582,7 @@ export const CheckSubscriptions = () => {
552582
const deploymentId = useSelector(getDeploymentId);
553583
const orgID = user.currentOrgId;
554584
const domain = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
555-
// const hostIdenticator = "lowcoder-test";
556-
585+
557586
const subscriptionSearchCustomer: LowcoderSearchCustomer = {
558587
hostname: domain,
559588
hostId: deploymentId,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ export function SubscriptionSetting() {
4848
try {
4949
const productData = await getProducts();
5050
setSubscriptionProducts(productData);
51-
console.log("productData", productData);
51+
// console.log("productData", productData);
5252
} catch (err) {
5353
setError("Failed to fetch product.");
54-
console.error(err);
54+
// console.error(err);
5555
} finally {
5656
setLoading(false);
5757
}

0 commit comments

Comments
 (0)