-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.tsx
103 lines (88 loc) · 2.91 KB
/
index.tsx
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
EventBotFaq,
EventFallback,
EventName,
} from '@botonic/plugin-hubtype-analytics/lib/cjs/types'
import { ActionRequest, Multichannel, RequestContext } from '@botonic/react'
import React from 'react'
import { FlowBuilderApi } from '../api'
import { FlowContent, FlowHandoff } from '../content-fields'
import { HtNodeWithContent } from '../content-fields/hubtype-fields'
import { getFlowBuilderPlugin } from '../helpers'
import { trackEvent } from './tracking'
import { getNodeByUserInput } from './user-input'
type FlowBuilderActionProps = {
contents: FlowContent[]
}
export class FlowBuilderAction extends React.Component<FlowBuilderActionProps> {
static contextType = RequestContext
static async botonicInit(
request: ActionRequest
): Promise<FlowBuilderActionProps> {
const flowBuilderPlugin = getFlowBuilderPlugin(request.plugins)
const locale = flowBuilderPlugin.getLocale(request.session)
const targetNode = await getTargetNode(
flowBuilderPlugin.cmsApi,
locale,
request
)
const contents = await flowBuilderPlugin.getContentsByNode(
targetNode,
locale
)
const handoffContent = contents.find(
content => content instanceof FlowHandoff
) as FlowHandoff
if (handoffContent) await handoffContent.doHandoff(request)
const renderContents = contents.filter(content =>
content instanceof FlowHandoff ? false : true
)
return { contents: renderContents }
}
render(): JSX.Element | JSX.Element[] {
const { contents } = this.props
return contents.map(content => content.toBotonic(content.id))
}
}
export class FlowBuilderMultichannelAction extends FlowBuilderAction {
render(): JSX.Element | JSX.Element[] {
const { contents } = this.props
return (
<Multichannel text={{ buttonsAsText: false }}>
{contents.map(content => content.toBotonic(content.id))}
</Multichannel>
)
}
}
async function getTargetNode(
cmsApi: FlowBuilderApi,
locale: string,
request: ActionRequest
) {
const contentId = request.input.payload
const targetNode = !contentId
? await getNodeByUserInput(cmsApi, locale, request)
: (cmsApi.getNodeById(contentId) as HtNodeWithContent)
if (targetNode) {
const event: EventBotFaq = {
event_type: EventName.botFaq,
event_data: {
faq_name: targetNode.code,
},
}
await trackEvent(request, event)
return targetNode
}
return await getFallbackNode(cmsApi, request)
}
async function getFallbackNode(cmsApi: FlowBuilderApi, request: ActionRequest) {
const isFirstFallbackOption =
request.session.user.extra_data.isFirstFallbackOption ?? true
const fallbackNode = cmsApi.getFallbackNode(isFirstFallbackOption)
request.session.user.extra_data.isFirstFallbackOption = !isFirstFallbackOption
const event: EventFallback = {
event_type: EventName.fallback,
}
await trackEvent(request, event)
return fallbackNode
}