Skip to content

Commit

Permalink
🚸 (buttons) Show collected variable in buttons block preview
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 23, 2022
1 parent 4109e63 commit f224ab9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ test.describe.parallel('Buttons input block', () => {
await page.click('[data-testid="block2-icon"]')
await page.click('text=Multiple choice?')
await page.fill('#button', 'Go')
await page.getByPlaceholder('Select a variable').click()
await page.getByText('var1').click()
await expect(page.getByText('Collectsvar1')).toBeVisible()
await page.click('[data-testid="block2-icon"]')

await page.locator('text=Item 1').hover()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,72 @@ import {
BlockWithItems,
defaultConditionContent,
ItemType,
Block,
LogicBlockType,
InputBlockType,
} from 'models'
import { SetTypebot } from '../TypebotProvider'
import produce from 'immer'
import { cleanUpEdgeDraft } from './edges'
import { byId, blockHasItems } from 'utils'
import cuid from 'cuid'
import { WritableDraft } from 'immer/dist/types/types-external'

type NewItem = Pick<Item, 'blockId' | 'outgoingEdgeId' | 'type'> & Partial<Item>

export type ItemsActions = {
createItem: (item: Item | Omit<Item, 'id'>, indices: ItemIndices) => void
createItem: (item: NewItem, indices: ItemIndices) => void
updateItem: (indices: ItemIndices, updates: Partial<Omit<Item, 'id'>>) => void
detachItemFromBlock: (indices: ItemIndices) => void
deleteItem: (indices: ItemIndices) => void
}

const createItem = (
block: WritableDraft<Block>,
item: NewItem,
itemIndex: number
) => {
switch (block.type) {
case LogicBlockType.CONDITION: {
if (item.type === ItemType.CONDITION) {
const newItem = {
...item,
id: 'id' in item && item.id ? item.id : cuid(),
content: item.content ?? defaultConditionContent,
}
block.items.splice(itemIndex, 0, newItem)
return newItem
}
break
}
case InputBlockType.CHOICE: {
if (item.type === ItemType.BUTTON) {
const newItem = {
...item,
id: 'id' in item && item.id ? item.id : cuid(),
content: item.content,
}
block.items.splice(itemIndex, 0, newItem)
return newItem
}
break
}
}
}

const itemsAction = (setTypebot: SetTypebot): ItemsActions => ({
createItem: (
item: Item | Omit<Item, 'id'>,
item: NewItem,
{ groupIndex, blockIndex, itemIndex }: ItemIndices
) =>
setTypebot((typebot) =>
produce(typebot, (typebot) => {
const block = typebot.groups[groupIndex].blocks[
blockIndex
] as BlockWithItems
const block = typebot.groups[groupIndex].blocks[blockIndex]

const newItem = createItem(block, item, itemIndex)

if (!newItem) return

const newItem = {
id: 'id' in item ? item.id : cuid(),
content:
item.type === ItemType.CONDITION
? defaultConditionContent
: undefined,
...item,
} as Item
if (item.outgoingEdgeId) {
const edgeIndex = typebot.edges.findIndex(byId(item.outgoingEdgeId))
edgeIndex !== -1
Expand All @@ -47,7 +80,6 @@ const itemsAction = (setTypebot: SetTypebot): ItemsActions => ({
})
: (newItem.outgoingEdgeId = undefined)
}
block.items.splice(itemIndex, 0, newItem)
})
),
updateItem: (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
Flex,
HStack,
Portal,
Stack,
Tag,
Text,
useColorModeValue,
useEventListener,
Expand All @@ -13,7 +15,13 @@ import {
useGraph,
} from '../../../providers'
import { useTypebot } from '@/features/editor'
import { BlockIndices, BlockWithItems, LogicBlockType, Item } from 'models'
import {
BlockIndices,
BlockWithItems,
LogicBlockType,
Item,
Variable,
} from 'models'
import React, { useEffect, useRef, useState } from 'react'
import { ItemNode } from './ItemNode'
import { SourceEndpoint } from '../../Endpoints'
Expand Down Expand Up @@ -127,8 +135,16 @@ export const ItemNodesList = ({
elem && (placeholderRefs.current[idx] = elem)
}

const collectedVariableId = 'options' in block && block.options.variableId

return (
<Stack flex={1} spacing={1} maxW="full" onClick={stopPropagating}>
{collectedVariableId && (
<CollectVariableLabel
variableId={collectedVariableId}
variables={typebot?.variables ?? []}
/>
)}
<PlaceholderNode
isVisible={showPlaceholders}
isExpanded={expandedPlaceholderIndex === 0}
Expand Down Expand Up @@ -202,3 +218,20 @@ const DefaultItemNode = ({ block }: { block: BlockWithItems }) => {
</Flex>
)
}

const CollectVariableLabel = ({
variableId,
variables,
}: {
variableId: string
variables: Variable[]
}) => (
<HStack fontStyle="italic" spacing={1}>
<Text fontSize="sm" color={useColorModeValue('gray.600', 'gray.400')}>
Collects
</Text>
<Tag bg="orange.400" color="white" size="sm">
{variables.find((variable) => variable.id === variableId)?.name}
</Tag>
</HStack>
)
4 changes: 2 additions & 2 deletions packages/models/src/features/blocks/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
LogicBlockOptions,
} from '.'
import { BubbleBlock, bubbleBlockSchema } from './bubbles'
import { InputBlock, inputBlockSchema } from './inputs'
import { ChoiceInputBlock, InputBlock, inputBlockSchema } from './inputs'
import { IntegrationBlock, integrationBlockSchema } from './integrations'
import { ConditionBlock, LogicBlock, logicBlockSchema } from './logic'
import { z } from 'zod'
Expand Down Expand Up @@ -51,7 +51,7 @@ export type BlockOptions =
| LogicBlockOptions
| IntegrationBlockOptions

export type BlockWithItems = Omit<Block, 'items'> & { items: Item[] }
export type BlockWithItems = ConditionBlock | ChoiceInputBlock

export type BlockBase = z.infer<typeof blockBaseSchema>

Expand Down
1 change: 0 additions & 1 deletion packages/models/src/features/blocks/logic/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export const conditionBlockSchema = blockBaseSchema.and(
z.object({
type: z.enum([LogicBlockType.CONDITION]),
items: z.array(conditionItemSchema),
options: z.object({}),
})
)

Expand Down

5 comments on commit f224ab9

@vercel
Copy link

@vercel vercel bot commented on f224ab9 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs.typebot.io
docs-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f224ab9 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on f224ab9 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2-alpha – ./apps/viewer

bot.artiweb.app
bot.devitus.com
bot.tc-mail.com
chat.sureb4.com
eventhub.com.au
games.klujo.com
sakuranembro.it
typebot.aloe.do
bot.piccinato.co
bot.sv-energy.it
botc.ceox.com.br
clo.closeer.work
cockroach.cr8.ai
faqs.nigerias.io
feedback.ofx.one
kw.wpwakanda.com
pant.maxbot.com.br
positivobra.com.br
survey.digienge.io
this-is-a-test.com
zap.techadviser.in
bot.digitalbled.com
bot.eventhub.com.au
bot.jepierre.com.br
bot.winglabs.com.br
carsalesenquiry.com
chatbot.repplai.com
demo.botscientis.us
forms.webisharp.com
kbsub.wpwakanda.com
live.botscientis.us
mentoria.omelhor.vc
nutrisamirbayde.com
order.maitempah.com
quest.wpwakanda.com
survey1.digienge.io
test.botscientis.us
typebot.stillio.com
wordsandimagery.com
bium.gratirabbit.com
bot.ansuraniphone.my
bot.cotemeuplano.com
bot.leadbooster.help
chat.hayurihijab.com
chatbee.agfunnel.com
click.sevenoways.com
connect.growthguy.in
kuiz.sistemniaga.com
offer.botscientis.us
sellmycarglasgow.com
talkbot.agfunnel.com
tenorioadvogados.com
uppity.wpwakanda.com
abutton.wpwakanda.com
acelera.maxbot.com.br
aidigitalmarketing.kr
bbutton.wpwakanda.com
bot.incusservices.com
bot.meuesocial.com.br
bot.ramonmatos.com.br
bot.cabinrentalagency.com
bot.fusionstarreviews.com
boyfriend-breakup.riku.ai
brigadeirosemdrama.com.br
chat.ertcrebateportal.com
chat.thisiscrushhouse.com
sellmyharleylouisiana.com
verfica.botmachine.com.br
configurator.bouclidom.com
help.atlasoutfittersk9.com
ted.meujalecobrasil.com.br
type.dericsoncalari.com.br
chatbot.berbelanjabiz.trade
designguide.techyscouts.com
presente.empresarias.com.mx
sell.sellthemotorhome.co.uk
anamnese.odontopavani.com.br
austin.channelautomation.com
bot.marketingplusmindset.com
piazzatorre.barrettamario.it
requests.swamprecordsgnv.com
type.cookieacademyonline.com
bot.brigadeirosemdrama.com.br
onboarding.libertydreamcare.ie
type.talitasouzamarques.com.br
agendamento.sergiolimajr.com.br
anamnese.clinicamegasjdr.com.br
bookings.littlepartymonkeys.com
bot.comercializadoraomicron.com
elevateyourmind.groovepages.com
yourfeedback.comebackreward.com
personal-trainer.barrettamario.it
preagendamento.sergiolimajr.com.br
studiotecnicoimmobiliaremerelli.it
download.thailandmicespecialist.com
register.thailandmicespecialist.com
viewer-v2-alpha-typebot-io.vercel.app
pesquisa.escolamodacomproposito.com.br
anamnese.clinicaramosodontologia.com.br
viewer-v2-alpha-git-main-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f224ab9 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on f224ab9 Dec 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-typebot-io.vercel.app
app.typebot.io
builder-v2-git-main-typebot-io.vercel.app

Please sign in to comment.