forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateRequestForm.tsx
More file actions
114 lines (104 loc) · 3.17 KB
/
Copy pathCreateRequestForm.tsx
File metadata and controls
114 lines (104 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
import { FileText, ChevronRight } from 'lucide-react'
import { Input, Select } from '@/components/ui/Input'
import { Button } from '@/components/ui/Button'
import { Card, CardHeader, CardTitle } from '@/components/ui/Card'
import type { PaymentRequestFormValues } from '@/types'
const requestSchema = z.object({
assetCode: z.string().min(1),
amount: z
.string()
.min(1, 'Amount is required')
.refine((v) => !isNaN(parseFloat(v)) && parseFloat(v) > 0, 'Amount must be a positive number'),
memo: z.string().max(28, 'Memo must be ≤ 28 characters').optional().default(''),
expiresInHours: z
.string()
.optional()
.default('')
.refine((v) => v === '' || (!isNaN(parseFloat(v)) && parseFloat(v) > 0), 'Must be a positive number'),
})
interface CreateRequestFormProps {
onSubmit: (values: PaymentRequestFormValues) => void
isLoading?: boolean
supportedAssets: { code: string; name: string }[]
}
const expiryOptions = [
{ value: '', label: 'Never' },
{ value: '1', label: '1 hour' },
{ value: '24', label: '1 day' },
{ value: '168', label: '1 week' },
]
export function CreateRequestForm({
onSubmit,
isLoading = false,
supportedAssets,
}: CreateRequestFormProps) {
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<PaymentRequestFormValues>({
resolver: zodResolver(requestSchema),
mode: 'onChange',
defaultValues: {
assetCode: 'XLM',
amount: '',
memo: '',
expiresInHours: '',
},
})
const assetOptions = supportedAssets.map((a) => ({ value: a.code, label: a.code }))
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<FileText size={18} className="text-stellar-400" />
Request a Payment
</CardTitle>
</CardHeader>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5" noValidate>
<div className="grid grid-cols-2 gap-3">
<Select label="Asset" options={assetOptions} fullWidth {...register('assetCode')} />
<Input
label="Amount"
placeholder="0.0000"
type="number"
min="0"
step="0.0000001"
error={errors.amount?.message}
fullWidth
{...register('amount')}
/>
</div>
<Input
label="Memo (optional)"
placeholder="Invoice #, reason for payment..."
hint="Up to 28 characters, shown to the payer."
error={errors.memo?.message}
fullWidth
{...register('memo')}
/>
<Select
label="Expires"
options={expiryOptions}
hint="After this time the request can no longer be paid."
fullWidth
{...register('expiresInHours')}
/>
<Button
type="submit"
fullWidth
size="lg"
loading={isLoading}
disabled={!isValid}
iconRight={<ChevronRight size={18} />}
>
Create Request
</Button>
</form>
</Card>
)
}