|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useActionState } from "react"; |
| 4 | +import { createService, updateService, setServiceStatus } from "./actions"; |
| 5 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import { Label } from "@/components/ui/label"; |
| 9 | + |
| 10 | +function CreateServiceCard() { |
| 11 | + const [state, action, isPending] = useActionState(createService, null); |
| 12 | + |
| 13 | + return ( |
| 14 | + <Card className="w-full shadow-sm"> |
| 15 | + <CardHeader> |
| 16 | + <CardTitle className="text-xl">Create Service</CardTitle> |
| 17 | + <CardDescription>Test creating a new service in DB and Stripe.</CardDescription> |
| 18 | + </CardHeader> |
| 19 | + <CardContent> |
| 20 | + {state?.message && ( |
| 21 | + <div className="bg-green-50 text-green-900 border border-green-200 p-3 rounded-md mb-4 text-sm"> |
| 22 | + {state.message} |
| 23 | + </div> |
| 24 | + )} |
| 25 | + {state?.errors?._form && ( |
| 26 | + <div className="bg-destructive/15 text-destructive border border-destructive/20 p-3 rounded-md mb-4 text-sm"> |
| 27 | + {state.errors._form[0]} |
| 28 | + </div> |
| 29 | + )} |
| 30 | + |
| 31 | + <form action={action} className="space-y-4" noValidate> |
| 32 | + <div className="space-y-2"> |
| 33 | + <Label htmlFor="title">Service Title</Label> |
| 34 | + <Input id="title" name="title" placeholder="e.g., Parent Coaching" /> |
| 35 | + {state?.errors?.title && ( |
| 36 | + <p className="text-xs text-destructive">{state.errors.title[0]}</p> |
| 37 | + )} |
| 38 | + </div> |
| 39 | + |
| 40 | + <div className="space-y-2"> |
| 41 | + <Label htmlFor="description">Description (Required)</Label> |
| 42 | + <Input id="description" name="description" placeholder="A short explanation" /> |
| 43 | + {state?.errors?.description && ( |
| 44 | + <p className="text-xs text-destructive">{state.errors.description[0]}</p> |
| 45 | + )} |
| 46 | + </div> |
| 47 | + |
| 48 | + <div className="grid grid-cols-2 gap-4"> |
| 49 | + <div className="space-y-2"> |
| 50 | + <Label htmlFor="price_cad">Price (CAD)</Label> |
| 51 | + <div className="relative"> |
| 52 | + <span className="absolute left-3 top-2.5 text-muted-foreground text-sm">$</span> |
| 53 | + <Input id="price_cad" name="price_cad" placeholder="99.00" className="pl-6" /> |
| 54 | + </div> |
| 55 | + {state?.errors?.price_cad && ( |
| 56 | + <p className="text-xs text-destructive">{state.errors.price_cad[0]}</p> |
| 57 | + )} |
| 58 | + </div> |
| 59 | + |
| 60 | + <div className="space-y-2"> |
| 61 | + <Label htmlFor="duration_minutes">Duration (Min)</Label> |
| 62 | + <Input id="duration_minutes" name="duration_minutes" defaultValue="60" /> |
| 63 | + {state?.errors?.duration_minutes && ( |
| 64 | + <p className="text-xs text-destructive">{state.errors.duration_minutes[0]}</p> |
| 65 | + )} |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div className="space-y-2"> |
| 70 | + <Label htmlFor="scheduled_at">Scheduled Dates (JSON Array)</Label> |
| 71 | + <Input id="scheduled_at" name="scheduled_at" placeholder='["2026-05-01T10:00:00Z"]' /> |
| 72 | + {state?.errors?.scheduled_at && ( |
| 73 | + <p className="text-xs text-destructive">{state.errors.scheduled_at[0]}</p> |
| 74 | + )} |
| 75 | + <p className="text-xs text-muted-foreground mt-1">Required for Bookings. Ignore for Coaching.</p> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div className="space-y-2"> |
| 79 | + <Label htmlFor="type">Service Type</Label> |
| 80 | + <select |
| 81 | + id="type" |
| 82 | + name="type" |
| 83 | + className="flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background disabled:cursor-not-allowed disabled:opacity-50" |
| 84 | + > |
| 85 | + <option value="coaching_session">Coaching Session</option> |
| 86 | + <option value="booking">Booking</option> |
| 87 | + </select> |
| 88 | + </div> |
| 89 | + |
| 90 | + <div className="pt-2"> |
| 91 | + <Button type="submit" className="w-full" disabled={isPending}> |
| 92 | + {isPending ? "Connecting to Stripe..." : "Create Service"} |
| 93 | + </Button> |
| 94 | + </div> |
| 95 | + </form> |
| 96 | + </CardContent> |
| 97 | + </Card> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function EditServiceCard() { |
| 102 | + const [state, action, isPending] = useActionState(updateService, null); |
| 103 | + |
| 104 | + return ( |
| 105 | + <Card className="w-full shadow-sm"> |
| 106 | + <CardHeader> |
| 107 | + <CardTitle className="text-xl">Edit Service</CardTitle> |
| 108 | + <CardDescription>Paste a Service ID (UUID) to update details.</CardDescription> |
| 109 | + </CardHeader> |
| 110 | + <CardContent> |
| 111 | + {state?.message && ( |
| 112 | + <div className="bg-green-50 text-green-900 border border-green-200 p-3 rounded-md mb-4 text-sm"> |
| 113 | + {state.message} |
| 114 | + </div> |
| 115 | + )} |
| 116 | + {state?.errors?._form && ( |
| 117 | + <div className="bg-destructive/15 text-destructive border border-destructive/20 p-3 rounded-md mb-4 text-sm"> |
| 118 | + {state.errors._form[0]} |
| 119 | + </div> |
| 120 | + )} |
| 121 | + |
| 122 | + <form action={action} className="space-y-4" noValidate> |
| 123 | + <div className="space-y-2"> |
| 124 | + <Label htmlFor="service_id">Service ID (UUID) *</Label> |
| 125 | + <Input id="service_id" name="service_id" placeholder="Copy from Supabase" /> |
| 126 | + {state?.errors?.service_id && ( |
| 127 | + <p className="text-xs text-destructive">{state.errors.service_id[0]}</p> |
| 128 | + )} |
| 129 | + </div> |
| 130 | + |
| 131 | + <div className="space-y-2"> |
| 132 | + <Label htmlFor="edit-title">New Title</Label> |
| 133 | + <Input id="edit-title" name="title" placeholder="Optional" /> |
| 134 | + {state?.errors?.title && ( |
| 135 | + <p className="text-xs text-destructive">{state.errors.title[0]}</p> |
| 136 | + )} |
| 137 | + </div> |
| 138 | + |
| 139 | + <div className="space-y-2"> |
| 140 | + <Label htmlFor="edit-description">New Description</Label> |
| 141 | + <Input id="edit-description" name="description" placeholder="Optional" /> |
| 142 | + {state?.errors?.description && ( |
| 143 | + <p className="text-xs text-destructive">{state.errors.description[0]}</p> |
| 144 | + )} |
| 145 | + </div> |
| 146 | + |
| 147 | + <div className="grid grid-cols-2 gap-4"> |
| 148 | + <div className="space-y-2"> |
| 149 | + <Label htmlFor="edit-price_cad">New Price (CAD)</Label> |
| 150 | + <div className="relative"> |
| 151 | + <span className="absolute left-3 top-2.5 text-muted-foreground text-sm">$</span> |
| 152 | + <Input id="edit-price_cad" name="price_cad" placeholder="Optional" className="pl-6" /> |
| 153 | + </div> |
| 154 | + {state?.errors?.price_cad && ( |
| 155 | + <p className="text-xs text-destructive">{state.errors.price_cad[0]}</p> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + |
| 159 | + <div className="space-y-2"> |
| 160 | + <Label htmlFor="edit-duration_minutes">Duration (Min)</Label> |
| 161 | + <Input id="edit-duration_minutes" name="duration_minutes" placeholder="Optional" /> |
| 162 | + {state?.errors?.duration_minutes && ( |
| 163 | + <p className="text-xs text-destructive">{state.errors.duration_minutes[0]}</p> |
| 164 | + )} |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + |
| 168 | + <div className="space-y-2"> |
| 169 | + <Label htmlFor="edit-scheduled_at">New Scheduled Dates (JSON Array)</Label> |
| 170 | + <Input id="edit-scheduled_at" name="scheduled_at" placeholder='Optional' /> |
| 171 | + {state?.errors?.scheduled_at && ( |
| 172 | + <p className="text-xs text-destructive">{state.errors.scheduled_at[0]}</p> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + |
| 176 | + <p className="text-xs text-muted-foreground">Note: Service Type is immutable.</p> |
| 177 | + |
| 178 | + <div className="pt-2"> |
| 179 | + <Button type="submit" variant="secondary" className="w-full" disabled={isPending}> |
| 180 | + {isPending ? "Connecting to Stripe..." : "Update Service"} |
| 181 | + </Button> |
| 182 | + </div> |
| 183 | + </form> |
| 184 | + </CardContent> |
| 185 | + </Card> |
| 186 | + ); |
| 187 | +} |
| 188 | + |
| 189 | +function UpdateStatusCard() { |
| 190 | + const [state, action, isPending] = useActionState(setServiceStatus, null); |
| 191 | + |
| 192 | + return ( |
| 193 | + <Card className="w-full shadow-sm border-amber-200"> |
| 194 | + <CardHeader> |
| 195 | + <CardTitle className="text-xl">Set Status</CardTitle> |
| 196 | + <CardDescription>Archive or disable an existing service.</CardDescription> |
| 197 | + </CardHeader> |
| 198 | + <CardContent> |
| 199 | + {state?.message && ( |
| 200 | + <div className="bg-green-50 text-green-900 border border-green-200 p-3 rounded-md mb-4 text-sm"> |
| 201 | + {state.message} |
| 202 | + </div> |
| 203 | + )} |
| 204 | + {state?.errors?._form && ( |
| 205 | + <div className="bg-destructive/15 text-destructive border border-destructive/20 p-3 rounded-md mb-4 text-sm"> |
| 206 | + {state.errors._form[0]} |
| 207 | + </div> |
| 208 | + )} |
| 209 | + |
| 210 | + <form action={action} className="space-y-4" noValidate> |
| 211 | + <div className="space-y-2"> |
| 212 | + <Label htmlFor="status_service_id">Service ID (UUID) *</Label> |
| 213 | + <Input id="status_service_id" name="service_id" placeholder="Copy from Supabase" /> |
| 214 | + {state?.errors?.service_id && ( |
| 215 | + <p className="text-xs text-destructive">{state.errors.service_id[0]}</p> |
| 216 | + )} |
| 217 | + </div> |
| 218 | + |
| 219 | + <div className="space-y-2"> |
| 220 | + <Label htmlFor="status">New Status</Label> |
| 221 | + <select |
| 222 | + id="status" |
| 223 | + name="status" |
| 224 | + className="flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background disabled:cursor-not-allowed disabled:opacity-50" |
| 225 | + > |
| 226 | + <option value="active">Active</option> |
| 227 | + <option value="disabled">Disabled</option> |
| 228 | + <option value="archived">Archived</option> |
| 229 | + </select> |
| 230 | + {state?.errors?.status && ( |
| 231 | + <p className="text-xs text-destructive">{state.errors.status[0]}</p> |
| 232 | + )} |
| 233 | + </div> |
| 234 | + |
| 235 | + <div className="pt-2"> |
| 236 | + <Button type="submit" variant="destructive" className="w-full" disabled={isPending}> |
| 237 | + {isPending ? "Connecting to Stripe..." : "Change Status"} |
| 238 | + </Button> |
| 239 | + </div> |
| 240 | + </form> |
| 241 | + </CardContent> |
| 242 | + </Card> |
| 243 | + ); |
| 244 | +} |
| 245 | + |
| 246 | +export function ServicesPlayground() { |
| 247 | + return ( |
| 248 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-8 w-full max-w-5xl items-start"> |
| 249 | + <CreateServiceCard /> |
| 250 | + <EditServiceCard /> |
| 251 | + <UpdateStatusCard /> |
| 252 | + </div> |
| 253 | + ); |
| 254 | +} |
0 commit comments