Skip to content

Commit

Permalink
🚸 (bot) Avoid waiting for blocks with no returned data
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Nov 16, 2022
1 parent 6725c17 commit 2bd7cee
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const BlockNode = ({
setFocusedGroupId,
previewingEdge,
} = useGraph()
const { mouseOverBlock, setMouseOverBlock, draggedItem } = useBlockDnd()
const { mouseOverBlock, setMouseOverBlock } = useBlockDnd()
const { typebot, updateBlock } = useTypebot()
const [isConnecting, setIsConnecting] = useState(false)
const [isPopoverOpened, setIsPopoverOpened] = useState(
Expand Down Expand Up @@ -105,7 +105,7 @@ export const BlockNode = ({
}

const handleMouseEnter = () => {
if (draggedItem !== undefined)
if (mouseOverBlock?.id !== block.id)
setMouseOverBlock({ id: block.id, ref: blockRef })
if (connectingIds)
setConnectingIds({
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
"path": "node_modules/cz-emoji"
}
},
"packageManager": "pnpm@7.14.0"
"packageManager": "pnpm@7.16.1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ if (window.$chatwoot) {
})(document, "script");
}`

export const executeChatwootBlock = async (
export const executeChatwootBlock = (
block: ChatwootBlock,
{ variables, isPreview, onNewLog }: IntegrationState
) => {
Expand All @@ -74,7 +74,7 @@ export const executeChatwootBlock = async (
)
)
try {
await func(...variables.map((v) => parseCorrectValueType(v.value)))
func(...variables.map((v) => parseCorrectValueType(v.value)))
} catch (err) {
console.error(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export const executeGoogleSheetBlock = async (
if (!('action' in block.options)) return block.outgoingEdgeId
switch (block.options.action) {
case GoogleSheetsAction.INSERT_ROW:
await insertRowInGoogleSheets(block.options, context)
insertRowInGoogleSheets(block.options, context)
break
case GoogleSheetsAction.UPDATE_ROW:
await updateRowInGoogleSheets(block.options, context)
updateRowInGoogleSheets(block.options, context)
break
case GoogleSheetsAction.GET:
await getRowFromGoogleSheets(block.options, context)
Expand All @@ -33,7 +33,7 @@ export const executeGoogleSheetBlock = async (
return block.outgoingEdgeId
}

const insertRowInGoogleSheets = async (
const insertRowInGoogleSheets = (
options: GoogleSheetsInsertRowOptions,
{ variables, apiHost, onNewLog, resultId }: IntegrationState
) => {
Expand All @@ -46,30 +46,31 @@ const insertRowInGoogleSheets = async (
return
}
const params = stringify({ resultId })
const { error } = await sendRequest({
sendRequest({
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${params}`,
method: 'POST',
body: {
credentialsId: options.credentialsId,
values: parseCellValues(options.cellsToInsert, variables),
},
})
onNewLog(
parseLog(
error,
'Succesfully inserted a row in the sheet',
'Failed to insert a row in the sheet'
}).then(({ error }) => {
onNewLog(
parseLog(
error,
'Succesfully inserted a row in the sheet',
'Failed to insert a row in the sheet'
)
)
)
})
}

const updateRowInGoogleSheets = async (
const updateRowInGoogleSheets = (
options: GoogleSheetsUpdateRowOptions,
{ variables, apiHost, onNewLog, resultId }: IntegrationState
) => {
if (!options.cellsToUpsert || !options.referenceCell) return
const params = stringify({ resultId })
const { error } = await sendRequest({
sendRequest({
url: `${apiHost}/api/integrations/google-sheets/spreadsheets/${options.spreadsheetId}/sheets/${options.sheetId}?${params}`,
method: 'PATCH',
body: {
Expand All @@ -80,14 +81,15 @@ const updateRowInGoogleSheets = async (
value: parseVariables(variables)(options.referenceCell.value ?? ''),
},
},
})
onNewLog(
parseLog(
error,
'Succesfully updated a row in the sheet',
'Failed to update a row in the sheet'
}).then(({ error }) => {
onNewLog(
parseLog(
error,
'Succesfully updated a row in the sheet',
'Failed to update a row in the sheet'
)
)
)
})
}

const getRowFromGoogleSheets = async (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parseLog } from '@/utils/helpers'
import { SendEmailBlock } from 'models'
import { sendRequest, byId } from 'utils'

export const executeSendEmailBlock = async (
export const executeSendEmailBlock = (
block: SendEmailBlock,
{
variables,
Expand All @@ -25,7 +25,7 @@ export const executeSendEmailBlock = async (
return block.outgoingEdgeId
}
const { options } = block
const { error } = await sendRequest({
sendRequest({
url: `${apiHost}/api/typebots/${typebotId}/integrations/email?resultId=${resultId}`,
method: 'POST',
body: {
Expand All @@ -43,9 +43,11 @@ export const executeSendEmailBlock = async (
isBodyCode: options.isBodyCode,
resultValues,
},
}).then(({ error }) => {
onNewLog(
parseLog(error, 'Succesfully sent an email', 'Failed to send an email')
)
})
onNewLog(
parseLog(error, 'Succesfully sent an email', 'Failed to send an email')
)

return block.outgoingEdgeId
}
2 changes: 1 addition & 1 deletion packages/bot-engine/src/utils/executeIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const executeIntegration = ({
}: {
block: IntegrationBlock
context: IntegrationState
}): Promise<string | undefined> => {
}): Promise<string | undefined> | string | undefined => {
switch (block.type) {
case IntegrationBlockType.GOOGLE_SHEETS:
return executeGoogleSheetBlock(block, context)
Expand Down
2 changes: 1 addition & 1 deletion packages/bot-engine/src/utils/executeLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export const executeLogic = async (
case LogicBlockType.CODE:
return { nextEdgeId: await executeCode(block, context) }
case LogicBlockType.TYPEBOT_LINK:
return await executeTypebotLink(block, context)
return executeTypebotLink(block, context)
}
}

0 comments on commit 2bd7cee

Please sign in to comment.