|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { useRouter } from 'next/navigation'; |
| 5 | +import { useForm } from 'react-hook-form'; |
| 6 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 7 | +import { z } from 'zod'; |
| 8 | +import { Input } from '@/components/ui/input'; |
| 9 | +import { Button } from '@/components/ui/button'; |
| 10 | +import { Label } from '@/components/ui/label'; |
| 11 | +import { Textarea } from '@/components/ui/textarea'; |
| 12 | +import { createLeague } from '@/actions/leagues/create-league'; |
| 13 | +import { toast } from 'sonner'; |
| 14 | + |
| 15 | +const leagueSchema = z.object({ |
| 16 | + name: z.enum(['BRONZE', 'SILVER', 'GOLD', 'PLATINUM', 'DIAMOND']), |
| 17 | + color: z.enum(['CD7F32', 'C0C0C0', 'FFD700', 'E5E4E2', 'b9f2ff']), |
| 18 | + description: z.string().min(10).max(500), |
| 19 | + xpRequirement: z.number().min(0), |
| 20 | + resetDate: z.string(), |
| 21 | + canBeRelegated: z.boolean(), |
| 22 | + maxPowerUpsPerWeek: z.number().min(1).max(10), |
| 23 | + xpMultiplier: z.number().min(1).max(5), |
| 24 | + maxUsers: z.number().min(10).max(100), |
| 25 | + promotionCount: z.number().min(1), |
| 26 | + relegationCount: z.number().min(1), |
| 27 | + weeklyChallenge: z.string().optional(), |
| 28 | + weeklyChallengeXP: z.number().optional(), |
| 29 | +}); |
| 30 | + |
| 31 | +type LeagueFormData = z.infer<typeof leagueSchema>; |
| 32 | + |
| 33 | +export default function CreateLeagueForm() { |
| 34 | + const router = useRouter(); |
| 35 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 36 | + |
| 37 | + const { |
| 38 | + register, |
| 39 | + handleSubmit, |
| 40 | + formState: { errors }, |
| 41 | + } = useForm<LeagueFormData>({ |
| 42 | + resolver: zodResolver(leagueSchema), |
| 43 | + defaultValues: { |
| 44 | + canBeRelegated: true, |
| 45 | + maxPowerUpsPerWeek: 3, |
| 46 | + xpMultiplier: 1, |
| 47 | + maxUsers: 30, |
| 48 | + promotionCount: 3, |
| 49 | + relegationCount: 5, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const onSubmit = async (data: LeagueFormData) => { |
| 54 | + try { |
| 55 | + setIsSubmitting(true); |
| 56 | + await createLeague(data); |
| 57 | + toast.success('League created successfully'); |
| 58 | + router.push('/admin/leagues/list'); |
| 59 | + } catch (error) { |
| 60 | + toast.error('Failed to create league'); |
| 61 | + console.error('Failed to create league:', error); |
| 62 | + } finally { |
| 63 | + setIsSubmitting(false); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <form onSubmit={handleSubmit(onSubmit)} className="space-y-6"> |
| 69 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 70 | + <div> |
| 71 | + <Label htmlFor="name" className="text-white"> |
| 72 | + League Name |
| 73 | + </Label> |
| 74 | + <select |
| 75 | + id="name" |
| 76 | + {...register('name')} |
| 77 | + className="w-full bg-black-50 text-white border border-black-25 rounded-md p-2" |
| 78 | + > |
| 79 | + <option value="BRONZE">Bronze League</option> |
| 80 | + <option value="SILVER">Silver League</option> |
| 81 | + <option value="GOLD">Gold League</option> |
| 82 | + <option value="PLATINUM">Platinum League</option> |
| 83 | + <option value="DIAMOND">Diamond League</option> |
| 84 | + </select> |
| 85 | + {errors.name && <p className="text-red-500 text-sm mt-1">{errors.name.message}</p>} |
| 86 | + </div> |
| 87 | + |
| 88 | + <div> |
| 89 | + <Label htmlFor="color" className="text-white"> |
| 90 | + League Color |
| 91 | + </Label> |
| 92 | + <select |
| 93 | + id="color" |
| 94 | + {...register('color')} |
| 95 | + className="w-full bg-black-50 text-white border border-black-25 rounded-md p-2" |
| 96 | + > |
| 97 | + <option value="CD7F32">Bronze</option> |
| 98 | + <option value="C0C0C0">Silver</option> |
| 99 | + <option value="FFD700">Gold</option> |
| 100 | + <option value="E5E4E2">Platinum</option> |
| 101 | + <option value="b9f2ff">Diamond</option> |
| 102 | + </select> |
| 103 | + {errors.color && <p className="text-red-500 text-sm mt-1">{errors.color.message}</p>} |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div> |
| 108 | + <Label htmlFor="description" className="text-white"> |
| 109 | + Description |
| 110 | + </Label> |
| 111 | + <Textarea |
| 112 | + id="description" |
| 113 | + {...register('description')} |
| 114 | + className="bg-black-50 text-white border border-black-25" |
| 115 | + placeholder="Enter league description..." |
| 116 | + /> |
| 117 | + {errors.description && ( |
| 118 | + <p className="text-red-500 text-sm mt-1">{errors.description.message}</p> |
| 119 | + )} |
| 120 | + </div> |
| 121 | + |
| 122 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 123 | + <div> |
| 124 | + <Label htmlFor="xpRequirement" className="text-white"> |
| 125 | + XP Requirement |
| 126 | + </Label> |
| 127 | + <Input |
| 128 | + id="xpRequirement" |
| 129 | + type="number" |
| 130 | + {...register('xpRequirement', { valueAsNumber: true })} |
| 131 | + className="bg-black-50 text-white border border-black-25" |
| 132 | + /> |
| 133 | + {errors.xpRequirement && ( |
| 134 | + <p className="text-red-500 text-sm mt-1">{errors.xpRequirement.message}</p> |
| 135 | + )} |
| 136 | + </div> |
| 137 | + |
| 138 | + <div> |
| 139 | + <Label htmlFor="resetDate" className="text-white"> |
| 140 | + Reset Date |
| 141 | + </Label> |
| 142 | + <Input |
| 143 | + id="resetDate" |
| 144 | + type="datetime-local" |
| 145 | + {...register('resetDate')} |
| 146 | + className="bg-black-50 text-white border border-black-25" |
| 147 | + /> |
| 148 | + {errors.resetDate && ( |
| 149 | + <p className="text-red-500 text-sm mt-1">{errors.resetDate.message}</p> |
| 150 | + )} |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + |
| 154 | + <div className="grid grid-cols-1 md:grid-cols-3 gap-6"> |
| 155 | + <div> |
| 156 | + <Label htmlFor="maxPowerUpsPerWeek" className="text-white"> |
| 157 | + Max Power-ups Per Week |
| 158 | + </Label> |
| 159 | + <Input |
| 160 | + id="maxPowerUpsPerWeek" |
| 161 | + type="number" |
| 162 | + {...register('maxPowerUpsPerWeek', { valueAsNumber: true })} |
| 163 | + className="bg-black-50 text-white border border-black-25" |
| 164 | + /> |
| 165 | + {errors.maxPowerUpsPerWeek && ( |
| 166 | + <p className="text-red-500 text-sm mt-1">{errors.maxPowerUpsPerWeek.message}</p> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + |
| 170 | + <div> |
| 171 | + <Label htmlFor="xpMultiplier" className="text-white"> |
| 172 | + XP Multiplier |
| 173 | + </Label> |
| 174 | + <Input |
| 175 | + id="xpMultiplier" |
| 176 | + type="number" |
| 177 | + step="0.1" |
| 178 | + {...register('xpMultiplier', { valueAsNumber: true })} |
| 179 | + className="bg-black-50 text-white border border-black-25" |
| 180 | + /> |
| 181 | + {errors.xpMultiplier && ( |
| 182 | + <p className="text-red-500 text-sm mt-1">{errors.xpMultiplier.message}</p> |
| 183 | + )} |
| 184 | + </div> |
| 185 | + |
| 186 | + <div> |
| 187 | + <Label htmlFor="maxUsers" className="text-white"> |
| 188 | + Max Users |
| 189 | + </Label> |
| 190 | + <Input |
| 191 | + id="maxUsers" |
| 192 | + type="number" |
| 193 | + {...register('maxUsers', { valueAsNumber: true })} |
| 194 | + className="bg-black-50 text-white border border-black-25" |
| 195 | + /> |
| 196 | + {errors.maxUsers && ( |
| 197 | + <p className="text-red-500 text-sm mt-1">{errors.maxUsers.message}</p> |
| 198 | + )} |
| 199 | + </div> |
| 200 | + </div> |
| 201 | + |
| 202 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 203 | + <div> |
| 204 | + <Label htmlFor="promotionCount" className="text-white"> |
| 205 | + Promotion Count |
| 206 | + </Label> |
| 207 | + <Input |
| 208 | + id="promotionCount" |
| 209 | + type="number" |
| 210 | + {...register('promotionCount', { valueAsNumber: true })} |
| 211 | + className="bg-black-50 text-white border border-black-25" |
| 212 | + /> |
| 213 | + {errors.promotionCount && ( |
| 214 | + <p className="text-red-500 text-sm mt-1">{errors.promotionCount.message}</p> |
| 215 | + )} |
| 216 | + </div> |
| 217 | + |
| 218 | + <div> |
| 219 | + <Label htmlFor="relegationCount" className="text-white"> |
| 220 | + Relegation Count |
| 221 | + </Label> |
| 222 | + <Input |
| 223 | + id="relegationCount" |
| 224 | + type="number" |
| 225 | + {...register('relegationCount', { valueAsNumber: true })} |
| 226 | + className="bg-black-50 text-white border border-black-25" |
| 227 | + /> |
| 228 | + {errors.relegationCount && ( |
| 229 | + <p className="text-red-500 text-sm mt-1">{errors.relegationCount.message}</p> |
| 230 | + )} |
| 231 | + </div> |
| 232 | + </div> |
| 233 | + |
| 234 | + <div> |
| 235 | + <div className="flex items-center gap-2 mb-4"> |
| 236 | + <input |
| 237 | + type="checkbox" |
| 238 | + id="canBeRelegated" |
| 239 | + {...register('canBeRelegated')} |
| 240 | + className="bg-black-50 border border-black-25" |
| 241 | + /> |
| 242 | + <Label htmlFor="canBeRelegated" className="text-white"> |
| 243 | + Can be relegated |
| 244 | + </Label> |
| 245 | + </div> |
| 246 | + </div> |
| 247 | + |
| 248 | + <div className="grid grid-cols-1 md:grid-cols-2 gap-6"> |
| 249 | + <div> |
| 250 | + <Label htmlFor="weeklyChallenge" className="text-white"> |
| 251 | + Weekly Challenge |
| 252 | + </Label> |
| 253 | + <Input |
| 254 | + id="weeklyChallenge" |
| 255 | + {...register('weeklyChallenge')} |
| 256 | + className="bg-black-50 text-white border border-black-25" |
| 257 | + placeholder="Optional weekly challenge" |
| 258 | + /> |
| 259 | + {errors.weeklyChallenge && ( |
| 260 | + <p className="text-red-500 text-sm mt-1">{errors.weeklyChallenge.message}</p> |
| 261 | + )} |
| 262 | + </div> |
| 263 | + |
| 264 | + <div> |
| 265 | + <Label htmlFor="weeklyChallengeXP" className="text-white"> |
| 266 | + Weekly Challenge XP |
| 267 | + </Label> |
| 268 | + <Input |
| 269 | + id="weeklyChallengeXP" |
| 270 | + type="number" |
| 271 | + {...register('weeklyChallengeXP', { valueAsNumber: true })} |
| 272 | + className="bg-black-50 text-white border border-black-25" |
| 273 | + placeholder="Optional XP reward" |
| 274 | + /> |
| 275 | + {errors.weeklyChallengeXP && ( |
| 276 | + <p className="text-red-500 text-sm mt-1">{errors.weeklyChallengeXP.message}</p> |
| 277 | + )} |
| 278 | + </div> |
| 279 | + </div> |
| 280 | + |
| 281 | + <div className="flex justify-end"> |
| 282 | + <Button |
| 283 | + type="submit" |
| 284 | + disabled={isSubmitting} |
| 285 | + className="bg-accent hover:bg-accent/80 text-white" |
| 286 | + > |
| 287 | + {isSubmitting ? 'Creating...' : 'Create League'} |
| 288 | + </Button> |
| 289 | + </div> |
| 290 | + </form> |
| 291 | + ); |
| 292 | +} |
0 commit comments