|
| 1 | +import { useForm } from "@tanstack/react-form" |
| 2 | +import { Button } from "#/components/ui/button" |
| 3 | +import { Input } from "#/components/ui/input" |
| 4 | +import { Switch } from "#/components/ui/switch" |
| 5 | +import { Label } from "#/components/ui/label" |
| 6 | +import { |
| 7 | + Select, |
| 8 | + SelectContent, |
| 9 | + SelectItem, |
| 10 | + SelectTrigger, |
| 11 | + SelectValue, |
| 12 | +} from "#/components/ui/select" |
| 13 | +import type { CreatePityRuleInput } from "#/lib/types/lottery" |
| 14 | +import type { LotteryTier } from "#/lib/types/lottery" |
| 15 | + |
| 16 | +interface PityRuleFormProps { |
| 17 | + tiers: LotteryTier[] |
| 18 | + defaultValues?: Partial<CreatePityRuleInput> |
| 19 | + onSubmit: (values: CreatePityRuleInput) => void | Promise<void> |
| 20 | + onCancel?: () => void |
| 21 | + isPending?: boolean |
| 22 | + submitLabel?: string |
| 23 | + disableGuaranteeTier?: boolean |
| 24 | +} |
| 25 | + |
| 26 | +export function PityRuleForm({ |
| 27 | + tiers, |
| 28 | + defaultValues, |
| 29 | + onSubmit, |
| 30 | + onCancel, |
| 31 | + isPending, |
| 32 | + submitLabel = "Create", |
| 33 | + disableGuaranteeTier, |
| 34 | +}: PityRuleFormProps) { |
| 35 | + const form = useForm({ |
| 36 | + defaultValues: { |
| 37 | + guaranteeTierId: defaultValues?.guaranteeTierId ?? "", |
| 38 | + hardPityThreshold: defaultValues?.hardPityThreshold ?? 90, |
| 39 | + softPityStartAt: defaultValues?.softPityStartAt ?? (null as number | null), |
| 40 | + softPityWeightIncrement: defaultValues?.softPityWeightIncrement ?? (null as number | null), |
| 41 | + isActive: defaultValues?.isActive ?? true, |
| 42 | + }, |
| 43 | + onSubmit: async ({ value }) => { |
| 44 | + const input: CreatePityRuleInput = { |
| 45 | + guaranteeTierId: value.guaranteeTierId, |
| 46 | + hardPityThreshold: value.hardPityThreshold, |
| 47 | + softPityStartAt: value.softPityStartAt, |
| 48 | + softPityWeightIncrement: value.softPityWeightIncrement, |
| 49 | + isActive: value.isActive, |
| 50 | + } |
| 51 | + await onSubmit(input) |
| 52 | + }, |
| 53 | + }) |
| 54 | + |
| 55 | + return ( |
| 56 | + <form |
| 57 | + onSubmit={(e) => { |
| 58 | + e.preventDefault() |
| 59 | + e.stopPropagation() |
| 60 | + form.handleSubmit() |
| 61 | + }} |
| 62 | + className="space-y-4" |
| 63 | + > |
| 64 | + <div className="grid gap-4 sm:grid-cols-2"> |
| 65 | + <form.Field |
| 66 | + name="guaranteeTierId" |
| 67 | + validators={{ |
| 68 | + onChange: ({ value }) => |
| 69 | + !value ? "Must select a tier" : undefined, |
| 70 | + }} |
| 71 | + > |
| 72 | + {(field) => ( |
| 73 | + <div className="space-y-2"> |
| 74 | + <Label>Guarantee Tier *</Label> |
| 75 | + <Select |
| 76 | + value={field.state.value} |
| 77 | + onValueChange={field.handleChange} |
| 78 | + disabled={disableGuaranteeTier} |
| 79 | + > |
| 80 | + <SelectTrigger className="w-full"> |
| 81 | + <SelectValue placeholder="Select tier..." /> |
| 82 | + </SelectTrigger> |
| 83 | + <SelectContent> |
| 84 | + {tiers.map((tier) => ( |
| 85 | + <SelectItem key={tier.id} value={tier.id}> |
| 86 | + {tier.name} |
| 87 | + </SelectItem> |
| 88 | + ))} |
| 89 | + </SelectContent> |
| 90 | + </Select> |
| 91 | + {field.state.meta.errors.length > 0 && ( |
| 92 | + <p className="text-sm text-destructive">{field.state.meta.errors[0]}</p> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + )} |
| 96 | + </form.Field> |
| 97 | + |
| 98 | + <form.Field |
| 99 | + name="hardPityThreshold" |
| 100 | + validators={{ |
| 101 | + onChange: ({ value }) => |
| 102 | + value <= 0 ? "Must be positive" : undefined, |
| 103 | + }} |
| 104 | + > |
| 105 | + {(field) => ( |
| 106 | + <div className="space-y-2"> |
| 107 | + <Label htmlFor={field.name}>Hard Pity Threshold *</Label> |
| 108 | + <Input |
| 109 | + id={field.name} |
| 110 | + type="number" |
| 111 | + min={1} |
| 112 | + value={field.state.value} |
| 113 | + onBlur={field.handleBlur} |
| 114 | + onChange={(e) => field.handleChange(Number(e.target.value))} |
| 115 | + placeholder="e.g. 90" |
| 116 | + /> |
| 117 | + <p className="text-xs text-muted-foreground"> |
| 118 | + Guaranteed after this many pulls without the tier. |
| 119 | + </p> |
| 120 | + {field.state.meta.errors.length > 0 && ( |
| 121 | + <p className="text-sm text-destructive">{field.state.meta.errors[0]}</p> |
| 122 | + )} |
| 123 | + </div> |
| 124 | + )} |
| 125 | + </form.Field> |
| 126 | + |
| 127 | + <form.Field name="softPityStartAt"> |
| 128 | + {(field) => ( |
| 129 | + <div className="space-y-2"> |
| 130 | + <Label htmlFor={field.name}>Soft Pity Start</Label> |
| 131 | + <Input |
| 132 | + id={field.name} |
| 133 | + type="number" |
| 134 | + min={1} |
| 135 | + value={field.state.value ?? ""} |
| 136 | + onBlur={field.handleBlur} |
| 137 | + onChange={(e) => |
| 138 | + field.handleChange(e.target.value ? Number(e.target.value) : null) |
| 139 | + } |
| 140 | + placeholder="e.g. 74" |
| 141 | + /> |
| 142 | + <p className="text-xs text-muted-foreground"> |
| 143 | + Start boosting weight after this many pulls. |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </form.Field> |
| 148 | + |
| 149 | + <form.Field name="softPityWeightIncrement"> |
| 150 | + {(field) => ( |
| 151 | + <div className="space-y-2"> |
| 152 | + <Label htmlFor={field.name}>Weight Increment</Label> |
| 153 | + <Input |
| 154 | + id={field.name} |
| 155 | + type="number" |
| 156 | + min={1} |
| 157 | + value={field.state.value ?? ""} |
| 158 | + onBlur={field.handleBlur} |
| 159 | + onChange={(e) => |
| 160 | + field.handleChange(e.target.value ? Number(e.target.value) : null) |
| 161 | + } |
| 162 | + placeholder="e.g. 60" |
| 163 | + /> |
| 164 | + <p className="text-xs text-muted-foreground"> |
| 165 | + Extra weight added per pull after soft pity starts. |
| 166 | + </p> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </form.Field> |
| 170 | + </div> |
| 171 | + |
| 172 | + <form.Field name="isActive"> |
| 173 | + {(field) => ( |
| 174 | + <div className="flex items-center gap-3"> |
| 175 | + <Switch |
| 176 | + id={field.name} |
| 177 | + checked={field.state.value} |
| 178 | + onCheckedChange={(checked) => field.handleChange(checked === true)} |
| 179 | + /> |
| 180 | + <Label htmlFor={field.name}>Active</Label> |
| 181 | + </div> |
| 182 | + )} |
| 183 | + </form.Field> |
| 184 | + |
| 185 | + <div className="flex items-center gap-2"> |
| 186 | + <form.Subscribe selector={(s) => s.canSubmit}> |
| 187 | + {(canSubmit) => ( |
| 188 | + <Button type="submit" size="sm" disabled={!canSubmit || isPending}> |
| 189 | + {isPending ? "Saving..." : submitLabel} |
| 190 | + </Button> |
| 191 | + )} |
| 192 | + </form.Subscribe> |
| 193 | + {onCancel && ( |
| 194 | + <Button type="button" variant="outline" size="sm" onClick={onCancel}> |
| 195 | + Cancel |
| 196 | + </Button> |
| 197 | + )} |
| 198 | + </div> |
| 199 | + </form> |
| 200 | + ) |
| 201 | +} |
0 commit comments