Verified Commit 6f0b826f authored by Marek Veselý's avatar Marek Veselý
Browse files

fix: optional free-form questions

parent 29cc31ec
Loading
Loading
Loading
Loading
+44 −38
Original line number Diff line number Diff line
@@ -94,23 +94,26 @@ const AnswerState = (
        let result = true
        set(({ questions }) => ({
          questions: questions.map(question => {
            let error = ''
            if (question.answer.length === 0) {
            const answerOptional =
              question.question.details.__typename ===
                'FreeFormQuestionDetailsType' &&
              question.question.details.min === 0
            if (!answerOptional && question.answer.length === 0) {
              result = false
              error = t('questionnaire.answerRequired')
            } else {
              return {
                ...question,
                error: t('questionnaire.answerRequired'),
              }
            }

            let error = ''
            if (
              question.question.details.__typename ===
                'FreeFormQuestionDetailsType' ||
              question.question.details.__typename ===
                'AutoFreeFormQuestionDetailsType'
            ) {
                const answer = question.answer.at(0)
                if (answer === undefined) {
                  throw new Error(
                    'FreeFormQuestionDetailsType should have exactly one answer'
                  )
                }
              const answer = question.answer.at(0) || ''

              const { min, max } = question.question.details
              if (answer.length < min) {
@@ -136,7 +139,7 @@ const AnswerState = (
                }
              }
            }
            }

            return {
              ...question,
              error: error || '',
@@ -238,7 +241,10 @@ const TraineeQuestionnaireContent: FC<TraineeQuestionnaireContentProps> = ({
            questInput: {
              answers: questionsAndAnswers.map(questionAndAnswer => ({
                questionId: questionAndAnswer.question.id,
                value: questionAndAnswer.answer,
                value:
                  questionAndAnswer.answer.length === 0
                    ? ['']
                    : questionAndAnswer.answer,
              })),
              questionnaireId,
              teamId,
+1 −0
Original line number Diff line number Diff line
@@ -1072,6 +1072,7 @@ export const Question = graphql(
      }
      type
      details {
        __typename
        ... on RadioQuestionDetailsType {
          ...RadioQuestionDetails
        }
+23 −23

File changed.

Preview size limit exceeded, changes collapsed.