import { Price, Radio } from '@akinon/next/components'; import { useLocalization } from '@akinon/next/hooks'; import { useSetSavedCardInstallmentOptionMutation } from '../redux/api'; import { useEffect, useState } from 'react'; import { SavedCard } from '../redux/reducer'; import { InstallmentTexts } from '../types'; const defaultTranslations = { payments: 'Payments', per_month: 'Per Month', total: 'Total' }; type SavedCardInstallmentsProps = { installmentOptions: any[]; translations?: InstallmentTexts; error: any; }; const SavedCardInstallments = ({ installmentOptions = [], translations, error }: SavedCardInstallmentsProps) => { const { t } = useLocalization(); const [installmentOption, setInstallmentOption] = useState(null); const [setInstallment] = useSetSavedCardInstallmentOptionMutation(); useEffect(() => { if ( installmentOptions.length > 0 && installmentOptions[0]?.pk !== installmentOption ) { const firstOptionPk = installmentOptions[0].pk; setInstallment({ installment: firstOptionPk }); setInstallmentOption(firstOptionPk); } }, [installmentOptions, installmentOption, setInstallment]); if (installmentOptions.length === 0) { return (
{t('checkout.payment.installment_options.description')}
); } return (
{installmentOptions.map((option, index) => ( ))}
{translations?.payments || defaultTranslations.payments} {translations?.per_month || defaultTranslations.per_month} {translations?.total || defaultTranslations.total}
{ setInstallment({ installment: option.pk }); setInstallmentOption(option.pk); }} data-testid={`saved-card-installment-${index}`} > {option.label}
{error && (
{String(error.message)}
)}
); }; export default SavedCardInstallments;