Skip to content

Commit

Permalink
refactor(plugin-flow-builder): use getResolvedLocale only inside getC…
Browse files Browse the repository at this point in the history
…ontentsByNode
  • Loading branch information
Iru89 committed Mar 5, 2024
1 parent badf45e commit 2b9e100
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
3 changes: 1 addition & 2 deletions packages/botonic-plugin-flow-builder/src/action/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ export class FlowBuilderAction extends React.Component<FlowBuilderActionProps> {
): Promise<FlowBuilderActionProps> {
const flowBuilderPlugin = getFlowBuilderPlugin(request.plugins)
const locale = flowBuilderPlugin.getLocale(request.session)
const localeResolved = flowBuilderPlugin.cmsApi.getLocaleResolved(locale)

const targetNode = await getTargetNode(flowBuilderPlugin.cmsApi, request)

const contents = await flowBuilderPlugin.getContentsByNode(
targetNode,
localeResolved
locale
)

const handoffContent = contents.find(
Expand Down
2 changes: 1 addition & 1 deletion packages/botonic-plugin-flow-builder/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class FlowBuilderApi {
return `${payloadNode.content.payload}${SEPARATOR}${payloadJson}`
}

getLocaleResolved(locale: string): string {
getResolvedLocale(locale: string): string {
if (this.flow.locales.find(flowLocale => flowLocale === locale)) {
return locale
}
Expand Down
24 changes: 10 additions & 14 deletions packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export default class BotonicPluginFlowBuilder implements Plugin {

if (checkUserTextInput) {
const locale = this.getLocale(request.session)
const localeResolved = this.cmsApi.getLocaleResolved(locale)
const resolvedLocale = this.cmsApi.getResolvedLocale(locale)
const nodeByUserInput = await getNodeByUserInput(
this.cmsApi,
localeResolved,
resolvedLocale,
request as unknown as ActionRequest
)
request.input.payload = this.cmsApi.getPayload(nodeByUserInput?.target)
Expand All @@ -96,8 +96,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
prevContents?: FlowContent[]
): Promise<FlowContent[]> {
const node = this.cmsApi.getNodeByCode(code) as HtNodeWithContent
const localeResolved = this.cmsApi.getLocaleResolved(locale)
return await this.getContentsByNode(node, localeResolved, prevContents)
return await this.getContentsByNode(node, locale, prevContents)
}

async getContentsById(
Expand All @@ -106,14 +105,12 @@ export default class BotonicPluginFlowBuilder implements Plugin {
prevContents?: FlowContent[]
): Promise<FlowContent[]> {
const node = this.cmsApi.getNodeById(id) as HtNodeWithContent
const localeResolved = this.cmsApi.getLocaleResolved(locale)
return await this.getContentsByNode(node, localeResolved, prevContents)
return await this.getContentsByNode(node, locale, prevContents)
}

async getStartContents(locale: string): Promise<FlowContent[]> {
const startNode = this.cmsApi.getStartNode()
const localeResolved = this.cmsApi.getLocaleResolved(locale)
return await this.getContentsByNode(startNode, localeResolved)
return await this.getContentsByNode(startNode, locale)
}

async getContentsByNode(
Expand All @@ -122,22 +119,21 @@ export default class BotonicPluginFlowBuilder implements Plugin {
prevContents?: FlowContent[]
): Promise<FlowContent[]> {
const contents = prevContents || []
const localeResolved = this.cmsApi.getLocaleResolved(locale)
const content = this.getFlowContent(node, localeResolved)
const resolvedLocale = this.cmsApi.getResolvedLocale(locale)

if (node.type === HtNodeWithContentType.FUNCTION) {
const targetId = await this.callFunction(node, localeResolved)
return this.getContentsById(targetId, localeResolved, contents)
const targetId = await this.callFunction(node, resolvedLocale)
return this.getContentsById(targetId, resolvedLocale, contents)
}

const content = this.getFlowContent(node, localeResolved)
const content = this.getFlowContent(node, resolvedLocale)
if (content) {
contents.push(content)
}
// TODO: prevent infinite recursive calls

if (node.follow_up) {
return this.getContentsById(node.follow_up.id, localeResolved, contents)
return this.getContentsById(node.follow_up.id, resolvedLocale, contents)
}

return contents
Expand Down

0 comments on commit 2b9e100

Please sign in to comment.