Passed
Pull Request — master (#2)
by Muhammad Dyas
02:13
created

src/cards/NewPollFormCard.ts   A

Complexity

Total Complexity 10
Complexity/F 1.43

Size

Lines of Code 113
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 82
mnd 3
bc 3
fnc 7
dl 0
loc 113
rs 10
bpm 0.4285
cpm 1.4285
noi 0
c 0
b 0
f 0

7 Functions

Rating   Name   Duplication   Size   Complexity  
A NewPollFormCard.buildOptionSwitchSection 0 29 1
A NewPollFormCard.buildHelpText 0 5 1
A NewPollFormCard.buildFooter 0 8 1
A NewPollFormCard.topicInput 0 8 1
A NewPollFormCard.buildSections 0 4 1
A NewPollFormCard.optionInput 0 9 1
A NewPollFormCard.buildTopicInputSection 0 13 4
1
import {BaseCard} from './BaseCard';
2
import {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 class NewPollFormCard extends BaseCard {
7
  private config: PollConfig;
8
9
  constructor(config: PollConfig) {
10
    super();
11
    this.config = config;
12
    this.buildSections();
13
    this.buildFooter();
14
  }
15
16
  buildSections() {
17
    this.buildTopicInputSection();
18
    this.buildOptionSwitchSection();
19
  }
20
21
  buildTopicInputSection() {
22
    const widgets = [];
23
    widgets.push(this.buildHelpText());
24
    widgets.push(this.topicInput(this.config.topic));
25
    for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
26
      const choice = this.config?.choices?.[i];
27
      widgets.push(this.optionInput(i, choice));
28
    }
29
    this.card.sections!.push({
30
      'collapsible': true,
31
      'uncollapsibleWidgetsCount': 6,
32
      widgets,
33
    });
34
  }
35
36
  buildOptionSwitchSection() {
37
    this.card.sections!.push({
38
      'widgets': [
39
        {
40
          'decoratedText': {
41
            'bottomLabel': 'If this checked the voters name will be not shown',
42
            'text': 'Anonymous voter',
43
            'switchControl': {
44
              'controlType': 'SWITCH',
45
              'name': 'is_anonymous',
46
              'value': '1',
47
              'selected': false,
48
            },
49
          },
50
          'horizontalAlignment': 'CENTER',
51
        },
52
        {
53
          'decoratedText': {
54
            'bottomLabel': 'After the poll is created, other member can add more option',
55
            'text': 'Allow to add more option(s)',
56
            'switchControl': {
57
              'controlType': 'SWITCH',
58
              'name': 'allow_add_option',
59
              'value': '1',
60
              'selected': true,
61
            },
62
          },
63
          'horizontalAlignment': 'CENTER',
64
        },
65
      ],
66
    });
67
  }
68
69
  buildHelpText() {
70
    return {
71
      textParagraph: {
72
        text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.',
73
      },
74
    };
75
  }
76
77
  topicInput(topic: string) {
78
    return {
79
      textInput: {
80
        label: 'Topic',
81
        type: 'MULTIPLE_LINE',
82
        name: 'topic',
83
        value: topic,
84
      },
85
    };
86
  }
87
88
  optionInput(
89
    index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget {
90
    return {
91
      textInput: {
92
        label: `Option ${index + 1}`,
93
        type: 'SINGLE_LINE',
94
        name: `option${index}`,
95
        value: value || '',
96
      },
97
    };
98
  }
99
100
  buildFooter() {
101
    this.card.fixedFooter = {
102
      'primaryButton': {
103
        'text': 'Submit',
104
        'onClick': {
105
          'action': {
106
            'function': 'start_poll',
107
          },
108
        },
109
      },
110
    };
111
  }
112
}
113