11import React , { useState } from 'react'
22import { useNavigate } from 'react-router-dom'
33import axios from 'axios'
4+ import { startWindToast } from "@mariojgt/wind-notify/packages/index.js"
45
56const API_URL = import . meta. env . VITE_API_URL
67
78const Quiz = ( ) => {
89 const navigate = useNavigate ( )
9-
10- const [ answers , setAnswers ] = useState ( { } )
11- const [ inputValue , setInputValue ] = useState ( "" )
12- const [ showModal , setShowModal ] = useState ( true )
10+ const [ answers , setAnswers ] = useState ( {
11+ q1 : '' ,
12+ q2 : '' ,
13+ q3 : '' ,
14+ q4 : '' ,
15+ q5 : '' ,
16+ q6 : '' ,
17+ q7 : ''
18+ } )
1319 const [ recommendedCar , setRecommendedCar ] = useState ( undefined )
1420 const [ showToast , setShowToast ] = useState ( undefined )
1521 const [ currentQuestionIndex , setCurrentQuestionIndex ] = useState ( 0 )
1622 const [ loading , setLoading ] = useState ( false )
1723
1824 const questions = [
19- "What is your budget?" ,
25+ "What is your current budget?" ,
2026 "Do you prefer a new or used car?" ,
21- "What car brands do you prefer?" ,
22- "What type of car body do you prefer?" ,
23- "What model year are you looking for?" ,
24- "Do you need an environmentally friendly car?" ,
25- "Do you prefer gas, diesel, electric, or hybrid?" ,
26- "How many miles are you comfortable with on a used car?" ,
27- "What color options do you prefer?" ,
28- "Are there any specific features you must have?" ,
29- "Tell me about your ideal car and what matters most to you"
27+ "What is your desired passenger capacity?" ,
28+ "What will be the purpose of your vehicle?" ,
29+ "What features do you want your vehicle to have?" ,
30+ "What are some things you wouldn't like with your vehicle?" ,
31+ "Are there specific safety features you are looking for?" ,
3032 ]
3133
3234 const questionKeys = {
@@ -43,53 +45,24 @@ const Quiz = () => {
4345 10 : "situation description"
4446 }
4547
46- const toggleModal = ( ) => {
47- setShowModal ( ! showModal )
48- }
49-
50- const handleNext = ( ) => {
51- const newAnswers = {
52- ...answers ,
53- [ questionKeys [ currentQuestionIndex ] ] : inputValue
54- }
55- setAnswers ( newAnswers )
56- setInputValue ( "" )
57- if ( currentQuestionIndex < questions . length - 1 ) {
58- setCurrentQuestionIndex ( currentQuestionIndex + 1 )
59- }
60- }
61-
62- const handlePrevious = ( ) => {
63- if ( currentQuestionIndex > 0 ) {
64- setCurrentQuestionIndex ( currentQuestionIndex - 1 )
65- setInputValue ( answers [ questionKeys [ currentQuestionIndex - 1 ] ] || "" )
66- }
67- }
68-
6948 const handleBack = ( ) => {
7049 navigate ( - 1 )
7150 }
7251
7352 const handleSubmit = async ( ) => {
74- setLoading ( true )
75- const finalAnswers = {
76- ...answers ,
77- [ questionKeys [ currentQuestionIndex ] ] : inputValue
78- }
79-
80- if ( Object . values ( finalAnswers ) . every ( answer => answer === "" ) ) {
53+ if ( Object . values ( answers ) . every ( answer => answer === "" ) ) {
8154 setShowToast ( { key : 'error' , value : 'Please provide at least 1 answer' } )
8255 setTimeout ( ( ) => setShowToast ( undefined ) , 3000 )
8356 return
8457 }
8558
86- setAnswers ( finalAnswers )
87- const flatAnswer = Object . entries ( finalAnswers )
59+ const flatAnswer = Object . entries ( answers )
8860 . filter ( ( [ _ , value ] ) => value )
89- . map ( ( [ key , value ] ) => `${ key } : ${ value } ` )
90- . join ( ", " )
91-
92- await sendAIQuery ( flatAnswer )
61+ . map ( ( [ _ , value ] , index ) => `${ questions [ index ] } ${ value } ` )
62+ . join ( "; " )
63+ setLoading ( true )
64+ // await sendAIQuery(flatAnswer)
65+ startWindToast ( "WARNING!" , "Our AI service is currently down" , 'warning' , 3 , 'top' )
9366 setLoading ( false )
9467 }
9568
@@ -128,21 +101,12 @@ const Quiz = () => {
128101 . map ( ( [ key , value ] ) => `${ key } : ${ value } ` )
129102 . join ( ", " )
130103
131- await sendAIQuery ( flatAnswer + "but not previous vehicle" )
104+ // await sendAIQuery(flatAnswer + "but not previous vehicle")
132105 setLoading ( false )
133106 }
134107
135108 return (
136- < div className = 'flex flex-col justify-center items-center' >
137- { showModal && (
138- < div className = "fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50" >
139- < div className = "bg-white text-gray-900 p-6 rounded-lg w-11/12 md:w-1/3 relative shadow-lg" >
140- < button onClick = { toggleModal } className = "absolute top-2 right-2 text-gray-600 text-2xl" > ×</ button >
141- < h2 className = "text-xl font-bold mb-4 text-blue-900" > Here to Help!</ h2 >
142- < p > Answer a few simple questions, and I'll help you find the perfect car!</ p >
143- </ div >
144- </ div >
145- ) }
109+ < div className = 'container mx-auto items-center space-y-2' >
146110
147111 { loading && (
148112 < div className = "fixed toast toast-center toast-middle z-50" >
@@ -160,54 +124,40 @@ const Quiz = () => {
160124 </ div >
161125 ) }
162126
163- < div className = 'bg-white text-gray-900 p-6 rounded-lg w-3/4 text-center mt-4 shadow-lg' >
164- < div className = 'h-2 w-full bg-gray-300' >
165- < div
166- className = 'h-2 bg-[#0a2530] transition-all duration-300'
167- style = { { width : `${ ( ( currentQuestionIndex + 1 ) / questions . length ) * 100 } %` } } >
168- </ div >
169- </ div >
170-
171- < div className = 'mx-auto bg-gradient-to-b from-primary to-gray-600 text-white p-6 rounded-lg w-3/4 text-center mt-4' >
172- < h2 className = 'text-2xl font-bold mb-2 text-blue-800' >
173- Question { currentQuestionIndex + 1 }
174- </ h2 >
175- < p className = 'text-xl mb-4' >
176- { questions [ currentQuestionIndex ] }
177- </ p >
178-
179- < p className = '' >
180- < textarea
181- type = 'textarea'
182- value = { inputValue }
183- onChange = { ( e ) => setInputValue ( e . target . value ) }
184- className = 'w-full p-2 text-black rounded-md'
185- placeholder = ''
186- />
187- </ p >
188-
189-
190- < div className = 'flex justify-between mt-6' >
191- < button
192- onClick = { handlePrevious }
193- disabled = { currentQuestionIndex === 0 }
194- className = { `px-4 py-2 rounded-md text-white bg-gray-400 hover:bg-gray-500 transition ${ currentQuestionIndex === 0 ? " cursor-not-allowed" : "" } ` } >
195- Previous
196- </ button >
197-
198- { currentQuestionIndex === questions . length - 1 ? (
199- < button
200- onClick = { ( ) => handleSubmit ( ) }
201- className = 'px-4 py-2 rounded-md text-white bg-green-600 hover:bg-green-500 transition' >
202- Finish
203- </ button >
204- ) : (
205- < button
206- onClick = { handleNext }
207- className = { `px-4 py-2 rounded-md text-white bg-gray-400 hover:bg-gray-500 transition` } >
208- Next
209- </ button >
210- ) }
127+ { /*Quiz section*/ }
128+ < div className = 'flex justify-center items-center' >
129+ < div className = "card bg-base-100 shadow-xl w-11/12 md:w-2/3 lg:w-1/2 border border-blue-900" >
130+ < div className = "card-body py-12 px-24" >
131+ < h2 className = "card-title text-center text-2xl font-bold text-blue-500 self-center" >
132+ Car Quiz
133+ </ h2 >
134+ < div className = "form-control" >
135+ { questions . map ( ( question , index ) => {
136+ const key = Object . keys ( answers ) [ index ]
137+ return (
138+ < fieldset className = "fieldset" key = { index } >
139+ < legend className = "fieldset-legend" > { question } </ legend >
140+ < label className = "input" >
141+ < input
142+ type = 'text'
143+ value = { answers . key }
144+ onChange = { ( e ) => setAnswers ( prev => ( { ...prev , [ key ] : e . target . value } ) ) }
145+ className = 'grow'
146+ />
147+ < span className = "badge badge-neutral badge-xs" > Optional</ span >
148+ </ label >
149+ </ fieldset >
150+ )
151+ } ) }
152+
153+ < div className = 'flex justify-center items center' >
154+ < button
155+ onClick = { ( ) => handleSubmit ( ) }
156+ className = "mt-2 btn w-1/3 bg-gradient-to-tr from-green-600 via-green-400 to-green-600 text-white hover:from-green-500 hover:to-green-400 transition-transform transform hover:scale-105 shadow-lg" >
157+ Finish
158+ </ button >
159+ </ div >
160+ </ div >
211161 </ div >
212162 </ div >
213163 </ div >
@@ -245,6 +195,8 @@ const Quiz = () => {
245195 Back to Inter
246196 </ button >
247197 </ div >
198+
199+
248200 </ div >
249201 )
250202}
0 commit comments