-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.js
75 lines (62 loc) · 2.51 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Various helper functions
// Helper function and execution to determine the first field that should be prefilled when a message shortcut is used
const determineFieldNameForMessageShortcutPrefill = (fields) => {
for (const [fieldName, fieldConfig] of fields.entries()) {
const x = fieldConfig.messageShortcutPrefill ? fieldName : null
if (x) {
return x
}
}
}
// Extract values from view submission payload
const extractInputsFromViewSubmissionPayload = ({ view }, fieldConfig) => {
const fieldsWithValues = {}
// Loop through all values we received from Slack and extract the value depending on field type
Object.keys(view.state.values).forEach((fieldName) => {
// fieldName represents the Slack view block_id and action_id (we use the Fields' object key for both)
const inputReceived = view.state.values[fieldName][fieldName]
// Make a copy of the field config
const fieldConfigCopy = Object.assign({}, fieldConfig.get(fieldName))
// Determine the value
fieldConfigCopy.value = slackInputViewStateToValue(inputReceived)
// Add the field config to the fieldsWithValues object
fieldsWithValues[fieldName] = fieldConfigCopy
})
return fieldsWithValues
}
// Extract value from view submission payload
// TODO support additional Slack input element types (https://api.slack.com/reference/block-kit/blocks#input)
const slackInputViewStateToValue = (inputViewState) => {
switch (inputViewState.type) {
case 'plain_text_input':
return inputViewState.value
case 'static_select':
return inputViewState.selected_option.value
default:
return null
}
}
// Validate inputs and return error object
const validateInputs = (fieldsWithValues) => {
const errors = {}
// Loop through all fields validate the value (if a validation function is defined)
for (const fieldName of Object.keys(fieldsWithValues)) {
const fieldConfigWithValue = fieldsWithValues[fieldName]
if (fieldConfigWithValue.validationFn) {
const validationError = fieldConfigWithValue.validationFn(fieldConfigWithValue.value)
if (validationError !== true) { // true means no error
errors[fieldName] = validationError
}
}
}
return errors
}
const objectStringifiedAsMardownCodeBlock = (object, replacer = null, indent = 2) => {
return `\`\`\`\n${JSON.stringify(object, replacer, indent)}\`\`\``
}
module.exports = {
determineFieldNameForMessageShortcutPrefill,
extractInputsFromViewSubmissionPayload,
validateInputs,
objectStringifiedAsMardownCodeBlock
}