@@ -20,6 +20,7 @@ export default function SurveyModal({
2020 const [ questionNumber , setQuestionNumber ] = useState < number > ( 0 ) ;
2121 const [ surveyQuestions , setSurveyQuestions ] = useState < any [ ] > ( [ ] ) ;
2222 const [ responses , setResponses ] = useState < { [ key : string ] : string } > ( { } ) ;
23+ const [ isSubmitting , setIsSubmitting ] = useState < boolean > ( false ) ;
2324
2425 useEffect ( ( ) => {
2526 if ( ! surveyId ) return ;
@@ -47,14 +48,19 @@ export default function SurveyModal({
4748 } ;
4849
4950 async function handleNextQuestion ( ) {
51+ if ( isSubmitting ) return ;
52+
5053 if ( questionNumber < surveyQuestions . length - 1 ) {
5154 setQuestionNumber ( questionNumber + 1 ) ;
5255 } else {
56+ if ( ! isCurrentAnswered ) return ;
57+
58+ setIsSubmitting ( true ) ;
5359 try {
5460 const userId = JSON . parse ( localStorage . user ) . _id ;
5561
5662 // Create question responses
57- let responseIds = [ ] ;
63+ const responseIds : string [ ] = [ ] ;
5864 for ( let id of Object . keys ( responses ) ) {
5965 const response = await apiClient . post ( "questionResponses" , {
6066 userId,
@@ -73,19 +79,17 @@ export default function SurveyModal({
7379 } ) ;
7480
7581 // Update progress
76- await apiClient . put (
77- `/courses/${ courseId } /progress/single/${ userId } ` ,
78- {
79- surveyComplete : true ,
80- webinarComplete : true ,
81- }
82- ) ;
82+ await apiClient . put ( `/courses/${ courseId } /progress/single/${ userId } ` , {
83+ surveyComplete : true ,
84+ webinarComplete : true ,
85+ } ) ;
8386 setSurveyCompleted ( true ) ;
87+ onClose ( ) ;
8488 } catch ( error ) {
8589 console . error ( error ) ;
90+ } finally {
91+ setIsSubmitting ( false ) ;
8692 }
87-
88- onClose ( ) ;
8993 }
9094 }
9195
@@ -107,6 +111,7 @@ export default function SurveyModal({
107111 onClick = { onClose }
108112 className = "absolute top-4 right-4 text-gray-500 hover:text-gray-700 text-2xl font-bold"
109113 aria-label = "Close"
114+ disabled = { isSubmitting }
110115 >
111116 ×
112117 </ button >
@@ -128,20 +133,25 @@ export default function SurveyModal({
128133 < button
129134 onClick = { handlePreviousQuestion }
130135 className = "mt-6 px-6 py-3 bg-orange-400 hover:bg-orange-500 text-white font-semibold rounded-lg transition-all disabled:bg-orange-200"
131- disabled = { questionNumber === 0 }
136+ disabled = { questionNumber === 0 || isSubmitting }
132137 >
133138 Previous
134139 </ button >
135140
136141 < button
137142 onClick = { handleNextQuestion }
138143 className = { `mt-6 px-6 py-3 text-white font-semibold rounded-lg transition-all ${
139- isCurrentAnswered
144+ isCurrentAnswered && ! isSubmitting
140145 ? "bg-orange-400 hover:bg-orange-500"
141146 : "bg-orange-200 cursor-not-allowed"
142147 } `}
148+ disabled = { ! isCurrentAnswered || isSubmitting }
143149 >
144- { questionNumber < surveyQuestions . length - 1 ? "Next" : "Finish" }
150+ { isSubmitting
151+ ? "Submitting..."
152+ : questionNumber < surveyQuestions . length - 1
153+ ? "Next"
154+ : "Finish" }
145155 </ button >
146156 </ div >
147157 </ div >
0 commit comments