Fakultas Ilmu Komputer UI

utils.ts 1.96 KB
Newer Older
1
2
3
import {
  dietReportTextFields,
  dietReportSelectFields,
4
} from 'constants/weeklyReport';
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { answerTypes } from 'scenes/questionnaire/ReadOnlyDietProfile/pages/types';
import { getSelectedAnswerString } from 'scenes/questionnaire/ReadOnlyDietProfile/pages/utils';
import { dietReportPage } from './types';

export const getQuestionAnswerCommentString = (
  page: string,
  question: string,
  answer: any,
  answerType: string,
  getTextInputProps: any,
): {
  label: string;
  answer: string;
  textInputProps: any;
  lowestScoreDescription: string | null;
  highestScoreDescription: string | null;
} => {
  let finalLowestScoreDescription = null;
  let finalHighestScoreDescription = null;

  let combinedFields = [];
  if (page === dietReportPage.PAGE1) {
    combinedFields = dietReportTextFields[page];
  } else if (page === dietReportPage.PAGE3) {
    combinedFields = dietReportSelectFields[page];
  } else if (page === dietReportPage.PAGE2 || page === dietReportPage.PAGE4) {
    combinedFields = dietReportSelectFields[page].concat(
      dietReportTextFields[page],
    );
  }

  let finalAnswer = answer;
  if (answerType === answerTypes.SELECT) {
    finalAnswer = getSelectedAnswerString(
      page,
      combinedFields,
      question,
      answer,
    );
    const lowestScore = combinedFields.find((qst) => qst.name === question)!
      .lowestScoreDescription;
    const highestScore = combinedFields.find((qst) => qst.name === question)!
      .highestScoreDescription;
    if (lowestScore && highestScore) {
      finalLowestScoreDescription = lowestScore;
      finalHighestScoreDescription = highestScore;
    }
  }
  if (finalAnswer === '') {
    finalAnswer = '-';
  }

  return {
    label: combinedFields.find((qst) => qst.name === question)!.label,
    answer: finalAnswer,
60
    textInputProps: getTextInputProps ? getTextInputProps(question) : null,
61
62
63
64
    lowestScoreDescription: finalLowestScoreDescription,
    highestScoreDescription: finalHighestScoreDescription,
  };
};