import { memo, useEffect } from 'react';
import { Form } from 'antd';
import { useTranslation } from 'react-i18next';
import { useResponsive } from '@/hooks/useSize';
import OrderSummary from './components/OrderSummary';
import PayMethodCard from './components/PayMethodCard';
import PlanCard from './components/PlanCard';
import UserInfo from './components/UserInfo';
import { useAction } from './useAction';
import { useService } from './useService';
import type { Plan } from './useService';
const PRICING_FORM_ID = 'pricing-form';
export interface PlanSelectorProps {
value?: string;
onChange?: (planId: string) => void;
plans: Plan[];
isMobile: boolean;
}
function PlanSelector({ value, onChange, plans, isMobile }: PlanSelectorProps) {
return (
{plans.map((plan) => (
onChange?.(plan.id)}
/>
))}
);
}
export interface PayMethodSelectorProps {
value?: string;
onChange?: (payType: string) => void;
payMethods: API.PayTypeItem[];
isMobile: boolean;
}
function PayMethodSelector({ value, onChange, payMethods, isMobile }: PayMethodSelectorProps) {
return (
*]:flex-[1_1_calc(50%-1rem)] [&>*]:max-w-[calc(50%-1rem)]'
}
>
{payMethods.map((item) => (
onChange?.(item.payType)}
/>
))}
);
}
const Pricing = memo(() => {
const { t } = useTranslation();
const { isMobile } = useResponsive();
const [form] = Form.useForm<{ planId: string; payMethod: string }>();
const { plans, payMethods } = useService();
const { handlePayNow } = useAction();
const planId = Form.useWatch('planId', form);
const payMethod = Form.useWatch('payMethod', form);
const selectedPlan = plans.find((p) => p.id === planId) ?? null;
const selectedPayMethod = payMethod ?? null;
useEffect(() => {
if (plans.length === 0) return;
const defaultPlan = plans.find((p) => p.isDefault);
if (defaultPlan) {
form.setFieldsValue({ planId: defaultPlan.id });
}
}, [plans, form]);
const onFinish = (values: { planId: string; payMethod: string }) => {
const plan = plans.find((p) => p.id === values.planId);
if (!plan || !values.payMethod) return;
handlePayNow(plan, values.payMethod);
};
return (
{t('pages.pricing.title')}
);
});
Pricing.displayName = 'Pricing';
export default Pricing;