2
0

chore(editor): 👔 Send email disabled in preview

This commit is contained in:
Baptiste Arnaud
2022-02-13 07:49:56 +01:00
parent f57827c530
commit f8a64151ef
7 changed files with 41 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import {
Flex, Flex,
FlexProps, FlexProps,
useEventListener, useEventListener,
useToast,
VStack, VStack,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { TypebotViewer } from 'bot-engine' import { TypebotViewer } from 'bot-engine'
@@ -13,7 +14,7 @@ import { headerHeight } from 'components/shared/TypebotHeader'
import { useEditor } from 'contexts/EditorContext' import { useEditor } from 'contexts/EditorContext'
import { useGraph } from 'contexts/GraphContext' import { useGraph } from 'contexts/GraphContext'
import { useTypebot } from 'contexts/TypebotContext/TypebotContext' import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
import React, { useMemo, useState } from 'react' import React, { useEffect, useMemo, useState } from 'react'
import { parseTypebotToPublicTypebot } from 'services/publicTypebot' import { parseTypebotToPublicTypebot } from 'services/publicTypebot'
export const PreviewDrawer = () => { export const PreviewDrawer = () => {
@@ -30,6 +31,10 @@ export const PreviewDrawer = () => {
[typebot] [typebot]
) )
const toast = useToast({
position: 'top-right',
})
const handleMouseDown = () => { const handleMouseDown = () => {
setIsResizing(true) setIsResizing(true)
} }
@@ -52,6 +57,19 @@ export const PreviewDrawer = () => {
setRightPanel(undefined) setRightPanel(undefined)
} }
useEffect(() => {
const onMessageFromBot = (event: MessageEvent) => {
console.log(event)
if (event.data.typebotInfo) {
toast({ description: event.data.typebotInfo })
}
}
window.addEventListener('message', onMessageFromBot)
return () => {
window.removeEventListener('message', onMessageFromBot)
}
})
return ( return (
<Flex <Flex
pos="absolute" pos="absolute"
@@ -93,6 +111,7 @@ export const PreviewDrawer = () => {
<TypebotViewer <TypebotViewer
typebot={publicTypebot} typebot={publicTypebot}
onNewBlockVisible={setPreviewingEdge} onNewBlockVisible={setPreviewingEdge}
isPreview
/> />
</Flex> </Flex>
)} )}

View File

@@ -70,11 +70,8 @@ test.describe('Send email step', () => {
await page.click('text=Preview') await page.click('text=Preview')
await typebotViewer(page).locator('text=Go').click() await typebotViewer(page).locator('text=Go').click()
await page.waitForResponse( await expect(
(resp) => page.locator('text=Emails are not sent in preview mode')
resp.request().url().includes('/api/integrations/email') && ).toBeVisible()
resp.status() === 200 &&
resp.request().method() === 'POST'
)
}) })
}) })

View File

@@ -31,7 +31,8 @@ export const ChatBlock = ({
onScroll, onScroll,
onBlockEnd, onBlockEnd,
}: ChatBlockProps) => { }: ChatBlockProps) => {
const { typebot, updateVariableValue, createEdge, apiHost } = useTypebot() const { typebot, updateVariableValue, createEdge, apiHost, isPreview } =
useTypebot()
const [displayedSteps, setDisplayedSteps] = useState<PublicStep[]>([]) const [displayedSteps, setDisplayedSteps] = useState<PublicStep[]>([])
const currentStepIndex = displayedSteps.length - 1 const currentStepIndex = displayedSteps.length - 1
@@ -67,6 +68,7 @@ export const ChatBlock = ({
typebotId: typebot.id, typebotId: typebot.id,
indices: { blockIndex, stepIndex: currentStepIndex }, indices: { blockIndex, stepIndex: currentStepIndex },
variables: typebot.variables, variables: typebot.variables,
isPreview,
updateVariableValue, updateVariableValue,
}, },
}) })

View File

