import React from 'react'; import Select from 'react-select'; import { useTranslation } from 'react-i18next'; import { FabModal } from '../base/fab-modal'; import { PaymentMethod, PaymentSchedule } from '../../models/payment-schedule'; import PaymentScheduleAPI from '../../api/payment-schedule'; interface UpdatePaymentMeanModalProps { isOpen: boolean, toggleModal: () => void, onError: (message: string) => void, afterSuccess: () => void, paymentSchedule: PaymentSchedule } /** * Option format, expected by react-select * @see https://github.com/JedWatson/react-select */ type selectOption = { value: PaymentMethod, label: string }; /** * Component to allow the member to change his payment mean for the given payment schedule (e.g. from card to transfer) */ export const UpdatePaymentMeanModal: React.FC = ({ isOpen, toggleModal, onError, afterSuccess, paymentSchedule }) => { const { t } = useTranslation('admin'); const [paymentMean, setPaymentMean] = React.useState(); /** * Convert all payment means to the react-select format */ const buildOptions = (): Array => { return Object.keys(PaymentMethod).filter(pm => PaymentMethod[pm] !== PaymentMethod.Card).map(pm => { return { value: PaymentMethod[pm], label: t(`app.admin.update_payment_mean_modal.method_${pm}`) }; }); }; /** * When the payment mean is changed in the select, update the state */ const handleMeanSelected = (option: selectOption): void => { setPaymentMean(option.value); }; /** * When the user clicks on the update button, update the default payment mean for the given payment schedule */ const handlePaymentMeanUpdate = (): void => { PaymentScheduleAPI.update({ id: paymentSchedule.id, payment_method: paymentMean }).then(() => { afterSuccess(); }).catch(error => { onError(error.message); }); }; return ( {t('app.admin.update_payment_mean_modal.update_info')} ); };