Completed
Push — master ( 4fcad4...efcce7 )
by Muhammad Dyas
19s queued 16s
created

src/cards/NewPollFormCard.ts   A

Complexity

Total Complexity 11
Complexity/F 1.38

Size

Lines of Code 116
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 84
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 11
mnd 3
bc 3
fnc 8
bpm 0.375
cpm 1.375
noi 0

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