|
| 1 | +import { getStaticUrl } from "@dashboard/config"; |
| 2 | +import { type GiftCardPaymentMethodDetailsFragment } from "@dashboard/graphql"; |
| 3 | +import { Box, Text } from "@saleor/macaw-ui-next"; |
| 4 | +import { Gift } from "lucide-react"; |
| 5 | +import { FormattedMessage } from "react-intl"; |
| 6 | + |
| 7 | +import icon from "./saleor-gift-card.svg"; |
| 8 | + |
| 9 | +const MASKED_GROUP = "****"; |
| 10 | +const BRAND_LOGO_SIZE = 26; |
| 11 | + |
| 12 | +const getPaymentMethodIconUrl = (fileName: string) => |
| 13 | + `${getStaticUrl()}payment-methods/${fileName}.svg`; |
| 14 | + |
| 15 | +const SaleorGiftCardIcon = () => ( |
| 16 | + <Box |
| 17 | + width={6} |
| 18 | + height={4} |
| 19 | + borderRadius={2} |
| 20 | + borderStyle="solid" |
| 21 | + borderWidth={1} |
| 22 | + borderColor="default1" |
| 23 | + display="flex" |
| 24 | + alignItems="center" |
| 25 | + justifyContent="center" |
| 26 | + > |
| 27 | + <img src={icon} alt="Saleor Gift Card" title="Saleor Gift Card" /> |
| 28 | + </Box> |
| 29 | +); |
| 30 | + |
| 31 | +const ExternalGiftCardIcon = ({ brand }: { brand: string | null }) => { |
| 32 | + if (brand) { |
| 33 | + return ( |
| 34 | + <img |
| 35 | + src={getPaymentMethodIconUrl("gift-card")} |
| 36 | + alt={brand} |
| 37 | + title={brand} |
| 38 | + height={BRAND_LOGO_SIZE} |
| 39 | + /> |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + return <Gift size={16} />; |
| 44 | +}; |
| 45 | + |
| 46 | +const formatGiftCardCode = (lastChars: string | null): string | null => { |
| 47 | + if (!lastChars) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + return `${MASKED_GROUP} ${lastChars}`; |
| 52 | +}; |
| 53 | + |
| 54 | +interface GiftCardPaymentMethodProps { |
| 55 | + details: GiftCardPaymentMethodDetailsFragment; |
| 56 | +} |
| 57 | + |
| 58 | +export const GiftCardPaymentMethod = ({ details }: GiftCardPaymentMethodProps) => { |
| 59 | + const formattedCode = formatGiftCardCode(details.lastChars); |
| 60 | + |
| 61 | + if (details.isSaleorGiftcard) { |
| 62 | + return ( |
| 63 | + <Box display="flex" alignItems="center" gap={2}> |
| 64 | + <SaleorGiftCardIcon /> |
| 65 | + <Text size={2} color="default2"> |
| 66 | + Saleor Gift Card |
| 67 | + </Text> |
| 68 | + {formattedCode && ( |
| 69 | + <Text size={2} color="default2"> |
| 70 | + <code>{formattedCode}</code> |
| 71 | + </Text> |
| 72 | + )} |
| 73 | + </Box> |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return ( |
| 78 | + <Box display="flex" alignItems="center" gap={2}> |
| 79 | + <ExternalGiftCardIcon brand={details.brand} /> |
| 80 | + <Text size={2} color="default2"> |
| 81 | + {details.brand ?? <FormattedMessage defaultMessage="Gift card" id="4X0ZI8" />} |
| 82 | + </Text> |
| 83 | + {formattedCode && ( |
| 84 | + <Text size={2} color="default2"> |
| 85 | + <code>{formattedCode}</code> |
| 86 | + </Text> |
| 87 | + )} |
| 88 | + </Box> |
| 89 | + ); |
| 90 | +}; |
0 commit comments