import React from 'react';
import PlanCard from '../modules/PlanCard';
import { useRouter } from 'next/router';
import { Plan } from '../../types';
import { useBillingContext } from '@/billing-ui/context/BillingContext';
interface PricingTableProps {
onSubscribe: (plan: Plan) => void;
onContactUs: () => void;
onTrial: (planCode: string) => void;
plans: Plan[];
}
const PricingTable: React.FC<PricingTableProps> = ({ onSubscribe, onContactUs, onTrial, plans }) => {
const { subscription } = useBillingContext();
const router = useRouter();
const handleContactUs = () => {
onContactUs();
};
const handleSubscribe = (plan: Plan) => {
if (onSubscribe) {
onSubscribe(plan);
} else {
router.push({
pathname: '/checkout',
query: {
plan: JSON.stringify(plan),
},
});
}
};
return (
<div className="flex flex-shrink justify-center bg-background w-full h-full px-6 gap-4 my-6">
{plans.length > 0 && subscription ? (
plans.map((plan) => (
<div key={plan.name} className='w-full flex-shrink'>
<PlanCard
plan={plan}
onContactUs={handleContactUs}
onSubscribe={() => handleSubscribe(plan)}
onTrial={onTrial}
/>
</div>
))
) : (
<div className="flex items-center justify-center w-full h-full">
<div className="animate-spin rounded-full h-16 w-16 border-t-4 border-b-4 border-primary"></div>
</div>
)}
</div>
);
};
export default PricingTable;