Passed
Push — master ( 33b817...bb2a4c )
by Muhammad Dyas
01:42
created

state.ts ➔ getChoicesFromInput   A

Complexity

Conditions 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 4
1
import {ClosableType, PollConfig, PollFormInputs, PollState} from './interfaces';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
5
/**
6
 * Add a new option to the state(like DB)
7
 *
8
 * @param {string} option - the new option name
9
 * @param {object} state - the current message state
10
 * @param {string} creator - who add the new option
11
 * @returns {void} card
12
 */
13
export function addOptionToState(option: string, state: PollState, creator = '') {
14
  const choiceLength = state.choices.length;
15
  state.choices.push(option);
16
  if (state.choiceCreator === undefined) {
17
    state.choiceCreator = {[choiceLength]: creator};
18
  } else {
19
    state.choiceCreator[choiceLength] = creator;
20
  }
21
}
22
23
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) {
24
  const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card;
25
  return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) ||
26
    getStateFromCardWhenHasHeader(card);
27
}
28
29
function getChoicesFromInput(formValues: PollFormInputs) {
30
  const choices = [];
31
  for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
32
    const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim();
33
    if (choice) {
34
      choices.push(choice);
35
    }
36
  }
37
  return choices;
38
}
39
40
export function getConfigFromInput(formValues: PollFormInputs) {
41
  const state: PollConfig = {topic: '', choices: []};
42
  state.topic = formValues.topic.stringInputs!.value![0]!.trim() ?? '';
43
  state.anon = formValues.is_anonymous?.stringInputs!.value![0] === '1';
44
  state.optionable = formValues.allow_add_option?.stringInputs!.value![0] === '1';
45
  state.type = parseInt(formValues.type?.stringInputs!.value![0] ?? '1') as ClosableType;
46
  state.choices = getChoicesFromInput(formValues);
47
48
  return state;
49
}
50
51
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
52
  return card?.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
53
}
54
55
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
56
  // when has header the first section is header
57
  return card?.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
58
}
59
60
function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) {
61
  // when has header the first section is header
62
  return card?.name;
63
}
64
65
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) {
66
  const parameters = event.common?.parameters;
67
68
  return parameters?.['state'];
69
}
70