(webhook) Enable advanced config for Zapier and Make.com

This commit is contained in:
Baptiste Arnaud
2023-03-06 10:42:17 +01:00
parent 5bda556200
commit c1a636b965
9 changed files with 479 additions and 328 deletions

View File

@@ -1,7 +1,6 @@
import { Text } from '@chakra-ui/react'
import { useTypebot } from '@/features/editor'
import { defaultWebhookAttributes, MakeComBlock, Webhook } from 'models'
import { useEffect } from 'react'
import { MakeComBlock } from 'models'
import { byId, isNotDefined } from 'utils'
type Props = {
@@ -9,22 +8,9 @@ type Props = {
}
export const MakeComContent = ({ block }: Props) => {
const { webhooks, typebot, updateWebhook } = useTypebot()
const { webhooks } = useTypebot()
const webhook = webhooks.find(byId(block.webhookId))
useEffect(() => {
if (!typebot) return
if (!webhook) {
const { webhookId } = block
const newWebhook = {
id: webhookId,
...defaultWebhookAttributes,
typebotId: typebot.id,
} as Webhook
updateWebhook(webhookId, newWebhook)
}
}, [block, typebot, updateWebhook, webhook])
if (isNotDefined(webhook?.body))
return <Text color="gray.500">Configure...</Text>
return (

View File

@@ -1,31 +1,68 @@
import {
Alert,
AlertIcon,
Button,
Input,
Link,
Stack,
Text,
} from '@chakra-ui/react'
import { Alert, AlertIcon, Button, Link, Stack, Text } from '@chakra-ui/react'
import { ExternalLinkIcon } from '@/components/icons'
import { useTypebot } from '@/features/editor'
import { MakeComBlock } from 'models'
import React from 'react'
import { byId } from 'utils'
import { MakeComBlock, Webhook, WebhookOptions } from 'models'
import React, { useCallback, useEffect, useState } from 'react'
import { byId, env } from 'utils'
import { WebhookAdvancedConfigForm } from '../../webhook/components/WebhookAdvancedConfigForm'
import { useDebouncedCallback } from 'use-debounce'
const debounceWebhookTimeout = 2000
type Props = {
block: MakeComBlock
onOptionsChange: (options: WebhookOptions) => void
}
export const MakeComSettings = ({ block }: Props) => {
const { webhooks } = useTypebot()
const webhook = webhooks.find(byId(block.webhookId))
export const MakeComSettings = ({
block: { webhookId, id: blockId, options },
onOptionsChange,
}: Props) => {
const { webhooks, updateWebhook } = useTypebot()
const webhook = webhooks.find(byId(webhookId))
const [localWebhook, _setLocalWebhook] = useState(webhook)
const updateWebhookDebounced = useDebouncedCallback(
async (newLocalWebhook) => {
await updateWebhook(newLocalWebhook.id, newLocalWebhook)
},
env('E2E_TEST') === 'true' ? 0 : debounceWebhookTimeout
)
const setLocalWebhook = useCallback(
(newLocalWebhook: Webhook) => {
_setLocalWebhook(newLocalWebhook)
updateWebhookDebounced(newLocalWebhook)
},
[updateWebhookDebounced]
)
useEffect(() => {
if (
!localWebhook ||
localWebhook.url ||
!webhook?.url ||
webhook.url === localWebhook.url
)
return
setLocalWebhook({
...localWebhook,
url: webhook?.url,
})
}, [webhook, localWebhook, setLocalWebhook])
useEffect(
() => () => {
updateWebhookDebounced.flush()
},
[updateWebhookDebounced]
)
return (
<Stack spacing={4}>
<Alert status={webhook?.url ? 'success' : 'info'} rounded="md">
<Alert status={localWebhook?.url ? 'success' : 'info'} rounded="md">
<AlertIcon />
{webhook?.url ? (
{localWebhook?.url ? (
<>Your scenario is correctly configured 🚀</>
) : (
<Stack>
@@ -41,7 +78,15 @@ export const MakeComSettings = ({ block }: Props) => {
</Stack>
)}
</Alert>
{webhook?.url && <Input value={webhook?.url} isDisabled />}
{localWebhook && (
<WebhookAdvancedConfigForm
blockId={blockId}
webhook={localWebhook}
options={options}
onWebhookChange={setLocalWebhook}
onOptionsChange={onOptionsChange}
/>
)}
</Stack>
)
}