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