forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriptionForm.tsx
More file actions
151 lines (138 loc) · 4.4 KB
/
Copy pathSubscriptionForm.tsx
File metadata and controls
151 lines (138 loc) · 4.4 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
import React from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { z } from 'zod'
import { Repeat, ChevronRight, User } 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 { SubscriptionFormValues } from '@/types'
// ─── Validation schema ────────────────────────────────────────────────────────
const todayISODate = () => new Date().toISOString().slice(0, 10)
const subscriptionSchema = z.object({
destinationAddress: z
.string()
.min(1, 'Recipient address is required')
.refine(isValidStellarAddress, 'Invalid Stellar address'),
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'),
interval: z.enum(['daily', 'weekly', 'monthly', 'yearly']),
startDate: z
.string()
.min(1, 'Start date is required')
.refine((v) => v >= todayISODate(), 'Start date cannot be in the past'),
memo: z.string().max(28, 'Memo must be ≤ 28 characters').optional().default(''),
})
interface SubscriptionFormProps {
onSubmit: (values: SubscriptionFormValues) => void
isLoading?: boolean
supportedAssets: { code: string; name: string }[]
defaultValues?: Partial<SubscriptionFormValues>
}
const intervalOptions = [
{ value: 'daily', label: 'Daily' },
{ value: 'weekly', label: 'Weekly' },
{ value: 'monthly', label: 'Monthly' },
{ value: 'yearly', label: 'Yearly' },
]
export function SubscriptionForm({
onSubmit,
isLoading = false,
supportedAssets,
defaultValues,
}: SubscriptionFormProps) {
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<SubscriptionFormValues>({
resolver: zodResolver(subscriptionSchema),
mode: 'onChange',
defaultValues: {
destinationAddress: '',
assetCode: 'XLM',
amount: '',
interval: 'monthly',
startDate: todayISODate(),
memo: '',
...defaultValues,
},
})
const assetOptions = supportedAssets.map((a) => ({ value: a.code, label: a.code }))
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Repeat size={18} className="text-stellar-400" />
New Recurring Payment
</CardTitle>
</CardHeader>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-5" noValidate>
<Input
label="Recipient Stellar Address"
placeholder="G..."
error={errors.destinationAddress?.message}
leftIcon={<User size={15} />}
fullWidth
{...register('destinationAddress')}
/>
<div className="grid grid-cols-2 gap-3">
<Select
label="Asset"
options={assetOptions}
fullWidth
{...register('assetCode')}
/>
<Input
label="Amount per payment"
placeholder="0.0000"
type="number"
min="0"
step="0.0000001"
error={errors.amount?.message}
fullWidth
{...register('amount')}
/>
</div>
<div className="grid grid-cols-2 gap-3">
<Select
label="Repeats"
options={intervalOptions}
fullWidth
{...register('interval')}
/>
<Input
label="Start date"
type="date"
min={todayISODate()}
error={errors.startDate?.message}
fullWidth
{...register('startDate')}
/>
</div>
<Input
label="Memo (optional)"
placeholder="Payment reference, ID, or note"
hint="Up to 28 characters. Applied to every scheduled run."
error={errors.memo?.message}
fullWidth
{...register('memo')}
/>
<Button
type="submit"
fullWidth
size="lg"
loading={isLoading}
disabled={!isValid}
iconRight={<ChevronRight size={18} />}
>
Review Subscription
</Button>
</form>
</Card>
)
}