Files
vf_react/boilerplate-chakra-pro-main/utils/helpers.ts
2025-11-22 11:41:56 +08:00

140 lines
3.2 KiB
TypeScript

import { Database } from '@/types/types_db';
type Price = Database['public']['Tables']['prices']['Row'];
export const getURL = (path?: string) => {
let url =
process?.env?.NEXT_PUBLIC_SITE_URL ?? // Set this to your site URL in production env.
process?.env?.NEXT_PUBLIC_VERCEL_URL ?? // Automatically set by Vercel.
'http://localhost:3000/';
// Make sure to include `https://` when not localhost.
url = url.includes('http') ? url : `https://${url}`;
// Make sure to including trailing `/`.
url = url.charAt(url.length - 1) === '/' ? url : `${url}/`;
if (path) {
path = path.replace(/^\/+/, '');
// Concatenate the URL and the path.
return path ? `${url}/${path}` : url;
}
return url;
};
export const postData = async ({
url,
data
}: {
url: string;
data?: { price: Price };
}) => {
console.log('posting,', url, data);
const res = await fetch(url, {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
credentials: 'same-origin',
body: JSON.stringify(data)
});
if (!res.ok) {
console.log('Error in postData', { url, data, res });
throw Error(res.statusText);
}
return res.json();
};
export const toDateTime = (secs: number) => {
var t = new Date('1970-01-01T00:30:00Z'); // Unix epoch start.
t.setSeconds(secs);
return t;
};
export const calculateTrialEndUnixTimestamp = (
trialPeriodDays: number | null | undefined
) => {
// Check if trialPeriodDays is null, undefined, or less than 2 days
if (
trialPeriodDays === null ||
trialPeriodDays === undefined ||
trialPeriodDays < 2
) {
return undefined;
}
const currentDate = new Date(); // Current date and time
const trialEnd = new Date(
currentDate.getTime() + (trialPeriodDays + 1) * 24 * 60 * 60 * 1000
); // Add trial days
return Math.floor(trialEnd.getTime() / 1000); // Convert to Unix timestamp in seconds
};
const toastKeyMap: { [key: string]: string[] } = {
status: ['status', 'status_description'],
error: ['error', 'error_description']
};
const getToastRedirect = (
path: string,
toastType: string,
toastName: string,
toastDescription: string = '',
disableButton: boolean = false,
arbitraryParams: string = ''
): string => {
const [nameKey, descriptionKey] = toastKeyMap[toastType];
let redirectPath = `${path}?${nameKey}=${encodeURIComponent(toastName)}`;
if (toastDescription) {
redirectPath += `&${descriptionKey}=${encodeURIComponent(
toastDescription
)}`;
}
if (disableButton) {
redirectPath += `&disable_button=true`;
}
if (arbitraryParams) {
redirectPath += `&${arbitraryParams}`;
}
return redirectPath;
};
export const getStatusRedirect = (
path: string,
statusName: string,
statusDescription: string = '',
disableButton: boolean = false,
arbitraryParams: string = ''
) =>
getToastRedirect(
path,
'status',
statusName,
statusDescription,
disableButton,
arbitraryParams
);
export const getErrorRedirect = (
path: string,
errorName: string,
errorDescription: string = '',
disableButton: boolean = false,
arbitraryParams: string = ''
) =>
getToastRedirect(
path,
'error',
errorName,
errorDescription,
disableButton,
arbitraryParams
);