2
0

feat(editor): ️ Rename variable button

This commit is contained in:
Baptiste Arnaud
2022-05-11 06:39:28 -07:00
parent ad3a140982
commit 2b56f83d43
2 changed files with 32 additions and 9 deletions

View File

@ -9,8 +9,9 @@ import {
Button, Button,
InputProps, InputProps,
IconButton, IconButton,
HStack,
} from '@chakra-ui/react' } from '@chakra-ui/react'
import { PlusIcon, TrashIcon } from 'assets/icons' import { EditIcon, PlusIcon, TrashIcon } from 'assets/icons'
import { useTypebot } from 'contexts/TypebotContext' import { useTypebot } from 'contexts/TypebotContext'
import cuid from 'cuid' import cuid from 'cuid'
import { Variable } from 'models' import { Variable } from 'models'
@ -35,7 +36,8 @@ export const VariableSearchInput = ({
...inputProps ...inputProps
}: Props) => { }: Props) => {
const { onOpen, onClose, isOpen } = useDisclosure() const { onOpen, onClose, isOpen } = useDisclosure()
const { typebot, createVariable, deleteVariable } = useTypebot() const { typebot, createVariable, deleteVariable, updateVariable } =
useTypebot()
const variables = typebot?.variables ?? [] const variables = typebot?.variables ?? []
const [inputValue, setInputValue] = useState( const [inputValue, setInputValue] = useState(
variables.find(byId(initialVariableId))?.name ?? '' variables.find(byId(initialVariableId))?.name ?? ''
@ -113,6 +115,19 @@ export const VariableSearchInput = ({
} }
} }
const handleRenameVariableClick =
(variable: Variable) => (e: React.MouseEvent) => {
e.stopPropagation()
const name = prompt('Rename variable', variable.name)
if (!name) return
updateVariable(variable.id, { name })
setFilteredItems(
filteredItems.map((item) =>
item.id === variable.id ? { ...item, name } : item
)
)
}
return ( return (
<Flex ref={dropdownRef} w="full"> <Flex ref={dropdownRef} w="full">
<Popover <Popover
@ -174,12 +189,20 @@ export const VariableSearchInput = ({
justifyContent="space-between" justifyContent="space-between"
> >
{item.name} {item.name}
<HStack>
<IconButton
icon={<EditIcon />}
aria-label="Rename variable"
size="xs"
onClick={handleRenameVariableClick(item)}
/>
<IconButton <IconButton
icon={<TrashIcon />} icon={<TrashIcon />}
aria-label="Remove variable" aria-label="Remove variable"
size="xs" size="xs"
onClick={handleDeleteVariableClick(item)} onClick={handleDeleteVariableClick(item)}
/> />
</HStack>
</Button> </Button>
) )
})} })}

View File

@ -25,7 +25,7 @@ export const variablesAction = (setTypebot: SetTypebot): VariablesActions => ({
) => ) =>
setTypebot((typebot) => setTypebot((typebot) =>
produce(typebot, (typebot) => { produce(typebot, (typebot) => {
typebot.variables.map((v) => typebot.variables = typebot.variables.map((v) =>
v.id === variableId ? { ...v, ...updates } : v v.id === variableId ? { ...v, ...updates } : v
) )
}) })