Skip to content

Commit

Permalink
feat(inputs): ✨ Add Set variable step
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Jan 14, 2022
1 parent 13f72f5 commit 4ccb7bc
Show file tree
Hide file tree
Showing 55 changed files with 1,024 additions and 223 deletions.
6 changes: 3 additions & 3 deletions apps/builder/components/board/StepTypesList/StepCard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, ButtonProps, Flex, HStack } from '@chakra-ui/react'
import { BubbleStepType, InputStepType, StepType } from 'models'
import { BubbleStepType, InputStepType, StepType, LogicStepType } from 'models'
import { useDnd } from 'contexts/DndContext'
import React, { useEffect, useState } from 'react'
import { StepIcon } from './StepIcon'
Expand All @@ -9,10 +9,10 @@ export const StepCard = ({
type,
onMouseDown,
}: {
type: BubbleStepType | InputStepType
type: BubbleStepType | InputStepType | LogicStepType
onMouseDown: (
e: React.MouseEvent,
type: BubbleStepType | InputStepType
type: BubbleStepType | InputStepType | LogicStepType
) => void
}) => {
const { draggedStepType } = useDnd()
Expand Down
6 changes: 5 additions & 1 deletion apps/builder/components/board/StepTypesList/StepIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import {
CalendarIcon,
ChatIcon,
CheckSquareIcon,
EditIcon,
EmailIcon,
FlagIcon,
GlobeIcon,
NumberIcon,
PhoneIcon,
TextIcon,
} from 'assets/icons'
import { BubbleStepType, InputStepType, StepType } from 'models'
import { BubbleStepType, InputStepType, LogicStepType, StepType } from 'models'
import React from 'react'

type StepIconProps = { type: StepType } & IconProps
Expand Down Expand Up @@ -41,6 +42,9 @@ export const StepIcon = ({ type, ...props }: StepIconProps) => {
case InputStepType.CHOICE: {
return <CheckSquareIcon {...props} />
}
case LogicStepType.SET_VARIABLE: {
return <EditIcon {...props} />
}
case 'start': {
return <FlagIcon {...props} />
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Text } from '@chakra-ui/react'
import { BubbleStepType, InputStepType, StepType } from 'models'
import { BubbleStepType, InputStepType, LogicStepType, StepType } from 'models'
import React from 'react'

type Props = { type: StepType }
Expand Down Expand Up @@ -28,6 +28,9 @@ export const StepTypeLabel = ({ type }: Props) => {
case InputStepType.CHOICE: {
return <Text>Button</Text>
}
case LogicStepType.SET_VARIABLE: {
return <Text>Set variable</Text>
}
default: {
return <></>
}
Expand Down
13 changes: 11 additions & 2 deletions apps/builder/components/board/StepTypesList/StepTypesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SimpleGrid,
useEventListener,
} from '@chakra-ui/react'
import { BubbleStepType, InputStepType } from 'models'
import { BubbleStepType, InputStepType, LogicStepType } from 'models'
import { useDnd } from 'contexts/DndContext'
import React, { useState } from 'react'
import { StepCard, StepCardOverlay } from './StepCard'
Expand All @@ -31,7 +31,7 @@ export const StepTypesList = () => {

const handleMouseDown = (
e: React.MouseEvent,
type: BubbleStepType | InputStepType
type: BubbleStepType | InputStepType | LogicStepType
) => {
const element = e.currentTarget as HTMLDivElement
const rect = element.getBoundingClientRect()
Expand Down Expand Up @@ -85,6 +85,15 @@ export const StepTypesList = () => {
<StepCard key={type} type={type} onMouseDown={handleMouseDown} />
))}
</SimpleGrid>

<Text fontSize="sm" fontWeight="semibold" color="gray.600">
Logic
</Text>
<SimpleGrid columns={2} spacing="2">
{Object.values(LogicStepType).map((type) => (
<StepCard key={type} type={type} onMouseDown={handleMouseDown} />
))}
</SimpleGrid>
{draggedStepType && (
<StepCardOverlay
type={draggedStepType}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
ChoiceInputOptions,
InputStep,
InputStepType,
LogicStepType,
SetVariableOptions,
Step,
TextInputOptions,
} from 'models'
import {
Expand All @@ -15,10 +18,12 @@ import {
} from './bodies'
import { ChoiceInputSettingsBody } from './bodies/ChoiceInputSettingsBody'
import { PhoneNumberSettingsBody } from './bodies/PhoneNumberSettingsBody'
import { SetVariableSettingsBody } from './bodies/SetVariableSettingsBody'

type Props = {
step: InputStep
step: Step
}

export const SettingsPopoverContent = ({ step }: Props) => {
const handleMouseDown = (e: React.MouseEvent) => e.stopPropagation()

Expand All @@ -35,7 +40,7 @@ export const SettingsPopoverContent = ({ step }: Props) => {
const SettingsPopoverBodyContent = ({ step }: Props) => {
const { updateStep } = useTypebot()
const handleOptionsChange = (
options: TextInputOptions | ChoiceInputOptions
options: TextInputOptions | ChoiceInputOptions | SetVariableOptions
) => updateStep(step.id, { options } as Partial<InputStep>)

switch (step.type) {
Expand Down Expand Up @@ -95,6 +100,14 @@ const SettingsPopoverBodyContent = ({ step }: Props) => {
/>
)
}
case LogicStepType.SET_VARIABLE: {
return (
<SetVariableSettingsBody
options={step.options}
onOptionsChange={handleOptionsChange}
/>
)
}
default: {
return <></>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { SwitchWithLabel } from 'components/shared/SwitchWithLabel'
import { ChoiceInputOptions } from 'models'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { ChoiceInputOptions, Variable } from 'models'
import React from 'react'

type ChoiceInputSettingsBodyProps = {
Expand All @@ -17,6 +18,8 @@ export const ChoiceInputSettingsBody = ({
options && onOptionsChange({ ...options, isMultipleChoice })
const handleButtonLabelChange = (buttonLabel: string) =>
options && onOptionsChange({ ...options, buttonLabel })
const handleVariableChange = (variable: Variable) =>
options && onOptionsChange({ ...options, variableId: variable.id })

return (
<Stack spacing={4}>
Expand All @@ -39,6 +42,15 @@ export const ChoiceInputSettingsBody = ({
/>
</Stack>
)}
<Stack>
<FormLabel mb="0" htmlFor="variable">
Save answer in a variable:
</FormLabel>
<VariableSearchInput
initialVariableId={options?.variableId}
onSelectVariable={handleVariableChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { SwitchWithLabel } from 'components/shared/SwitchWithLabel'
import { DateInputOptions } from 'models'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { DateInputOptions, Variable } from 'models'
import React from 'react'

type DateInputSettingsBodyProps = {
Expand All @@ -23,6 +24,8 @@ export const DateInputSettingsBody = ({
onOptionsChange({ ...options, isRange })
const handleHasTimeChange = (hasTime: boolean) =>
onOptionsChange({ ...options, hasTime })
const handleVariableChange = (variable: Variable) =>
onOptionsChange({ ...options, variableId: variable.id })

return (
<Stack spacing={4}>
Expand Down Expand Up @@ -75,6 +78,15 @@ export const DateInputSettingsBody = ({
onChange={handleButtonLabelChange}
/>
</Stack>
<Stack>
<FormLabel mb="0" htmlFor="variable">
Save answer in a variable:
</FormLabel>
<VariableSearchInput
initialVariableId={options?.variableId}
onSelectVariable={handleVariableChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { EmailInputOptions } from 'models'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { EmailInputOptions, Variable } from 'models'
import React from 'react'

type EmailInputSettingsBodyProps = {
Expand All @@ -16,6 +17,8 @@ export const EmailInputSettingsBody = ({
onOptionsChange({ ...options, labels: { ...options?.labels, placeholder } })
const handleButtonLabelChange = (button: string) =>
onOptionsChange({ ...options, labels: { ...options?.labels, button } })
const handleVariableChange = (variable: Variable) =>
onOptionsChange({ ...options, variableId: variable.id })

return (
<Stack spacing={4}>
Expand All @@ -41,6 +44,15 @@ export const EmailInputSettingsBody = ({
onChange={handleButtonLabelChange}
/>
</Stack>
<Stack>
<FormLabel mb="0" htmlFor="variable">
Save answer in a variable:
</FormLabel>
<VariableSearchInput
initialVariableId={options?.variableId}
onSelectVariable={handleVariableChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { FormLabel, HStack, Stack } from '@chakra-ui/react'
import { SmartNumberInput } from 'components/settings/SmartNumberInput'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { NumberInputOptions } from 'models'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { NumberInputOptions, Variable } from 'models'
import React from 'react'
import { removeUndefinedFields } from 'services/utils'

Expand All @@ -24,6 +25,8 @@ export const NumberInputSettingsBody = ({
onOptionsChange(removeUndefinedFields({ ...options, max }))
const handleStepChange = (step?: number) =>
onOptionsChange(removeUndefinedFields({ ...options, step }))
const handleVariableChange = (variable: Variable) =>
onOptionsChange({ ...options, variableId: variable.id })

return (
<Stack spacing={4}>
Expand Down Expand Up @@ -79,6 +82,15 @@ export const NumberInputSettingsBody = ({
onValueChange={handleStepChange}
/>
</HStack>
<Stack>
<FormLabel mb="0" htmlFor="variable">
Save answer in a variable:
</FormLabel>
<VariableSearchInput
initialVariableId={options?.variableId}
onSelectVariable={handleVariableChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedInput } from 'components/shared/DebouncedInput'
import { EmailInputOptions } from 'models'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { EmailInputOptions, Variable } from 'models'
import React from 'react'

type PhoneNumberSettingsBodyProps = {
Expand All @@ -16,6 +17,8 @@ export const PhoneNumberSettingsBody = ({
onOptionsChange({ ...options, labels: { ...options?.labels, placeholder } })
const handleButtonLabelChange = (button: string) =>
onOptionsChange({ ...options, labels: { ...options?.labels, button } })
const handleVariableChange = (variable: Variable) =>
onOptionsChange({ ...options, variableId: variable.id })

return (
<Stack spacing={4}>
Expand All @@ -41,6 +44,15 @@ export const PhoneNumberSettingsBody = ({
onChange={handleButtonLabelChange}
/>
</Stack>
<Stack>
<FormLabel mb="0" htmlFor="variable">
Save answer in a variable:
</FormLabel>
<VariableSearchInput
initialVariableId={options?.variableId}
onSelectVariable={handleVariableChange}
/>
</Stack>
</Stack>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { FormLabel, Stack } from '@chakra-ui/react'
import { DebouncedTextarea } from 'components/shared/DebouncedTextarea'
import { VariableSearchInput } from 'components/shared/VariableSearchInput'
import { SetVariableOptions, Variable } from 'models'
import React from 'react'

type Props = {
options?: SetVariableOptions
onOptionsChange: (options: SetVariableOptions) => void
}

export const SetVariableSettingsBody = ({
options,
onOptionsChange,
}: Props) => {
const handleVariableChange = (variable: Variable) =>
onOptionsChange({ ...options, variableId: variable.id })
const handleExpressionChange = (expressionToEvaluate: string) =>
onOptionsChange({ ...options, expressionToEvaluate })

return (
<Stack spacing={4}>
<Stack>
<FormLabel mb="0" htmlFor="variable-search">
Search or create variable:
</FormLabel>
<VariableSearchInput
onSelectVariable={handleVariableChange}
initialVariableId={options?.variableId}
id="variable-search"
/>
</Stack>
<Stack>
<FormLabel mb="0" htmlFor="expression">
Value / Expression:
</FormLabel>
<DebouncedTextarea
id="expression"
initialValue={options?.expressionToEvaluate ?? ''}
delay={100}
onChange={handleExpressionChange}
/>
</Stack>
</Stack>
)
}
Loading

2 comments on commit 4ccb7bc

@vercel
Copy link

@vercel vercel bot commented on 4ccb7bc Jan 14, 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

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

@vercel
Copy link

@vercel vercel bot commented on 4ccb7bc Jan 14, 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 – ./apps/viewer

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

Please sign in to comment.