1
|
|
|
import {splitMessage} from './helpers/utils'; |
2
|
|
|
import {MAX_NUM_OF_OPTIONS} from './config/default'; |
3
|
|
|
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1'; |
4
|
|
|
import {PollState} from './helpers/interfaces'; |
5
|
|
|
|
6
|
|
|
/** Upper bounds on number of choices to present. */ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Build widget with instructions on how to use form. |
10
|
|
|
* |
11
|
|
|
* @returns {object} card widget |
12
|
|
|
*/ |
13
|
|
|
function helpText() { |
14
|
|
|
return { |
15
|
|
|
textParagraph: { |
16
|
|
|
text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.', |
17
|
|
|
}, |
18
|
|
|
}; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Build the text input for a choice. |
23
|
|
|
* |
24
|
|
|
* @param {number} index - Index to identify the choice |
25
|
|
|
* @param {string|undefined} value - Initial value to render (optional) |
26
|
|
|
* @returns {object} card widget |
27
|
|
|
*/ |
28
|
|
|
function optionInput( |
29
|
|
|
index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget { |
30
|
|
|
return { |
31
|
|
|
textInput: { |
32
|
|
|
label: `Option ${index + 1}`, |
33
|
|
|
type: 'SINGLE_LINE', |
34
|
|
|
name: `option${index}`, |
35
|
|
|
value: value || '', |
36
|
|
|
}, |
37
|
|
|
}; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Build the text input for the poll topic. |
42
|
|
|
* |
43
|
|
|
* @param {string|undefined} topic - Initial value to render (optional) |
44
|
|
|
* @returns {object} card widget |
45
|
|
|
*/ |
46
|
|
|
function topicInput(topic: string) { |
47
|
|
|
return { |
48
|
|
|
textInput: { |
49
|
|
|
label: 'Topic', |
50
|
|
|
type: 'MULTIPLE_LINE', |
51
|
|
|
name: 'topic', |
52
|
|
|
value: topic, |
53
|
|
|
}, |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Build the buttons/actions for the form. |
59
|
|
|
* |
60
|
|
|
* @returns {object} card widget |
61
|
|
|
*/ |
62
|
|
|
function fixedFooter() { |
63
|
|
|
return { |
64
|
|
|
'primaryButton': { |
65
|
|
|
'text': 'Submit', |
66
|
|
|
'onClick': { |
67
|
|
|
'action': { |
68
|
|
|
'function': 'start_poll', |
69
|
|
|
}, |
70
|
|
|
}, |
71
|
|
|
}, |
72
|
|
|
}; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Build the configuration form. |
77
|
|
|
* |
78
|
|
|
* @param {object} options - Initial state to render with form |
79
|
|
|
* @param {string|undefined} options.topic - Topic of poll (optional) |
80
|
|
|
* @param {string[]|undefined} options.choices - Text of choices to display to users (optional) |
81
|
|
|
* @returns {object} card |
82
|
|
|
*/ |
83
|
|
|
export function buildConfigurationForm(options: PollState): chatV1.Schema$GoogleAppsCardV1Card { |
84
|
|
|
const widgets = []; |
85
|
|
|
widgets.push(helpText()); |
86
|
|
|
widgets.push(topicInput(options.topic)); |
87
|
|
|
for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) { |
88
|
|
|
const choice = options?.choices?.[i]; |
89
|
|
|
widgets.push(optionInput(i, choice)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Assemble the card |
93
|
|
|
return { |
94
|
|
|
'sections': [ |
95
|
|
|
{ |
96
|
|
|
'collapsible': true, |
97
|
|
|
'uncollapsibleWidgetsCount': 6, |
98
|
|
|
widgets, |
99
|
|
|
}, |
100
|
|
|
{ |
101
|
|
|
'widgets': [ |
102
|
|
|
{ |
103
|
|
|
'decoratedText': { |
104
|
|
|
'bottomLabel': 'If this checked the voters name will be not shown', |
105
|
|
|
'text': 'Anonymous voter', |
106
|
|
|
'switchControl': { |
107
|
|
|
'controlType': 'SWITCH', |
108
|
|
|
'name': 'is_anonymous', |
109
|
|
|
'value': '1', |
110
|
|
|
'selected': false, |
111
|
|
|
}, |
112
|
|
|
}, |
113
|
|
|
'horizontalAlignment': 'CENTER', |
114
|
|
|
}, |
115
|
|
|
{ |
116
|
|
|
'decoratedText': { |
117
|
|
|
'bottomLabel': 'After the poll is created, other member can add more option', |
118
|
|
|
'text': 'Allow to add more option(s)', |
119
|
|
|
'switchControl': { |
120
|
|
|
'controlType': 'SWITCH', |
121
|
|
|
'name': 'allow_add_option', |
122
|
|
|
'value': '1', |
123
|
|
|
'selected': true, |
124
|
|
|
}, |
125
|
|
|
}, |
126
|
|
|
'horizontalAlignment': 'CENTER', |
127
|
|
|
}, |
128
|
|
|
], |
129
|
|
|
}, |
130
|
|
|
], |
131
|
|
|
'fixedFooter': fixedFooter(), |
132
|
|
|
}; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Build poll options from message sent by user. |
137
|
|
|
* |
138
|
|
|
* @param {string} message - message or text after poll command |
139
|
|
|
* @returns {object} option |
140
|
|
|
*/ |
141
|
|
|
export function buildOptionsFromMessage(message: string): PollState { |
142
|
|
|
const explodedMesage = splitMessage(message); |
143
|
|
|
const topic = explodedMesage[0] !== 'undefined' && explodedMesage[0] ? |
144
|
|
|
explodedMesage[0] : |
145
|
|
|
''; |
146
|
|
|
if (explodedMesage.length > 0) { |
147
|
|
|
explodedMesage.shift(); |
148
|
|
|
} |
149
|
|
|
return { |
150
|
|
|
topic, |
151
|
|
|
choices: explodedMesage, |
152
|
|
|
}; |
153
|
|
|
} |
154
|
|
|
|