import type { FC } from '../../lib/teact/teact'; import React, { memo, useEffect, useMemo } from '../../lib/teact/teact'; import { withGlobal } from '../../global'; import type { ApiInvoice, ApiShippingAddress, ApiWebDocument } from '../../api/types'; import type { Price } from '../../types'; import { selectTabState } from '../../global/selectors'; import useFlag from '../../hooks/useFlag'; import useLang from '../../hooks/useLang'; 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 = { prices?: Price[]; shippingPrices: any; tipAmount?: number; totalAmount?: number; currency?: string; info?: { shippingAddress?: ApiShippingAddress; phone?: string; name?: string; }; photo?: ApiWebDocument; text?: string; title?: string; credentialsTitle?: string; shippingMethod?: string; }; const ReceiptModal: FC = ({ isOpen, onClose, prices, shippingPrices, tipAmount, totalAmount, currency, info, photo, text, title, credentialsTitle, shippingMethod, }) => { const lang = useLang(); const [isModalOpen, openModal, closeModal] = useFlag(); useEffect(() => { if (isOpen) { openModal(); } }, [isOpen, openModal]); const checkoutInfo = useMemo(() => { return getCheckoutInfo(credentialsTitle, info, shippingMethod); }, [info, shippingMethod, credentialsTitle]); const invoice: ApiInvoice = useMemo(() => { return { mediaType: 'invoice', photo, text: text!, title: title!, amount: totalAmount!, currency: currency!, }; }, [currency, photo, text, title, totalAmount]); return (

{lang('PaymentReceipt')}

); }; export default memo(withGlobal( (global): StateProps => { const { receipt } = selectTabState(global).payment; const { currency, prices, info, totalAmount, credentialsTitle, shippingPrices, shippingMethod, photo, text, title, tipAmount, } = (receipt || {}); return { currency, prices, info, tipAmount, totalAmount, credentialsTitle, shippingPrices, shippingMethod, photo, text, title, }; }, )(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, }; }