forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchForm.tsx
More file actions
154 lines (140 loc) · 5.2 KB
/
Copy pathBatchForm.tsx
File metadata and controls
154 lines (140 loc) · 5.2 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from 'react'
import { useFieldArray, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
import { Layers, Plus, Trash2, 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 { isValidStellarAddress } from '@/lib/stellar'
import type { BatchPaymentFormValues } from '@/types'
import { MAX_BATCH_RECIPIENTS } from '@/lib/stellar'
// ─── Validation schema ────────────────────────────────────────────────────────
const recipientSchema = z.object({
destinationAddress: z
.string()
.min(1, 'Required')
.refine(isValidStellarAddress, 'Invalid Stellar address'),
amount: z
.string()
.min(1, 'Required')
.refine((v) => !isNaN(parseFloat(v)) && parseFloat(v) > 0, 'Must be > 0'),
memo: z.string().max(28).optional().default(''),
})
const batchSchema = z.object({
assetCode: z.string().min(1),
recipients: z
.array(recipientSchema)
.min(1, 'Add at least one recipient')
.max(MAX_BATCH_RECIPIENTS, `Batch supports at most ${MAX_BATCH_RECIPIENTS} recipients`),
})
interface BatchFormProps {
onSubmit: (values: BatchPaymentFormValues) => void
isLoading?: boolean
supportedAssets: { code: string; name: string }[]
defaultValues?: Partial<BatchPaymentFormValues>
}
export function BatchForm({
onSubmit,
isLoading = false,
supportedAssets,
defaultValues,
}: BatchFormProps) {
const {
register,
handleSubmit,
control,
watch,
formState: { errors, isValid },
} = useForm<BatchPaymentFormValues>({
resolver: zodResolver(batchSchema),
mode: 'onChange',
defaultValues: {
assetCode: 'XLM',
recipients: [{ destinationAddress: '', amount: '', memo: '' }],
...defaultValues,
},
})
const { fields, append, remove } = useFieldArray({ control, name: 'recipients' })
const recipients = watch('recipients')
const assetCode = watch('assetCode')
const total = recipients.reduce((sum, r) => sum + (parseFloat(r.amount) || 0), 0)
const assetOptions = supportedAssets.map((a) => ({ value: a.code, label: a.code }))
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Layers size={18} className="text-stellar-400" />
Batch / Split Payment
</CardTitle>
</CardHeader>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5" noValidate>
<Select label="Asset" options={assetOptions} fullWidth {...register('assetCode')} />
<div className="space-y-3">
{fields.map((field, index) => (
<div
key={field.id}
className="flex items-start gap-2 p-3 rounded-xl border border-navy-600/50 bg-navy-900/30"
>
<div className="flex-1 space-y-2">
<Input
placeholder="G... recipient address"
fullWidth
error={errors.recipients?.[index]?.destinationAddress?.message}
{...register(`recipients.${index}.destinationAddress` as const)}
/>
<Input
placeholder="Amount"
type="number"
min="0"
step="0.0000001"
fullWidth
error={errors.recipients?.[index]?.amount?.message}
{...register(`recipients.${index}.amount` as const)}
/>
</div>
<button
type="button"
aria-label={`Remove recipient ${index + 1}`}
onClick={() => remove(index)}
disabled={fields.length <= 1}
className="mt-1 p-2 rounded-lg text-slate-500 hover:text-danger-400 hover:bg-danger-500/10 disabled:opacity-30 disabled:cursor-not-allowed transition-all"
>
<Trash2 size={15} />
</button>
</div>
))}
{typeof errors.recipients?.message === 'string' && (
<p className="text-xs text-danger-400">{errors.recipients.message}</p>
)}
<Button
type="button"
variant="outline"
size="sm"
icon={<Plus size={14} />}
onClick={() => append({ destinationAddress: '', amount: '', memo: '' })}
disabled={fields.length >= MAX_BATCH_RECIPIENTS}
>
Add recipient
</Button>
</div>
<div className="flex items-center justify-between px-1 pt-1 border-t border-navy-700/40">
<span className="text-sm text-slate-400">Total ({fields.length} recipients)</span>
<span className="text-lg font-bold text-white tabular-nums">
{total.toFixed(4)} {assetCode}
</span>
</div>
<Button
type="submit"
fullWidth
size="lg"
loading={isLoading}
disabled={!isValid}
iconRight={<ChevronRight size={18} />}
>
Review Batch
</Button>
</form>
</Card>
)
}