|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React from "react"; |
| 4 | +import { withFieldGroup } from "@/lib/forms/onboarding-form"; |
| 5 | +import { |
| 6 | + useCaseLabels, |
| 7 | + useCaseDescriptions, |
| 8 | + useCases, |
| 9 | + useCaseSchema, |
| 10 | +} from "@/lib/validations/onboarding"; |
| 11 | +import { Button } from "@refref/ui/components/button"; |
| 12 | +import { Checkbox } from "@refref/ui/components/checkbox"; |
| 13 | +import { Label } from "@refref/ui/components/label"; |
| 14 | +import { ChevronLeft, ChevronRight } from "lucide-react"; |
| 15 | + |
| 16 | +export const UseCaseStep = withFieldGroup({ |
| 17 | + defaultValues: { |
| 18 | + useCase: [] as (typeof useCases)[number][], |
| 19 | + }, |
| 20 | + props: { |
| 21 | + onNext: () => {}, |
| 22 | + onPrevious: () => {}, |
| 23 | + isFirstStep: false, |
| 24 | + isLastStep: false, |
| 25 | + isSubmitting: false, |
| 26 | + submitButtonRef: { |
| 27 | + current: null, |
| 28 | + } as React.RefObject<HTMLButtonElement | null>, |
| 29 | + }, |
| 30 | + render: function Render({ |
| 31 | + group, |
| 32 | + onNext, |
| 33 | + onPrevious, |
| 34 | + isFirstStep, |
| 35 | + isLastStep, |
| 36 | + isSubmitting, |
| 37 | + submitButtonRef, |
| 38 | + }) { |
| 39 | + const onSubmit = async () => { |
| 40 | + group.form.setFieldMeta("*", (m) => ({ ...m, errors: [] })); |
| 41 | + const errors = await group.validateAllFields("change"); |
| 42 | + if (errors.length > 0) { |
| 43 | + return; |
| 44 | + } |
| 45 | + onNext(); |
| 46 | + }; |
| 47 | + |
| 48 | + const onBefore = () => { |
| 49 | + Object.values(group.fieldsMap).forEach((field) => { |
| 50 | + group.form.setFieldMeta(field, (m) => ({ |
| 51 | + ...m, |
| 52 | + errors: [], |
| 53 | + errorMap: {}, |
| 54 | + })); |
| 55 | + }); |
| 56 | + |
| 57 | + onPrevious(); |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="space-y-6"> |
| 62 | + <div className="space-y-2"> |
| 63 | + <h2 className="text-2xl font-semibold"> |
| 64 | + Which programs are you interested in? |
| 65 | + </h2> |
| 66 | + <p className="text-muted-foreground"> |
| 67 | + Choose all that apply. You can change this later. |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div className="space-y-3"> |
| 72 | + <group.AppField |
| 73 | + name="useCase" |
| 74 | + validators={{ |
| 75 | + onChange: useCaseSchema.shape.useCase, |
| 76 | + }} |
| 77 | + > |
| 78 | + {(field) => { |
| 79 | + const value = field.state.value ?? []; |
| 80 | + |
| 81 | + const toggleOption = (option: (typeof useCases)[number]) => { |
| 82 | + const next = value.includes(option) |
| 83 | + ? value.filter((v) => v !== option) |
| 84 | + : [...value, option]; |
| 85 | + field.handleChange(next); |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + {useCases.map((option) => ( |
| 91 | + <Label |
| 92 | + key={option} |
| 93 | + htmlFor={`usecase-${option}`} |
| 94 | + className="flex items-start space-x-3 rounded-lg border p-4 hover:bg-muted/50 cursor-pointer transition-colors" |
| 95 | + data-testid={`checkbox-option-${option}`} |
| 96 | + > |
| 97 | + <Checkbox |
| 98 | + id={`usecase-${option}`} |
| 99 | + checked={value.includes(option)} |
| 100 | + onCheckedChange={() => toggleOption(option)} |
| 101 | + className="mt-0.5" |
| 102 | + /> |
| 103 | + <div className="flex-1 space-y-1"> |
| 104 | + <span className="font-medium"> |
| 105 | + {useCaseLabels[option]} |
| 106 | + </span> |
| 107 | + <p className="text-sm text-muted-foreground"> |
| 108 | + {useCaseDescriptions[option]} |
| 109 | + </p> |
| 110 | + </div> |
| 111 | + </Label> |
| 112 | + ))} |
| 113 | + {field.state.meta.errors && |
| 114 | + field.state.meta.errors.length > 0 && ( |
| 115 | + <p className="text-sm text-destructive mt-2"> |
| 116 | + {typeof field.state.meta.errors[0] === "string" |
| 117 | + ? field.state.meta.errors[0] |
| 118 | + : field.state.meta.errors[0]?.message || |
| 119 | + "Invalid value"} |
| 120 | + </p> |
| 121 | + )} |
| 122 | + </> |
| 123 | + ); |
| 124 | + }} |
| 125 | + </group.AppField> |
| 126 | + </div> |
| 127 | + {/* Navigation Buttons */} |
| 128 | + <div className="flex justify-between pt-6 border-t"> |
| 129 | + <Button |
| 130 | + type="button" |
| 131 | + variant="outline" |
| 132 | + onClick={onBefore} |
| 133 | + disabled={isFirstStep} |
| 134 | + data-testid="onboarding-previous-btn" |
| 135 | + > |
| 136 | + <ChevronLeft className="h-4 w-4 mr-2" /> |
| 137 | + Previous |
| 138 | + </Button> |
| 139 | + |
| 140 | + <Button |
| 141 | + ref={submitButtonRef} |
| 142 | + type="button" |
| 143 | + onClick={onSubmit} |
| 144 | + disabled={isSubmitting} |
| 145 | + data-testid="onboarding-next-btn" |
| 146 | + > |
| 147 | + {isLastStep |
| 148 | + ? isSubmitting |
| 149 | + ? "Creating..." |
| 150 | + : "Complete Setup" |
| 151 | + : "Next"} |
| 152 | + {!isLastStep && <ChevronRight className="h-4 w-4 ml-2" />} |
| 153 | + </Button> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + ); |
| 157 | + }, |
| 158 | +}); |
0 commit comments