|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Button, |
| 4 | + Card, |
| 5 | + Divider, |
| 6 | + Text, |
| 7 | + TextInput, |
| 8 | + NumberInput, |
| 9 | + Title, |
| 10 | + Modal, |
| 11 | + Anchor, |
| 12 | + CopyButton, |
| 13 | + Group, |
| 14 | +} from '@mantine/core'; |
| 15 | +import { useForm } from '@mantine/form'; |
| 16 | +import { showNotification } from '@mantine/notifications'; |
| 17 | +import { IconAlertCircle } from '@tabler/icons-react'; |
| 18 | +import React, { useState } from 'react'; |
| 19 | +import { PostInvoiceLinkRequest, PostInvoiceLinkResponse } from '@common/types/stripe'; |
| 20 | +import FullScreenLoader from '@ui/components/AuthContext/LoadingScreen'; |
| 21 | + |
| 22 | +interface StripeCreateLinkPanelProps { |
| 23 | + createLink: (payload: PostInvoiceLinkRequest) => Promise<PostInvoiceLinkResponse>; |
| 24 | + isLoading: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export const StripeCreateLinkPanel: React.FC<StripeCreateLinkPanelProps> = ({ |
| 28 | + createLink, |
| 29 | + isLoading, |
| 30 | +}) => { |
| 31 | + const [modalOpened, setModalOpened] = useState(false); |
| 32 | + const [returnedLink, setReturnedLink] = useState<string | null>(null); |
| 33 | + |
| 34 | + const form = useForm({ |
| 35 | + initialValues: { |
| 36 | + invoiceId: '', |
| 37 | + invoiceAmountUsd: 100, |
| 38 | + contactName: '', |
| 39 | + contactEmail: '', |
| 40 | + }, |
| 41 | + validate: { |
| 42 | + invoiceId: (value) => (value.length < 1 ? 'Invoice ID is required' : null), |
| 43 | + invoiceAmountUsd: (value) => (value < 0.5 ? 'Amount must be at least $0.50' : null), |
| 44 | + contactName: (value) => (value.length < 1 ? 'Contact Name is required' : null), |
| 45 | + contactEmail: (value) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? null : 'Invalid email'), |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + const handleSubmit = async (values: typeof form.values) => { |
| 50 | + try { |
| 51 | + const response = await createLink(values); |
| 52 | + setReturnedLink(response.link); |
| 53 | + setModalOpened(true); |
| 54 | + form.reset(); |
| 55 | + } catch (err) { |
| 56 | + showNotification({ |
| 57 | + title: 'Error', |
| 58 | + message: 'Failed to create payment link. Please try again or contact support.', |
| 59 | + color: 'red', |
| 60 | + icon: <IconAlertCircle size={16} />, |
| 61 | + }); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + if (isLoading) { |
| 66 | + return <FullScreenLoader />; |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Box mt="xl" mb="xl"> |
| 71 | + <Title order={2} mb="sm"> |
| 72 | + Create a Payment Link |
| 73 | + </Title> |
| 74 | + <form onSubmit={form.onSubmit(handleSubmit)}> |
| 75 | + <TextInput |
| 76 | + label="Invoice ID" |
| 77 | + placeholder="ACM100" |
| 78 | + description="Make sure the Invoice ID is prefixed with a unique string for your group to avoid processing delays." |
| 79 | + {...form.getInputProps('invoiceId')} |
| 80 | + required |
| 81 | + /> |
| 82 | + <NumberInput |
| 83 | + label="Invoice Amount" |
| 84 | + leftSectionPointerEvents="none" |
| 85 | + leftSection={<Text>$</Text>} |
| 86 | + placeholder="100" |
| 87 | + min={0.5} |
| 88 | + {...form.getInputProps('invoiceAmountUsd')} |
| 89 | + required |
| 90 | + /> |
| 91 | + <TextInput |
| 92 | + label="Invoice Recipient Name" |
| 93 | + placeholder="John Doe" |
| 94 | + {...form.getInputProps('contactName')} |
| 95 | + required |
| 96 | + /> |
| 97 | + <TextInput |
| 98 | + label="Invoice Recipient Email" |
| 99 | + |
| 100 | + {...form.getInputProps('contactEmail')} |
| 101 | + required |
| 102 | + /> |
| 103 | + |
| 104 | + <Button type="submit" fullWidth mt="md"> |
| 105 | + Create Link |
| 106 | + </Button> |
| 107 | + </form> |
| 108 | + |
| 109 | + <Modal |
| 110 | + opened={modalOpened} |
| 111 | + size="xl" |
| 112 | + onClose={() => setModalOpened(false)} |
| 113 | + title="Payment Link Created!" |
| 114 | + > |
| 115 | + {returnedLink && ( |
| 116 | + <Box mt="md"> |
| 117 | + <Group> |
| 118 | + <Text style={{ color: 'blue' }}>{returnedLink}</Text> |
| 119 | + <CopyButton value={returnedLink}> |
| 120 | + {({ copied, copy }) => ( |
| 121 | + <Button color={copied ? 'teal' : 'blue'} onClick={copy}> |
| 122 | + {copied ? 'Copied!' : 'Copy Link'} |
| 123 | + </Button> |
| 124 | + )} |
| 125 | + </CopyButton> |
| 126 | + </Group> |
| 127 | + <Text mt="sm">Provide this link to your billing contact for payment.</Text> |
| 128 | + </Box> |
| 129 | + )} |
| 130 | + </Modal> |
| 131 | + </Box> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +export default StripeCreateLinkPanel; |
0 commit comments