From 4af5ba8a6d98e85c79974bd3ed091505bdd0bd87 Mon Sep 17 00:00:00 2001 From: pramod mahajan Date: Sat, 16 Aug 2025 09:43:11 +0530 Subject: [PATCH] added upgraded subscription functionality --- src/components/Tenanat/EditProfile.jsx | 2 +- src/components/Tenanat/SubScription.jsx | 29 ++- .../Tenanat/SubScriptionHistory.jsx | 218 +++++++++--------- src/components/Tenanat/TenantForm.jsx | 2 +- src/hooks/useTenant.js | 36 ++- src/repositories/TenantRepository.jsx | 1 + 6 files changed, 172 insertions(+), 116 deletions(-) diff --git a/src/components/Tenanat/EditProfile.jsx b/src/components/Tenanat/EditProfile.jsx index 98af6f45..9b186344 100644 --- a/src/components/Tenanat/EditProfile.jsx +++ b/src/components/Tenanat/EditProfile.jsx @@ -173,7 +173,7 @@ const EditProfile = ({ TenantId,onClose }) => {
- +
diff --git a/src/components/Tenanat/SubScription.jsx b/src/components/Tenanat/SubScription.jsx index 0fae1adb..1006e5b3 100644 --- a/src/components/Tenanat/SubScription.jsx +++ b/src/components/Tenanat/SubScription.jsx @@ -1,5 +1,5 @@ import React, { useState, useEffect } from "react"; -import { useAddSubscription, useSubscriptionPlan } from "../../hooks/useTenant"; +import { useAddSubscription, useSubscriptionPlan, useUpgradeSubscription } from "../../hooks/useTenant"; import SegmentedControl from "./SegmentedControl"; import { useFormContext } from "react-hook-form"; import { CONSTANT_TEXT } from "../../utils/constants"; @@ -11,13 +11,14 @@ const SubScription = ({ onSubmitSubScription, onNext }) => { const [frequency, setFrequency] = useState(2); const [selectedPlanId, setSelectedPlanId] = useState(null); const selectedTenant = useSelector( - (store) => store.globalVariables.currentTenant?.data + (store) => store.globalVariables.currentTenant ); const naviget = useNavigate(); const { data: plans = [], isError, isLoading, + error:subscriptionGettingError } = useSubscriptionPlan(frequency); const { @@ -35,6 +36,11 @@ const SubScription = ({ onSubmitSubScription, onNext }) => { } = useAddSubscription(() => { naviget("/tenants") }); + const { + mutate : updgradeSubscription,isPending : upgrading + } = useUpgradeSubscription(()=>{ + naviget("/tenants") + }) const handleSubscriptionSubmit = async () => { const isValid = await trigger([ "planId", @@ -48,11 +54,19 @@ const SubScription = ({ onSubmitSubScription, onNext }) => { if (isValid) { const payload = getValues(); // onSubmitSubScription(payload); - const subscriptionPayload = { ...payload, tenantId: selectedTenant.id }; - AddSubScription(subscriptionPayload); + let subscriptionPayload = null; + + if(selectedTenant?.operationMode === 1){ + subscriptionPayload = { planId : payload.planId,currencyId :payload.currencyId,maxUsers:payload.maxUsers, tenantId: selectedTenant?.data?.id } + updgradeSubscription(subscriptionPayload) + }else{ + subscriptionPayload = {...payload,tenantId: selectedTenant?.data?.id} + AddSubScription(subscriptionPayload); + } } }; + const handlePlanSelection = (plan) => { setSelectedPlanId(plan.id); setValue("planId", plan.id); @@ -61,7 +75,8 @@ const SubScription = ({ onSubmitSubScription, onNext }) => { }; const selectedPlan = plans.find((p) => p.id === selectedPlanId); - + if(isLoading) return
Loading....
+ if(isError) return
{subscriptionGettingError?.message}
return (
@@ -208,9 +223,9 @@ const SubScription = ({ onSubmitSubScription, onNext }) => { onClick={handleSubscriptionSubmit} className="btn btn-sm btn-primary" type="button" - disabled={isPending} + disabled={isPending || upgrading} > - {isPending ? "Please Wait..." : "Submit"} + {isPending || upgrading ? "Please Wait..." : "Submit"}
diff --git a/src/components/Tenanat/SubScriptionHistory.jsx b/src/components/Tenanat/SubScriptionHistory.jsx index 9747e87a..2aa0a32d 100644 --- a/src/components/Tenanat/SubScriptionHistory.jsx +++ b/src/components/Tenanat/SubScriptionHistory.jsx @@ -64,83 +64,85 @@ const SubScriptionHistory = ({ tenantId }) => { if (percentage < 80) return "warning"; return "danger"; }; - const SubscriptionColumns = [ - { - key: "createdAt", - label: "Date", - getValue: (e) => formatUTCToLocalTime(e?.createdAt), - align: "text-start", - }, - { - key: "frequency", - label: "Type", - getValue: (e) => { - switch (e.frequency) { - case 1: - return "Monthly"; - case 3: - return "Quarterly"; - case 12: - return "Yearly"; - default: - return "N/A"; - } - }, - align: "text-start", - }, - { - key: "price", - label: "Amount", - getValue: (e) => ( - <> - {e.currency?.symbol || "₹"} {e.price} - - ), - align: "text-end", - }, - { - key: "submittedBy", - label: "Submitted By", - getValue: (e) => - `${e.createdBy?.firstName ?? ""} ${ - e.createdBy?.lastName ?? "" - }`.trim() || "N/A", - customRender: (e) => ( -
- - - {`${e.createdBy?.firstName ?? ""} ${ - e.createdBy?.lastName ?? "" - }`.trim() || "N/A"} - -
- ), - align: "text-start", - }, - { - key: "action", - label: "Action", - customRender: (e) => ( - - ), - align: "text-center", - }, - ]; + // const SubscriptionColumns = [ + // { + // key: "createdAt", + // label: "Date", + // getValue: (e) => formatUTCToLocalTime(e?.createdAt), + // align: "text-start", + // }, + // { + // key: "frequency", + // label: "Type", + // getValue: (e) => { + // switch (e.frequency) { + // case 1: + // return "Monthly"; + // case 3: + // return "Quarterly"; + // case 12: + // return "Yearly"; + // default: + // return "N/A"; + // } + // }, + // align: "text-start", + // }, + // { + // key: "price", + // label: "Amount", + // getValue: (e) => ( + // <> + // {e.currency?.symbol || "₹"} {e.price} + // + // ), + // align: "text-end", + // }, + // { + // key: "submittedBy", + // label: "Submitted By", + // getValue: (e) => + // `${e.createdBy?.firstName ?? ""} ${ + // e.createdBy?.lastName ?? "" + // }`.trim() || "N/A", + // customRender: (e) => ( + //
+ // + // + // {`${e.createdBy?.firstName ?? ""} ${ + // e.createdBy?.lastName ?? "" + // }`.trim() || "N/A"} + // + //
+ // ), + // align: "text-start", + // }, + // { + // key: "action", + // label: "Action", + // customRender: (e) => ( + // + // ), + // align: "text-center", + // }, + // ]; return ( -
+
-

Active Subscription

+
+
Active Subscription
+
@@ -176,37 +178,45 @@ const SubScriptionHistory = ({ tenantId }) => {
-
- - - - - - - - - - - - {data?.subscriptionHistery?.map((item) => ( - - - - - - - - ))} - -
DateTypeAmountPlan NameAction
{formatUTCToLocalTime(item.createdAt)}{SUBSCRIPTION_PLAN_FREQUENCIES[item.frequency] || "N/A"} - {item.currency?.symbol} - {item.price} - {item.planName} - -
- - +
+
+
+
History
+
+ + + + + + + + + + + + + {data?.subscriptionHistery?.map((item) => ( + + + + + + + + ))} + +
DateTypeAmountPlan NameAction
{formatUTCToLocalTime(item.createdAt)} + {SUBSCRIPTION_PLAN_FREQUENCIES[item.frequency] || "N/A"} + + + {" "} + {item.currency?.symbol} + {item.price} + + {item.planName} + +
); diff --git a/src/components/Tenanat/TenantForm.jsx b/src/components/Tenanat/TenantForm.jsx index a491b6ad..b7849dd6 100644 --- a/src/components/Tenanat/TenantForm.jsx +++ b/src/components/Tenanat/TenantForm.jsx @@ -36,7 +36,7 @@ useEffect(() => { } } else { // Default: no tenant selected - setActiveTab(0); + return ; } }, [HasSelectedCurrentTenant,activeTab]); diff --git a/src/hooks/useTenant.js b/src/hooks/useTenant.js index d2903740..0a54d78a 100644 --- a/src/hooks/useTenant.js +++ b/src/hooks/useTenant.js @@ -72,7 +72,6 @@ export const useSubscriptionPlan=(freq)=>{ return useQuery({ queryKey:['SubscriptionPlan',freq], queryFn:async()=>{ - console.log("call") const res = await TenantRepository.getSubscriptionPlan(freq); return res.data; } @@ -124,12 +123,43 @@ export const useAddSubscription =(onSuccessCallback)=>{ return res.data; }, onSuccess:(data,variables)=>{ + const {tenantId} = variables; showToast("Tenant Plan Added SuccessFully","success") - queryClient.invalidateQueries({queryKey:["Tenants"]}); + queryClient.invalidateQueries({queryKey:["Tenant",tenantId]}) if(onSuccessCallback) onSuccessCallback() }, onError:(error)=>{ showToast(error.response.message || error.message || `Something went wrong`,"error") } }) -} \ No newline at end of file +} + +export const useUpgradeSubscription = (onSuccessCallback) => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async (subscriptionPayload) => { + const res = await TenantRepository.upgradeSubscription(subscriptionPayload); + return res.data; + }, + onSuccess: (data, variables) => { + const { tenantId } = variables; + showToast("Tenant Plan Upgraded Successfully", "success"); + + // Refetch tenant details + queryClient.invalidateQueries({ queryKey: ["Tenant", tenantId] }); + queryClient.invalidateQueries({ queryKey: ["Tenants"] }); + + if (onSuccessCallback) onSuccessCallback(); + }, + onError: (error) => { + showToast( + error?.response?.message || + error?.response?.data?.errors || + error.message || + "Something went wrong", + "error" + ); + } + }); +}; diff --git a/src/repositories/TenantRepository.jsx b/src/repositories/TenantRepository.jsx index 82ce5c0b..59995cca 100644 --- a/src/repositories/TenantRepository.jsx +++ b/src/repositories/TenantRepository.jsx @@ -16,4 +16,5 @@ export const TenantRepository = { updateTenantDetails :(id,data)=> api.put(`/api/Tenant/edit/${id}`,data), addSubscription: (data) => api.post("/api/Tenant/add-subscription", data), + upgradeSubscription :(data)=> api.put("/api/Tenant/update-subscription",data) };