Passed
Pull Request — master (#1)
by Muhammad Dyas
02:04
created

config-form.ts ➔ buildConfigurationForm   B

Complexity

Conditions 4

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 58
c 0
b 0
f 0
rs 8.968
cc 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import {splitMessage} from './helpers/utils';
2
import {MAX_NUM_OF_OPTIONS} from './config/default';
3
4
/** Upper bounds on number of choices to present. */
5
6
/**
7
 * Build widget with instructions on how to use form.
8
 *
9
 * @returns {object} card widget
10
 */
11
function helpText() {
12
  return {
13
    textParagraph: {
14
      text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.',
15
    },
16
  };
17
}
18
19
/**
20
 * Build the text input for a choice.
21
 *
22
 * @param {number} index - Index to identify the choice
23
 * @param {string|undefined} value - Initial value to render (optional)
24
 * @returns {object} card widget
25
 */
26
function optionInput(index, value) {
27
  return {
28
    textInput: {
29
      label: `Option ${index + 1}`,
30
      type: 'SINGLE_LINE',
31
      name: `option${index}`,
32
      value: value || '',
33
    },
34
  };
35
}
36
37
/**
38
 * Build the text input for the poll topic.
39
 *
40
 * @param {string|undefined} topic - Initial value to render (optional)
41
 * @returns {object} card widget
42
 */
43
function topicInput(topic) {
44
  return {
45
    textInput: {
46
      label: 'Topic',
47
      type: 'MULTIPLE_LINE',
48
      name: 'topic',
49
      value: topic || '',
50
    },
51
  };
52
}
53
54
/**
55
 * Build the buttons/actions for the form.
56
 *
57
 * @returns {object} card widget
58
 */
59
function fixedFooter() {
60
  return {
61
    'primaryButton': {
62
      'text': 'Submit',
63
      'onClick': {
64
        'action': {
65
          'function': 'start_poll',
66
        },
67
      },
68
    },
69
  };
70
}
71
72
/**
73
 * Build the configuration form.
74
 *
75
 * @param {object} options - Initial state to render with form
76
 * @param {string|undefined} options.topic - Topic of poll (optional)
77
 * @param {string[]|undefined} options.choices - Text of choices to display to users (optional)
78
 * @returns {object} card
79
 */
80
export function buildConfigurationForm(options) {
81
  const widgets = [];
82
  widgets.push(helpText());
83
  widgets.push(topicInput(options.topic));
84
  for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
85
    const choice = options?.choices?.[i];
86
    widgets.push(optionInput(i, choice));
87
  }
88
89
  // Assemble the card
90
  return {
91
    'sections': [
92
      {
93
        'collapsible': true,
94
        'uncollapsibleWidgetsCount': 6,
95
        widgets,
96
      },
97
      {
98
        'widgets': [
99
          {
100
            'decoratedText': {
101
              'bottomLabel': 'If this checked the voters name will be not shown',
102
              'text': 'Anonymous voter',
103
              'switchControl': {
104
                'controlType': 'SWITCH',
105
                'name': 'is_anonymous',
106
                'value': '1',
107
                'selected': false,
108
              },
109
            },
110
            'horizontalAlignment': 'CENTER',
111
          },
112
          {
113
            'decoratedText': {
114
              'bottomLabel': 'After the poll is created, other member can add more option',
115
              'text': 'Allow to add more option(s)',
116
              'switchControl': {
117
                'controlType': 'SWITCH',
118
                'name': 'allow_add_option',
119
                'value': '1',
120
                'selected': true,
121
              },
122
            },
123
            'horizontalAlignment': 'CENTER',
124
          },
125
        ],
126
      },
127
    ],
128
    'fixedFooter': fixedFooter(),
129
  };
130
}
131
132
/**
133
 * Build poll options from message sent by user.
134
 *
135
 * @param {string} message - message or text after poll command
136
 * @returns {object} option
137
 */
138
export function buildOptionsFromMessage(message) {
139
  const explodedMesage = splitMessage(message);
140
  const topic = explodedMesage[0] !== 'undefined' && explodedMesage[0] ? explodedMesage[0] : '';
141
  if (explodedMesage.length > 0) {
142
    explodedMesage.shift();
143
  }
144
  return {
145
    topic,
146
    choices: explodedMesage,
147
  };
148
}
149