Passed
Pull Request — master (#67)
by Muhammad Dyas
02:38 queued 01:05
created

src/cards/PollDialogCard.ts   A

Complexity

Total Complexity 6
Complexity/F 2

Size

Lines of Code 65
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 50
mnd 3
bc 3
fnc 3
dl 0
loc 65
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A PollDialogCard.getUserVotes 0 13 4
A PollDialogCard.choice 0 25 1
A PollDialogCard.create 0 7 1
1
import PollCard from './PollCard';
2
import {LocaleTimezone, PollState, Voter} from '../helpers/interfaces';
3
import {chat_v1 as chatV1} from '@googleapis/chat';
4
import {progressBarText} from '../helpers/vote';
5
6
export default class PollDialogCard extends PollCard {
7
  private readonly voter: Voter;
8
  private userVotes: number[] | undefined;
9
10
  constructor(state: PollState, timezone: LocaleTimezone, voter: Voter) {
11
    super(state, timezone);
12
    this.voter = voter;
13
  }
14
  create() {
15
    this.buildHeader();
16
    this.buildSections();
17
    this.buildButtons();
18
    this.buildFooter();
19
    this.card.name = this.getSerializedState();
20
    return this.card;
21
  }
22
23
  getUserVotes(): number[] {
24
    if (this.state.votes === undefined) {
25
      return [];
26
    }
27
    const votes = [];
28
    const voter = this.voter;
29
    for (let i = 0; i < this.state.choices.length; i++) {
30
      if (this.state.votes[i] !== undefined && this.state.votes[i].findIndex((x) => x.uid === voter.uid) > -1) {
31
        votes.push(i);
32
      }
33
    }
34
    return votes;
35
  }
36
  choice(index: number, text: string, voteCount: number, totalVotes: number): chatV1.Schema$GoogleAppsCardV1Widget {
37
    this.userVotes = this.getUserVotes();
38
39
    const progressBar = progressBarText(voteCount, totalVotes);
40
41
    const voteSwitch: chatV1.Schema$GoogleAppsCardV1SwitchControl = {
42
      'controlType': 'SWITCH',
43
      'name': 'mySwitchControl',
44
      'value': 'myValue',
45
      'selected': this.userVotes.includes(index),
46
      'onChangeAction': {
47
        'function': 'switch_vote',
48
        'parameters': [
49
          {
50
            key: 'index',
51
            value: index.toString(10),
52
          },
53
        ],
54
      },
55
    };
56
    return {
57
      decoratedText: {
58
        'bottomLabel': `${progressBar} ${voteCount}`,
59
        'text': text,
60
        'switchControl': voteSwitch,
61
      },
62
    };
63
  }
64
}
65