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

vote-card.ts ➔ buildVoteCard   B

Complexity

Conditions 5

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 61
c 0
b 0
f 0
rs 8.5733
cc 5

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 {choiceSection} from './helpers/vote';
2
import {ICON_URL_48X48} from './config/default';
3
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
4
5
/**
6
 * Builds the card header including the question and author details.
7
 *
8
 * @param {string} topic - Topic of the poll
9
 * @param {string} author - Display name of user that created the poll
10
 * @returns {object} card widget
11
 */
12
function cardHeader(topic, author) {
13
  return {
14
    title: topic,
15
    subtitle: `Posted by ${author}`,
16
    imageUrl: ICON_URL_48X48,
17
    imageType: 'CIRCLE',
18
  };
19
}
20
21
/**
22
 * Builds the section header if the topic is too long
23
 *
24
 * @param {string} topic - Topic of the poll
25
 * @param {string} author - Display name of user that created the poll
26
 * @returns {object} card section
27
 */
28
function sectionHeader(topic, author) {
29
  return {
30
    widgets: [
31
      {
32
        'decoratedText': {
33
          'text': topic,
34
          'wrapText': true,
35
          'bottomLabel': `Posted by ${author}`,
36
          'startIcon': {
37
            'altText': 'Absolute Poll',
38
            'iconUrl': ICON_URL_48X48,
39
            'imageType': 'SQUARE',
40
          },
41
        },
42
      },
43
    ],
44
  };
45
}
46
47
/**
48
 * Builds the configuration form.
49
 *
50
 * @param {object} poll - Current state of poll
51
 * @param {object} poll.author - User that submitted the poll
52
 * @param {string} poll.topic - Topic of poll
53
 * @param {string[]} poll.choices - Text of choices to display to users
54
 * @param {object} poll.votes - Map of cast votes keyed by choice index
55
 * @param {object} poll.choiceCreator - Map of cast votes keyed by choice index
56
 * @param {boolean} poll.anon - Is anonymous?(will save voter name or not)
57
 * @param {boolean} poll.optionable - Can other user add other option?
58
 * @returns {object} card
59
 */
60
export function buildVoteCard(poll) {
61
  const sections = [];
62
  const state = JSON.stringify(poll);
63
64
  const votes: Array<Array<object>> = Object.values(poll.votes);
65
  const totalVotes: number = votes.reduce((sum, vote) => sum + vote.length, 0);
66
  for (let i = 0; i < poll.choices.length; ++i) {
67
    const creator = poll.choiceCreator?.[i];
68
    const section = choiceSection(i, poll, totalVotes, state, creator);
69
    sections.push(section);
70
  }
71
  if (poll.optionable) {
72
    sections.push(
73
        {
74
          'widgets': [
75
            {
76
              'buttonList': {
77
                'buttons': [
78
                  {
79
                    'text': 'Add Option',
80
                    'onClick': {
81
                      'action': {
82
                        'function': 'add_option_form',
83
                        'interaction': 'OPEN_DIALOG',
84
                        'parameters': [],
85
                      },
86
                    },
87
                  },
88
                ],
89
              },
90
            },
91
          ],
92
        });
93
  }
94
  const card: chatV1.Schema$CardWithId = {
95
    'cardId': 'unique-card-id',
96
    'card': {
97
      sections,
98
    },
99
  };
100
  if (poll.topic.length > 40) {
101
    const widgetHeader = sectionHeader(poll.topic, poll.author.displayName);
102
    card.card.sections = [widgetHeader, ...sections];
103
  } else {
104
    card.card.header = cardHeader(poll.topic, poll.author.displayName);
105
  }
106
  return card;
107
}
108