Completed
Push — master ( 8e01eb...25ec60 )
by Muhammad Dyas
17s queued 12s
created

NewPollFormCard.buildOptionSwitchSection   B

Complexity

Conditions 7

Size

Total Lines 54
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 54
c 0
b 0
f 0
rs 7.448
cc 7

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 BaseCard from './BaseCard';
2
import {ClosableType, PollConfig} from '../helpers/interfaces';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
5
6
export default class NewPollFormCard extends BaseCard {
7
  private config: PollConfig;
8
9
  constructor(config: PollConfig) {
10
    super();
11
    this.config = config;
12
  }
13
14
  create() {
15
    this.buildSections();
16
    this.buildFooter();
17
    return this.card;
18
  }
19
20
  buildSections() {
21
    this.buildTopicInputSection();
22
    this.buildOptionSwitchSection();
23
  }
24
25
  buildTopicInputSection() {
26
    const widgets = [];
27
    widgets.push(this.buildHelpText());
28
    widgets.push(this.topicInput(this.config.topic));
29
    for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
30
      const choice = this.config?.choices?.[i];
31
      widgets.push(this.optionInput(i, choice));
32
    }
33
    this.card.sections!.push({
34
      'collapsible': true,
35
      'uncollapsibleWidgetsCount': 6,
36
      widgets,
37
    });
38
  }
39
40
  buildOptionSwitchSection() {
41
    this.card.sections!.push({
42
      'widgets': [
43
        {
44
          'decoratedText': {
45
            'bottomLabel': 'If this checked the voters name will be not shown',
46
            'text': 'Anonymous voter',
47
            'switchControl': {
48
              'controlType': 'SWITCH',
49
              'name': 'is_anonymous',
50
              'value': '1',
51
              'selected': this.config?.anon ?? false,
52
            },
53
          },
54
          'horizontalAlignment': 'CENTER',
55
        },
56
        {
57
          'decoratedText': {
58
            'bottomLabel': 'After the poll is created, other member can add more option',
59
            'text': 'Allow to add more option(s)',
60
            'switchControl': {
61
              'controlType': 'SWITCH',
62
              'name': 'allow_add_option',
63
              'value': '1',
64
              'selected': this.config?.optionable ?? true,
65
            },
66
          },
67
          'horizontalAlignment': 'CENTER',
68
        },
69
        {
70
          'selectionInput': {
71
            'type': 'DROPDOWN',
72
            'label': 'Allow to close poll',
73
            'name': 'type',
74
            'items': [
75
              {
76
                'text': 'Yes, but only creator',
77
                'value': '1',
78
                'selected': this.config.type === ClosableType.CLOSEABLE_BY_CREATOR,
79
              },
80
              {
81
                'text': 'Yes, anyone can close',
82
                'value': '2',
83
                'selected': this.config.type === ClosableType.CLOSEABLE_BY_ANYONE,
84
              },
85
              {
86
                'text': 'No, I want unclosable poll',
87
                'value': '0',
88
                'selected': this.config.type === ClosableType.UNCLOSEABLE,
89
              },
90
            ],
91
          },
92
          'horizontalAlignment': 'START',
93
        },
94
      ],
95
    });
96
  }
97
98
  buildHelpText() {
99
    return {
100
      textParagraph: {
101
        text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.',
102
      },
103
    };
104
  }
105
106
  topicInput(topic: string) {
107
    return {
108
      textInput: {
109
        label: 'Topic',
110
        type: 'MULTIPLE_LINE',
111
        name: 'topic',
112
        value: topic,
113
      },
114
    };
115
  }
116
117
  optionInput(
118
    index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget {
119
    return {
120
      textInput: {
121
        label: `Option ${index + 1}`,
122
        type: 'SINGLE_LINE',
123
        name: `option${index}`,
124
        value: value || '',
125
      },
126
    };
127
  }
128
129
  buildFooter() {
130
    this.card.fixedFooter = {
131
      'primaryButton': {
132
        'text': 'Submit',
133
        'onClick': {
134
          'action': {
135
            'function': 'start_poll',
136
          },
137
        },
138
      },
139
    };
140
  }
141
}
142