@@ -14,6 +14,7 @@ import { Answer, BackgroundType, Edge, PublicTypebot } from 'models'
export type TypebotViewerProps = { export type TypebotViewerProps = {
typebot: PublicTypebot typebot: PublicTypebot
isPreview?: boolean
apiHost?: string apiHost?: string
onNewBlockVisible?: (edge: Edge) => void onNewBlockVisible?: (edge: Edge) => void
onNewAnswer?: (answer: Answer) => void onNewAnswer?: (answer: Answer) => void
@@ -22,6 +23,7 @@ export type TypebotViewerProps = {
export const TypebotViewer = ({ export const TypebotViewer = ({
typebot, typebot,
apiHost = process.env.NEXT_PUBLIC_VIEWER_HOST, apiHost = process.env.NEXT_PUBLIC_VIEWER_HOST,
isPreview = false,
onNewBlockVisible, onNewBlockVisible,
onNewAnswer, onNewAnswer,
onCompleted, onCompleted,
@@ -66,7 +68,7 @@ export const TypebotViewer = ({
}:wght@300;400;600&display=swap');`, }:wght@300;400;600&display=swap');`,
}} }}
/> />
<TypebotContext typebot={typebot} apiHost={apiHost}> <TypebotContext typebot={typebot} apiHost={apiHost} isPreview={isPreview}>
<AnswersContext> <AnswersContext>
<div <div
className="flex text-base overflow-hidden bg-cover h-screen w-screen flex-col items-center typebot-container" className="flex text-base overflow-hidden bg-cover h-screen w-screen flex-col items-center typebot-container"

View File

@@ -4,6 +4,7 @@ import React, { createContext, ReactNode, useContext, useState } from 'react'
const typebotContext = createContext<{ const typebotContext = createContext<{
typebot: PublicTypebot typebot: PublicTypebot
apiHost: string apiHost: string
isPreview: boolean
updateVariableValue: (variableId: string, value: string) => void updateVariableValue: (variableId: string, value: string) => void
createEdge: (edge: Edge) => void createEdge: (edge: Edge) => void
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -14,10 +15,12 @@ export const TypebotContext = ({
children, children,
typebot, typebot,
apiHost, apiHost,
isPreview,
}: { }: {
children: ReactNode children: ReactNode
typebot: PublicTypebot typebot: PublicTypebot
apiHost: string apiHost: string
isPreview: boolean
}) => { }) => {
const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot) const [localTypebot, setLocalTypebot] = useState<PublicTypebot>(typebot)
@@ -42,6 +45,7 @@ export const TypebotContext = ({
value={{ value={{
typebot: localTypebot, typebot: localTypebot,
apiHost, apiHost,
isPreview,
updateVariableValue, updateVariableValue,
createEdge, createEdge,
}} }}

View File

@@ -15,6 +15,7 @@ import {
import { stringify } from 'qs' import { stringify } from 'qs'
import { sendRequest } from 'utils' import { sendRequest } from 'utils'
import { sendGaEvent } from '../../lib/gtag' import { sendGaEvent } from '../../lib/gtag'
import { sendInfoMessage } from './postMessage'
import { parseVariables, parseVariablesInObject } from './variable' import { parseVariables, parseVariablesInObject } from './variable'
const safeEval = eval const safeEval = eval
@@ -24,6 +25,7 @@ type IntegrationContext = {
apiHost: string apiHost: string
typebotId: string typebotId: string
indices: Indices indices: Indices
isPreview: boolean
variables: Variable[] variables: Variable[]
updateVariableValue: (variableId: string, value: string) => void updateVariableValue: (variableId: string, value: string) => void
} }
@@ -177,8 +179,10 @@ const executeWebhook = async (
const sendEmail = async ( const sendEmail = async (
step: SendEmailStep, step: SendEmailStep,
{ variables, apiHost }: IntegrationContext { variables, apiHost, isPreview }: IntegrationContext
) => { ) => {
if (isPreview) sendInfoMessage('Emails are not sent in preview mode')
if (isPreview) return step.outgoingEdgeId
const { options } = step const { options } = step
const { error } = await sendRequest({ const { error } = await sendRequest({
url: `${apiHost}/api/integrations/email`, url: `${apiHost}/api/integrations/email`,

View File

@@ -0,0 +1,3 @@
export const sendInfoMessage = (typebotInfo: string) => {
parent.postMessage({ typebotInfo })
}