2
0
Files
bot/apps/builder/components/account/PersonalInfoForm.tsx

134 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-12-27 15:59:32 +01:00
import {
Stack,
Heading,
HStack,
Avatar,
Button,
FormControl,
FormLabel,
Input,
Tooltip,
Flex,
Text,
2022-02-21 06:57:44 +01:00
InputRightElement,
InputGroup,
2021-12-27 15:59:32 +01:00
} from '@chakra-ui/react'
import { UploadIcon } from 'assets/icons'
import { UploadButton } from 'components/shared/buttons/UploadButton'
import { useUser } from 'contexts/UserContext'
import React, { ChangeEvent, useState } from 'react'
2022-01-28 17:57:14 +01:00
import { isDefined } from 'utils'
2021-12-27 15:59:32 +01:00
export const PersonalInfoForm = () => {
const {
user,
updateUser,
saveUser,
hasUnsavedChanges,
isSaving,
isOAuthProvider,
} = useUser()
const [reloadParam, setReloadParam] = useState('')
2022-02-21 06:57:44 +01:00
const [isApiTokenVisible, setIsApiTokenVisible] = useState(false)
2021-12-27 15:59:32 +01:00
2022-01-20 16:14:47 +01:00
const handleFileUploaded = async (url: string) => {
2021-12-27 15:59:32 +01:00
setReloadParam(Date.now().toString())
updateUser({ image: url })
}
const handleNameChange = (e: ChangeEvent<HTMLInputElement>) => {
updateUser({ name: e.target.value })
}
const handleEmailChange = (e: ChangeEvent<HTMLInputElement>) => {
updateUser({ email: e.target.value })
}
2022-02-21 06:57:44 +01:00
const toggleTokenVisibility = () => setIsApiTokenVisible(!isApiTokenVisible)
2021-12-27 15:59:32 +01:00
return (
<Stack direction="row" spacing="10" justifyContent={'space-between'}>
<Heading as="h2" fontSize="xl">
Personal info
</Heading>
<Stack spacing="6" w="400px">
<HStack spacing={6}>
<Avatar
size="lg"
src={user?.image ? `${user.image}?${reloadParam}` : undefined}
name={user?.name ?? undefined}
/>
<Stack>
<UploadButton
size="sm"
2022-01-20 16:14:47 +01:00
filePath={`users/${user?.id}/avatar`}
2021-12-27 15:59:32 +01:00
leftIcon={<UploadIcon />}
2022-01-20 16:14:47 +01:00
onFileUploaded={handleFileUploaded}
2021-12-27 15:59:32 +01:00
>
Change photo
</UploadButton>
<Text color="gray.500" fontSize="sm">
.jpg or.png, max 1MB
</Text>
</Stack>
</HStack>
<FormControl>
<FormLabel htmlFor="name">Name</FormLabel>
<Input
id="name"
value={user?.name ?? ''}
onChange={handleNameChange}
/>
</FormControl>
2022-01-28 17:57:14 +01:00
{isDefined(user?.email) && (
<Tooltip
label="Updating email is not available."
placement="left"
hasArrow
>
<FormControl>
<FormLabel
htmlFor="email"
color={isOAuthProvider ? 'gray.500' : 'current'}
>
Email address
</FormLabel>
<Input
id="email"
type="email"
isDisabled
value={user?.email ?? ''}
onChange={handleEmailChange}
/>
</FormControl>
</Tooltip>
)}
2022-02-21 06:57:44 +01:00
<FormControl>
<FormLabel htmlFor="name">API token</FormLabel>
<InputGroup>
<Input
id="token"
value={user?.apiToken ?? ''}
type={isApiTokenVisible ? 'text' : 'password'}
/>
<InputRightElement mr="3">
<Button size="xs" onClick={toggleTokenVisibility}>
{isApiTokenVisible ? 'Hide' : 'Show'}
</Button>
</InputRightElement>
</InputGroup>
</FormControl>
2021-12-27 15:59:32 +01:00
{hasUnsavedChanges && (
<Flex justifyContent="flex-end">
<Button colorScheme="blue" onClick={saveUser} isLoading={isSaving}>
Save
</Button>
</Flex>
)}
</Stack>
</Stack>
)
}