|
1 | | -'use client' |
2 | | - |
3 | | -import { useCallback, useEffect, useRef, useState } from 'react' |
4 | | - |
5 | | -import { Button } from '@/components/ui/button' |
6 | | -import { Input } from '@/components/ui/input' |
7 | | -import type { Question } from '@/lib/api-types' |
8 | | - |
9 | | -interface QuestionInputProps { |
10 | | - questions: Question[] |
11 | | - onSubmit?: (answers: Record<string, string>) => void |
12 | | -} |
13 | | - |
14 | | -export function QuestionInput({ questions, onSubmit }: QuestionInputProps) { |
15 | | - const [selectedAnswers, setSelectedAnswers] = useState<Record<string, string>>({}) |
16 | | - const [showOther, setShowOther] = useState<Record<number, boolean>>({}) |
17 | | - const [otherValues, setOtherValues] = useState<Record<number, string>>({}) |
18 | | - const submittedRef = useRef(false) |
19 | | - |
20 | | - const isResolved = questions.every((q) => q.selected_answer) |
21 | | - const isInteractive = !!onSubmit && !isResolved && !submittedRef.current |
22 | | - |
23 | | - // Auto-submit when all questions have a local answer |
24 | | - useEffect(() => { |
25 | | - if ( |
26 | | - !submittedRef.current && |
27 | | - onSubmit && |
28 | | - questions.length > 0 && |
29 | | - questions.every((q) => selectedAnswers[q.question]) |
30 | | - ) { |
31 | | - submittedRef.current = true |
32 | | - onSubmit(selectedAnswers) |
33 | | - } |
34 | | - }, [selectedAnswers, questions, onSubmit]) |
35 | | - |
36 | | - const handleOptionClick = useCallback( |
37 | | - (question: Question, label: string) => { |
38 | | - if (submittedRef.current) return |
39 | | - setSelectedAnswers((prev) => ({ ...prev, [question.question]: label })) |
40 | | - }, |
41 | | - [] |
42 | | - ) |
43 | | - |
44 | | - const handleOtherSubmit = useCallback( |
45 | | - (questionIdx: number, question: Question) => { |
46 | | - const value = otherValues[questionIdx] |
47 | | - if (!value?.trim() || submittedRef.current) return |
48 | | - setSelectedAnswers((prev) => ({ ...prev, [question.question]: value.trim() })) |
49 | | - }, |
50 | | - [otherValues] |
51 | | - ) |
52 | | - |
53 | | - return ( |
54 | | - <div className="space-y-4 py-1"> |
55 | | - {questions.map((q, qi) => { |
56 | | - const localAnswer = selectedAnswers[q.question] |
57 | | - const resolvedAnswer = q.selected_answer |
58 | | - const answered = !!localAnswer || !!resolvedAnswer |
59 | | - |
60 | | - return ( |
61 | | - <div key={qi} className="space-y-2"> |
62 | | - {q.header && ( |
63 | | - <p className="text-sm text-muted-foreground">{q.header}</p> |
64 | | - )} |
65 | | - <p className="text-sm font-medium">{q.question}</p> |
66 | | - |
67 | | - <div className="flex flex-wrap gap-1.5"> |
68 | | - {q.options.map((opt, oi) => { |
69 | | - const isSelected = resolvedAnswer === opt.label || localAnswer === opt.label |
70 | | - |
71 | | - if (isInteractive && !localAnswer) { |
72 | | - return ( |
73 | | - <Button |
74 | | - key={oi} |
75 | | - variant="outline" |
76 | | - size="sm" |
77 | | - onClick={() => handleOptionClick(q, opt.label)} |
78 | | - title={opt.description || undefined} |
79 | | - className="h-auto px-2 py-1 text-xs border-sky/30 bg-sky/5 hover:bg-sky/10 hover:border-sky/50" |
80 | | - > |
81 | | - {opt.label} |
82 | | - </Button> |
83 | | - ) |
84 | | - } |
85 | | - |
86 | | - return ( |
87 | | - <span |
88 | | - key={oi} |
89 | | - className={`inline-flex rounded-md border px-2 py-1 text-xs ${ |
90 | | - isSelected |
91 | | - ? 'border-primary bg-primary/10 text-primary font-medium' |
92 | | - : 'border-border/50 text-muted-foreground' |
93 | | - }`} |
94 | | - > |
95 | | - {opt.label} |
96 | | - </span> |
97 | | - ) |
98 | | - })} |
99 | | - {isInteractive && !localAnswer && ( |
100 | | - <Button |
101 | | - variant="ghost" |
102 | | - size="sm" |
103 | | - onClick={() => setShowOther((prev) => ({ ...prev, [qi]: true }))} |
104 | | - className="h-auto px-2 py-1 text-xs text-muted-foreground hover:text-foreground" |
105 | | - > |
106 | | - Other |
107 | | - </Button> |
108 | | - )} |
109 | | - </div> |
110 | | - |
111 | | - {/* Free-text answer that doesn't match any option */} |
112 | | - {(resolvedAnswer || localAnswer) && !q.options.some((o) => o.label === (resolvedAnswer || localAnswer)) && ( |
113 | | - <p className="text-xs text-primary italic">→ {resolvedAnswer || localAnswer}</p> |
114 | | - )} |
115 | | - |
116 | | - {isInteractive && !localAnswer && showOther[qi] && ( |
117 | | - <div className="flex gap-2"> |
118 | | - <Input |
119 | | - placeholder="Your answer..." |
120 | | - value={otherValues[qi] || ''} |
121 | | - onChange={(e) => |
122 | | - setOtherValues((prev) => ({ ...prev, [qi]: e.target.value })) |
123 | | - } |
124 | | - onKeyDown={(e) => { |
125 | | - if (e.key === 'Enter') { |
126 | | - e.preventDefault() |
127 | | - handleOtherSubmit(qi, q) |
128 | | - } |
129 | | - }} |
130 | | - /> |
131 | | - <Button |
132 | | - size="sm" |
133 | | - onClick={() => handleOtherSubmit(qi, q)} |
134 | | - disabled={!otherValues[qi]?.trim()} |
135 | | - > |
136 | | - Submit |
137 | | - </Button> |
138 | | - </div> |
139 | | - )} |
140 | | - </div> |
141 | | - ) |
142 | | - })} |
143 | | - </div> |
144 | | - ) |
145 | | -} |
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useCallback, useEffect, useRef, useState } from 'react' |
| 4 | + |
| 5 | +import { Button } from '@/components/ui/button' |
| 6 | +import { Input } from '@/components/ui/input' |
| 7 | +import type { Question } from '@/lib/api-types' |
| 8 | + |
| 9 | +interface QuestionInputProps { |
| 10 | + questions: Question[] |
| 11 | + onSubmit?: (answers: Record<string, string>) => void |
| 12 | +} |
| 13 | + |
| 14 | +export function QuestionInput({ questions, onSubmit }: QuestionInputProps) { |
| 15 | + const [selectedAnswers, setSelectedAnswers] = useState<Record<string, string>>({}) |
| 16 | + const [showOther, setShowOther] = useState<Record<number, boolean>>({}) |
| 17 | + const [otherValues, setOtherValues] = useState<Record<number, string>>({}) |
| 18 | + const submittedRef = useRef(false) |
| 19 | + |
| 20 | + const isResolved = questions.every((q) => q.selected_answer) |
| 21 | + const isInteractive = !!onSubmit && !isResolved |
| 22 | + |
| 23 | + // Auto-submit when all questions have a local answer |
| 24 | + useEffect(() => { |
| 25 | + if ( |
| 26 | + !submittedRef.current && |
| 27 | + onSubmit && |
| 28 | + questions.length > 0 && |
| 29 | + questions.every((q) => selectedAnswers[q.question]) |
| 30 | + ) { |
| 31 | + submittedRef.current = true |
| 32 | + onSubmit(selectedAnswers) |
| 33 | + } |
| 34 | + }, [selectedAnswers, questions, onSubmit]) |
| 35 | + |
| 36 | + const handleOptionClick = useCallback( |
| 37 | + (question: Question, label: string) => { |
| 38 | + if (submittedRef.current) return |
| 39 | + setSelectedAnswers((prev) => ({ ...prev, [question.question]: label })) |
| 40 | + }, |
| 41 | + [] |
| 42 | + ) |
| 43 | + |
| 44 | + const handleOtherSubmit = useCallback( |
| 45 | + (questionIdx: number, question: Question) => { |
| 46 | + const value = otherValues[questionIdx] |
| 47 | + if (!value?.trim() || submittedRef.current) return |
| 48 | + setSelectedAnswers((prev) => ({ ...prev, [question.question]: value.trim() })) |
| 49 | + }, |
| 50 | + [otherValues] |
| 51 | + ) |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="space-y-4 py-1"> |
| 55 | + {questions.map((q, qi) => { |
| 56 | + const localAnswer = selectedAnswers[q.question] |
| 57 | + const resolvedAnswer = q.selected_answer |
| 58 | + const answered = !!localAnswer || !!resolvedAnswer |
| 59 | + |
| 60 | + return ( |
| 61 | + <div key={qi} className="space-y-2"> |
| 62 | + {q.header && ( |
| 63 | + <p className="text-sm text-muted-foreground">{q.header}</p> |
| 64 | + )} |
| 65 | + <p className="text-sm font-medium">{q.question}</p> |
| 66 | + |
| 67 | + <div className="flex flex-wrap gap-1.5"> |
| 68 | + {q.options.map((opt, oi) => { |
| 69 | + const isSelected = resolvedAnswer === opt.label || localAnswer === opt.label |
| 70 | + |
| 71 | + if (isInteractive && !localAnswer) { |
| 72 | + return ( |
| 73 | + <Button |
| 74 | + key={oi} |
| 75 | + variant="outline" |
| 76 | + size="sm" |
| 77 | + onClick={() => handleOptionClick(q, opt.label)} |
| 78 | + title={opt.description || undefined} |
| 79 | + className="h-auto px-2 py-1 text-xs border-sky/30 bg-sky/5 hover:bg-sky/10 hover:border-sky/50" |
| 80 | + > |
| 81 | + {opt.label} |
| 82 | + </Button> |
| 83 | + ) |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <span |
| 88 | + key={oi} |
| 89 | + className={`inline-flex rounded-md border px-2 py-1 text-xs ${ |
| 90 | + isSelected |
| 91 | + ? 'border-primary bg-primary/10 text-primary font-medium' |
| 92 | + : 'border-border/50 text-muted-foreground' |
| 93 | + }`} |
| 94 | + > |
| 95 | + {opt.label} |
| 96 | + </span> |
| 97 | + ) |
| 98 | + })} |
| 99 | + {isInteractive && !localAnswer && ( |
| 100 | + <Button |
| 101 | + variant="ghost" |
| 102 | + size="sm" |
| 103 | + onClick={() => setShowOther((prev) => ({ ...prev, [qi]: true }))} |
| 104 | + className="h-auto px-2 py-1 text-xs text-muted-foreground hover:text-foreground" |
| 105 | + > |
| 106 | + Other |
| 107 | + </Button> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + |
| 111 | + {/* Free-text answer that doesn't match any option */} |
| 112 | + {(resolvedAnswer || localAnswer) && !q.options.some((o) => o.label === (resolvedAnswer || localAnswer)) && ( |
| 113 | + <p className="text-xs text-primary italic">→ {resolvedAnswer || localAnswer}</p> |
| 114 | + )} |
| 115 | + |
| 116 | + {isInteractive && !localAnswer && showOther[qi] && ( |
| 117 | + <div className="flex gap-2"> |
| 118 | + <Input |
| 119 | + placeholder="Your answer..." |
| 120 | + value={otherValues[qi] || ''} |
| 121 | + onChange={(e) => |
| 122 | + setOtherValues((prev) => ({ ...prev, [qi]: e.target.value })) |
| 123 | + } |
| 124 | + onKeyDown={(e) => { |
| 125 | + if (e.key === 'Enter') { |
| 126 | + e.preventDefault() |
| 127 | + handleOtherSubmit(qi, q) |
| 128 | + } |
| 129 | + }} |
| 130 | + /> |
| 131 | + <Button |
| 132 | + size="sm" |
| 133 | + onClick={() => handleOtherSubmit(qi, q)} |
| 134 | + disabled={!otherValues[qi]?.trim()} |
| 135 | + > |
| 136 | + Submit |
| 137 | + </Button> |
| 138 | + </div> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + ) |
| 142 | + })} |
| 143 | + </div> |
| 144 | + ) |
| 145 | +} |
0 commit comments