File tree Expand file tree Collapse file tree
src/providers/SurveyProvider/SurveyDialog Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -14,10 +14,12 @@ export const SurveyDialog: FC = () => {
1414 const {
1515 activeSurvey,
1616 activeQuestionIndex,
17+ setActiveQuestionIndex,
1718 cancelSurvey,
1819 hideSurvey,
1920 answerQuestion,
2021 currentAnswer,
22+ setCurrentAnswer,
2123 isCancelled,
2224 } = useSurvey ( ) ;
2325 const [ isAnswering , setIsAnswering ] = useState ( false ) ;
@@ -60,11 +62,27 @@ export const SurveyDialog: FC = () => {
6062 }
6163 } ;
6264
65+ const handleOnBack = ( ) => {
66+ if ( activeQuestionIndex === 0 ) {
67+ cancelSurvey ( ) ;
68+ } else if ( activeQuestionIndex ) {
69+ const previousAnswer =
70+ activeSurvey . questions [ activeQuestionIndex - 1 ] . answer ;
71+ if ( previousAnswer ) {
72+ setActiveQuestionIndex ( ( prev ) => ( prev ?? 1 ) - 1 ) ;
73+ setCurrentAnswer ( {
74+ id : activeSurvey . questions [ activeQuestionIndex - 1 ] . questionId ,
75+ ...previousAnswer ,
76+ } ) ;
77+ }
78+ }
79+ } ;
80+
6381 const defaultActions : DialogProps [ 'actions' ] = [
6482 {
65- text : activeQuestionIndex === 0 ? 'Maybe later' : 'Cancel ' ,
83+ text : activeQuestionIndex === 0 ? 'Maybe later' : 'Back ' ,
6684 variant : 'ghost' ,
67- onClick : cancelSurvey ,
85+ onClick : handleOnBack ,
6886 } ,
6987 {
7088 text :
You can’t perform that action at this time.
0 commit comments