2
0
Files
bot/apps/builder/src/features/blocks/inputs/payment/components/PaymentSettings.tsx

207 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-05-24 14:25:15 -07:00
import {
Stack,
useDisclosure,
Text,
Select,
HStack,
Accordion,
AccordionButton,
AccordionIcon,
AccordionItem,
AccordionPanel,
} from '@chakra-ui/react'
import { DropdownList } from '@/components/DropdownList'
import {
PaymentAddress,
PaymentInputOptions,
PaymentProvider,
} from '@typebot.io/schemas'
import React, { ChangeEvent } from 'react'
import { currencies } from '../currencies'
2022-05-24 14:25:15 -07:00
import { StripeConfigModal } from './StripeConfigModal'
import { CredentialsDropdown } from '@/features/credentials/components/CredentialsDropdown'
import { TextInput } from '@/components/inputs'
import { useWorkspace } from '@/features/workspace/WorkspaceProvider'
import { PaymentAddressSettings } from './PaymentAddressSettings'
2022-05-24 14:25:15 -07:00
type Props = {
options: PaymentInputOptions
onOptionsChange: (options: PaymentInputOptions) => void
}
export const PaymentSettings = ({ options, onOptionsChange }: Props) => {
const { workspace } = useWorkspace()
2022-05-24 14:25:15 -07:00
const { isOpen, onOpen, onClose } = useDisclosure()
const updateProvider = (provider: PaymentProvider) => {
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
provider,
})
}
const updateCredentials = (credentialsId?: string) => {
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
credentialsId,
})
}
const updateAmount = (amount?: string) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
amount,
})
const updateCurrency = (e: ChangeEvent<HTMLSelectElement>) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
currency: e.target.value,
})
const updateName = (name: string) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
additionalInformation: { ...options.additionalInformation, name },
})
const updateEmail = (email: string) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
additionalInformation: { ...options.additionalInformation, email },
})
const updatePhoneNumber = (phoneNumber: string) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
additionalInformation: { ...options.additionalInformation, phoneNumber },
})
const updateButtonLabel = (button: string) =>
2022-05-24 14:25:15 -07:00
onOptionsChange({
...options,
labels: { ...options.labels, button },
})
const updateSuccessLabel = (success: string) =>
onOptionsChange({
...options,
labels: { ...options.labels, success },
2022-05-24 14:25:15 -07:00
})
const updateDescription = (description: string) =>
onOptionsChange({
...options,
additionalInformation: { ...options.additionalInformation, description },
})
const updateAddress = (address: PaymentAddress) =>
onOptionsChange({
...options,
additionalInformation: { ...options.additionalInformation, address },
})
2022-05-24 14:25:15 -07:00
return (
<Stack spacing={4}>
<Stack>
<Text>Provider:</Text>
<DropdownList
onItemSelect={updateProvider}
2022-05-24 14:25:15 -07:00
items={Object.values(PaymentProvider)}
currentItem={options.provider}
/>
</Stack>
<Stack>
<Text>Account:</Text>
{workspace && (
<CredentialsDropdown
type="stripe"
workspaceId={workspace.id}
currentCredentialsId={options.credentialsId}
onCredentialsSelect={updateCredentials}
onCreateNewClick={onOpen}
/>
)}
2022-05-24 14:25:15 -07:00
</Stack>
<HStack>
<TextInput
label="Price amount:"
onChange={updateAmount}
defaultValue={options.amount ?? ''}
placeholder="30.00"
/>
2022-05-24 14:25:15 -07:00
<Stack>
<Text>Currency:</Text>
<Select
placeholder="Select option"
value={options.currency}
onChange={updateCurrency}
2022-05-24 14:25:15 -07:00
>
{currencies.map((currency) => (
<option value={currency.code} key={currency.code}>
2022-05-24 14:25:15 -07:00
{currency.code}
</option>
))}
</Select>
</Stack>
</HStack>
<TextInput
label="Button label:"
onChange={updateButtonLabel}
defaultValue={options.labels.button}
placeholder="Pay"
/>
<TextInput
label="Success message:"
onChange={updateSuccessLabel}
defaultValue={options.labels.success ?? 'Success'}
placeholder="Success"
/>
2022-05-24 14:25:15 -07:00
<Accordion allowToggle>
<AccordionItem>
<AccordionButton justifyContent="space-between">
Additional information
<AccordionIcon />
</AccordionButton>
<AccordionPanel py={4} as={Stack} spacing="6">
<TextInput
label="Description:"
defaultValue={options.additionalInformation?.description ?? ''}
onChange={updateDescription}
placeholder="A digital product"
/>
<TextInput
label="Name:"
defaultValue={options.additionalInformation?.name ?? ''}
onChange={updateName}
placeholder="John Smith"
/>
<TextInput
label="Email:"
defaultValue={options.additionalInformation?.email ?? ''}
onChange={updateEmail}
placeholder="john@gmail.com"
/>
<TextInput
label="Phone number:"
defaultValue={options.additionalInformation?.phoneNumber ?? ''}
onChange={updatePhoneNumber}
placeholder="+33XXXXXXXXX"
/>
<PaymentAddressSettings
address={options.additionalInformation?.address}
onAddressChange={updateAddress}
/>
2022-05-24 14:25:15 -07:00
</AccordionPanel>
</AccordionItem>
</Accordion>
<StripeConfigModal
isOpen={isOpen}
onClose={onClose}
onNewCredentials={updateCredentials}
2022-05-24 14:25:15 -07:00
/>
</Stack>
)
}