import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useMemo } from '../../lib/teact/teact'; import { withGlobal } from '../../global'; import type { ApiReceiptRegular, ApiShippingAddress } from '../../api/types'; import { selectTabState } from '../../global/selectors'; import useFlag from '../../hooks/useFlag'; import useLang from '../../hooks/useLang'; import usePrevious from '../../hooks/usePrevious'; import Button from '../ui/Button'; import Modal from '../ui/Modal'; import Checkout from './Checkout'; import './PaymentModal.scss'; export type OwnProps = { isOpen?: boolean; onClose: () => void; }; type StateProps = { receipt?: ApiReceiptRegular; }; const ReceiptModal: FC = ({ isOpen, onClose, receipt, }) => { const lang = useLang(); const [isModalOpen, openModal, closeModal] = useFlag(); useEffect(() => { if (isOpen) { openModal(); } }, [isOpen, openModal]); const prevReceipt = usePrevious(receipt); const renderingReceipt = receipt || prevReceipt; const checkoutInfo = useMemo(() => { if (!renderingReceipt) return undefined; return getCheckoutInfo(renderingReceipt.credentialsTitle, renderingReceipt.info, renderingReceipt.shippingMethod); }, [renderingReceipt]); return ( {renderingReceipt && ( <>

{lang('PaymentReceipt')}

)}
); }; export default memo(withGlobal( (global): StateProps => { const { receipt } = selectTabState(global).payment; return { receipt, }; }, )(ReceiptModal)); function getCheckoutInfo(paymentMethod?: string, info?: { phone?: string; name?: string; shippingAddress?: ApiShippingAddress; }, shippingMethod?: string) { if (!info) { return { paymentMethod }; } const { shippingAddress } = info; const fullAddress = shippingAddress?.streetLine1 ? `${shippingAddress.streetLine1}, ${shippingAddress.city}, ${shippingAddress.countryIso2}` : undefined; const { phone, name } = info; return { paymentMethod, shippingAddress: fullAddress, name, phone, shippingMethod, }; }