2026-02-10 16:35:19 +05:30
|
|
|
import { useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { useDispatch } from 'react-redux';
|
|
|
|
|
import {
|
|
|
|
|
ChangeAppExpDateData,
|
|
|
|
|
changeHeightforExpDate,
|
|
|
|
|
getAppSubscriptionDate,
|
|
|
|
|
} from './Features/BookingScreen/BookingData/BookingData.js';
|
2026-02-11 12:45:21 +05:30
|
|
|
import { sendSms } from './Features/Payment/PaymentDetails/PaymentDetails.js';
|
|
|
|
|
import { getSession } from './Services/Others.js';
|
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
2026-02-13 13:58:58 +05:30
|
|
|
const subDirectory = import.meta.env.ENV_BASE_URL;
|
2026-02-10 16:35:19 +05:30
|
|
|
const useSubscriptionManager = (sessionData, logout) => {
|
|
|
|
|
const dispatch = useDispatch();
|
2026-02-11 12:45:21 +05:30
|
|
|
const navigate = useNavigate();
|
2026-02-10 16:35:19 +05:30
|
|
|
const prevRemainingDays = useRef(null);
|
|
|
|
|
const [remainingDays, setRemainingDays] = useState(null);
|
2026-02-11 12:45:21 +05:30
|
|
|
const [extendModel, setExtendModel] = useState(false);
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
|
logout();
|
|
|
|
|
setExtendModel(false)
|
2026-02-10 16:35:19 +05:30
|
|
|
|
2026-02-11 12:45:21 +05:30
|
|
|
}
|
2026-02-13 13:58:58 +05:30
|
|
|
|
2026-02-11 12:45:21 +05:30
|
|
|
function formatDate(date) {
|
|
|
|
|
const year = date.getFullYear();
|
|
|
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
|
|
|
|
const day = String(date.getDate()).padStart(2, '0');
|
|
|
|
|
const hours = String(date.getHours()).padStart(2, '0');
|
|
|
|
|
const minutes = String(date.getMinutes()).padStart(2, '0');
|
|
|
|
|
const seconds = String(date.getSeconds()).padStart(2, '0');
|
|
|
|
|
const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
|
|
|
|
|
|
|
|
|
|
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${milliseconds}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 13:58:58 +05:30
|
|
|
|
|
|
|
|
|
2026-02-10 16:35:19 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (!sessionData) return;
|
|
|
|
|
|
|
|
|
|
const fetchExpDate = async () => {
|
|
|
|
|
const { CompId, AppId, BranchId, UserType } = sessionData;
|
|
|
|
|
|
|
|
|
|
const res = await dispatch(
|
|
|
|
|
getAppSubscriptionDate({ CompId, BranchId, AppId })
|
|
|
|
|
).unwrap();
|
|
|
|
|
|
|
|
|
|
const expData = res?.data?.data?.[0];
|
|
|
|
|
if (!expData) return logout();
|
|
|
|
|
|
|
|
|
|
if (prevRemainingDays.current !== expData.RemainingDays) {
|
|
|
|
|
prevRemainingDays.current = expData.RemainingDays;
|
|
|
|
|
|
|
|
|
|
setRemainingDays(expData.RemainingDays);
|
|
|
|
|
dispatch(ChangeAppExpDateData(expData));
|
|
|
|
|
dispatch(changeHeightforExpDate(expData.RemainingDays));
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
expData.RemainingDays < 1 &&
|
|
|
|
|
expData.RemainingHours < 1 &&
|
|
|
|
|
expData.RemainingMinutes < 1 &&
|
|
|
|
|
expData.RemainingSeconds < 1 &&
|
|
|
|
|
UserType !== 'Super Admin'
|
|
|
|
|
) {
|
2026-02-11 18:10:44 +05:30
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(UserType !== 'Super Admin' ||
|
|
|
|
|
UserType !== 'Super Admin User') && (expData?.PlanType?.toLowerCase() == "extend expired")
|
|
|
|
|
) {
|
|
|
|
|
console.log('Subscription expired, logging out...');
|
|
|
|
|
alert('Subscription expired');
|
|
|
|
|
logout();
|
|
|
|
|
}
|
|
|
|
|
else if (
|
|
|
|
|
(UserType !== 'Super Admin' ||
|
|
|
|
|
UserType !== 'Super Admin User') && (expData?.PlanType?.toLowerCase() == "plan expired")
|
|
|
|
|
) {
|
|
|
|
|
setExtendModel(true)
|
|
|
|
|
}
|
2026-02-10 16:35:19 +05:30
|
|
|
}
|
2026-02-11 18:10:44 +05:30
|
|
|
|
2026-02-10 16:35:19 +05:30
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fetchExpDate();
|
|
|
|
|
const interval = setInterval(fetchExpDate, 60 * 60 * 1000);
|
|
|
|
|
return () => clearInterval(interval);
|
|
|
|
|
}, [sessionData]);
|
|
|
|
|
|
2026-02-13 13:58:58 +05:30
|
|
|
return { remainingDays, handleCancel,extendModel,setExtendModel };
|
2026-02-10 16:35:19 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useSubscriptionManager;
|