update Ui

This commit is contained in:
pramod mahajan 2025-08-25 11:58:50 +05:30
parent fe87ebc859
commit 207c2820bf

View File

@ -4,9 +4,7 @@ import { useDispatch } from "react-redux";
import { setCurrentTenant } from "../../slices/globalVariablesSlice"; import { setCurrentTenant } from "../../slices/globalVariablesSlice";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { formatUTCToLocalTime } from "../../utils/dateUtils"; import { formatUTCToLocalTime } from "../../utils/dateUtils";
import { MANAGE_TENANTS, SUBSCRIPTION_PLAN_FREQUENCIES } from "../../utils/constants"; import { SUBSCRIPTION_PLAN_FREQUENCIES } from "../../utils/constants";
import { useHasAnyPermission } from "../../hooks/useExpense";
const SubScriptionHistory = ({ tenantId }) => { const SubScriptionHistory = ({ tenantId }) => {
const { data, isLoading, isError, error } = useTenantDetails(tenantId); const { data, isLoading, isError, error } = useTenantDetails(tenantId);
@ -15,10 +13,8 @@ const SubScriptionHistory = ({ tenantId }) => {
useEffect(() => { useEffect(() => {
if (data) { if (data) {
// Tenant exists set operationMode: 1
dispatch(setCurrentTenant({ operationMode: 1, data })); dispatch(setCurrentTenant({ operationMode: 1, data }));
} else { } else {
// No tenant yet set operationMode: 0
dispatch(setCurrentTenant({ operationMode: 0, data: null })); dispatch(setCurrentTenant({ operationMode: 0, data: null }));
} }
}, [data, dispatch]); }, [data, dispatch]);
@ -32,7 +28,6 @@ const SubScriptionHistory = ({ tenantId }) => {
const plan = data?.currentPlan; const plan = data?.currentPlan;
// No subscription plan yet show "Add Subscription" button
if (!plan) { if (!plan) {
return ( return (
<div className="text-center"> <div className="text-center">
@ -46,97 +41,109 @@ const SubScriptionHistory = ({ tenantId }) => {
); );
} }
// Subscription plan exists show details // Format dates
const start = plan?.startDate ? new Date(plan.startDate) : null;
const end = plan?.endDate ? new Date(plan.endDate) : null;
const today = new Date(); const today = new Date();
const start = new Date(plan.startDate);
const end = new Date(plan.endDate);
const totalDays = Math.ceil((end - start) / (1000 * 60 * 60 * 24)); const totalDays = start && end ? Math.ceil((end - start) / (1000 * 60 * 60 * 24)) : 0;
const daysLeft = Math.max( const daysLeft = end ? Math.max(0, Math.ceil((end - today) / (1000 * 60 * 60 * 24))) : 0;
0,
Math.ceil((end - today) / (1000 * 60 * 60 * 24))
);
const percentage = Math.min(
100,
Math.round(((totalDays - daysLeft) / totalDays) * 100)
);
const getProgressVariant = () => {
if (percentage < 50) return "success";
if (percentage < 80) return "warning";
return "danger";
};
const SubscriptionColumns = [ const SubscriptionColumns = [
{ {
key: "createdAt", key: "createdAt",
label: "Date", label: "Date",
getValue: (e) => formatUTCToLocalTime(e?.createdAt), getValue: (e) => formatUTCToLocalTime(e?.createdAt),
align: "text-start", align: "text-start",
}, },
{ {
key: "frequency", key: "frequency",
label: "Type", label: "Type",
getValue: (e) => getValue: (e) => SUBSCRIPTION_PLAN_FREQUENCIES[e.frequency] || "N/A",
SUBSCRIPTION_PLAN_FREQUENCIES[e.frequency] || "N/A", align: "text-start",
align: "text-start", },
}, {
{ key: "price",
key: "price", label: "Amount",
label: "Amount", getValue: (e) => (
getValue: (e) => ( <span className="d-inline-block px-3">
<span className="d-inline-block px-3"> {e.currency?.symbol || "₹"} {e.price}
{e.currency?.symbol || "₹"} {e.price} </span>
</span> ),
), align: "text-start",
align: "text-end", },
}, {
{ key: "planName",
key: "planName", label: "Plan Name",
label: "Plan Name", getValue: (e) => (
getValue: (e) => ( <span className="d-inline-block ps-4">{e.planName}</span>
<span className="d-inline-block ps-4"> ),
{e.planName} align: "text-start",
</span> },
), {
align: "text-start", key: "action",
}, label: "Action",
getValue: (e) => (
{ <div className="dropdown text-center">
key: "action", <button
label: "Action", className="btn btn-icon btn-sm dropdown-toggle hide-arrow"
getValue: (e) => ( data-bs-toggle="dropdown"
<i >
className="bx bx-cloud-download" <i className="bx bx-dots-vertical-rounded"></i>
onClick={() => console.log("Download clicked for", e.id)} </button>
role="button" <div className="dropdown-menu dropdown-menu-end">
/> {/* View */}
), <button
align: "text-center", className="dropdown-item py-1"
}, onClick={() => navigate(`/employee/${e.id}`)}
]; >
<i className="bx bx-detail bx-sm"></i> View
</button>
{/* Download */}
<button
className="dropdown-item py-1"
onClick={() => console.log("Download clicked for", e.id)}
>
<i className="bx bx-cloud-download bx-sm"></i> Download
</button>
</div>
</div>
),
align: "text-center",
},
];
return ( return (
<div className=" p-2"> <div className="p-2">
<div className="text-start mb-3"> <div className="text-start mb-3">
<div className="divider text-start my-1"> {/* Active subscription */}
<div className="divider-text">Active Subscription</div> <div className="divider text-start my-1">
</div> <div className="divider-text">Active Subscription</div>
</div>
<div className="row"> <div className="row">
<div className="col-12 col-sm-8 border rounded p-3 shadow-sm"> <div className="col-12 col-sm-8 border rounded p-3 shadow-sm">
<p className="text-primary fw-bold m-0">{plan.planName || "N/A"}</p> <p className="text-primary fw-bold m-0 fs-4">
{plan.planName || "N/A"}
</p>
{plan.description && (
<div className="col rounded-2 justify-content-start p-0">
<p className="m-0">{plan.description}</p>
</div>
)}
<div className="d-flex justify-content-between align-items-end mt-2"> <div className="d-flex justify-content-between align-items-end mt-2">
<div> <div>
<h3 className="m-0"> <h3 className="m-0">
{plan.currency?.symbol} {plan.price} {plan.currency?.symbol || "₹"} {plan.price}
</h3> </h3>
<small className="text-muted">{plan.description}</small> <small className="text-muted">
{SUBSCRIPTION_PLAN_FREQUENCIES[plan.frequency] || ""}
</small>
</div> </div>
<div> <div>
<button <button
className="btn btn-sm btn-success" className="btn btn-sm btn-primary"
onClick={handleUpgradePlan} onClick={handleUpgradePlan}
> >
Upgrade Plan Upgrade Plan
@ -144,52 +151,52 @@ const SubScriptionHistory = ({ tenantId }) => {
</div> </div>
</div> </div>
<div className="mt-3"> <div className="mt-3 small text-muted">
{/* Progress bar placeholder */} <div className="d-flex justify-content-between">
<div className="d-flex justify-content-between small text-muted mt-1"> <span>
<span>{totalDays - daysLeft} days used</span> Active Since :{" "}
<span>{daysLeft} days left</span> {plan.startDate ? formatUTCToLocalTime(plan.startDate) : "N/A"} ({daysLeft} days left)
</span>
</div>
<div className="text-start mt-1">
Ends on : {plan.endDate ? formatUTCToLocalTime(plan.endDate) : "N/A"}
</div> </div>
</div> </div>
</div>
</div>
<div className="text-end mt-1 small text-muted"> {/* History */}
Ends on {end.toLocaleDateString()} <div className="divider text-start my-3">
</div> <div className="divider-text">
<i className="bx bx-history"></i> <small>History</small>
</div> </div>
</div> </div>
<div className="text-start my-2">
</div>
<div className="divider text-start my-1">
<div className="divider-text"> <i className="bx bx-history"></i> <small>History</small></div>
</div>
<table className="table border-top dataTable text-nowrap align-middle"> <table className="table border-top dataTable text-nowrap align-middle">
<thead className="align-middle"> <thead className="align-middle">
<tr> <tr>
{SubscriptionColumns.map((col) => ( {SubscriptionColumns.map((col) => (
<th key={col.key} className={col.align}> <th key={col.key} className={col.align}>
{col.label} {col.label}
</th> </th>
))} ))}
</tr> </tr>
</thead> </thead>
<tbody className="align-middle"> <tbody className="align-middle">
{data?.subscriptionHistery {data?.subscriptionHistery
?.slice() ?.slice()
.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)) // latest first .sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt))
.map((item) => ( .map((item) => (
<tr key={item.id}> <tr key={item.id}>
{SubscriptionColumns.map((col) => ( {SubscriptionColumns.map((col) => (
<td key={col.key} className={col.align}> <td key={col.key} className={col.align}>
{col.getValue ? col.getValue(item) : item[col.key] || "N/A"} {col.getValue ? col.getValue(item) : item[col.key] || "N/A"}
</td> </td>
))} ))}
</tr> </tr>
))} ))}
</tbody> </tbody>
</table> </table>
</div> </div>
</div> </div>
); );