hedgedoc/src/components/landing/pages/profile/settings/profile-display-name.tsx
Erik Michelson 747d9686fa
User profile page (#102)
* Added mock profile page

* Added translations

* Modularized page, added LoginProvider, started validation

* Re-adding profile route after router refactoring

* Added API calls for password and name change

* Added user deletion modal

* Refactored translations in profile-page to match new locales

* Fixed merge-conflicted locales/de.json

* Removed values for password fields

* Fixed capitalization of LoginProvider

* Fixed codestyle method declarations

* Fix names of methods and started countdown

* Fix countdown

Signed-off-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>

Co-authored-by: Tilman Vatteroth <tilman.vatteroth@tu-dortmund.de>
2020-05-31 21:56:39 +02:00

66 lines
2.2 KiB
TypeScript

import React, { ChangeEvent, FormEvent, useState } from 'react'
import { Button, Card, Form } from 'react-bootstrap'
import { Trans, useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux'
import { updateDisplayName } from '../../../../../api/user'
import { ApplicationState } from '../../../../../redux'
import { getAndSetUser } from '../../../../../utils/apiUtils'
export const ProfileDisplayName: React.FC = () => {
const { t } = useTranslation()
const user = useSelector((state: ApplicationState) => state.user)
const [submittable, setSubmittable] = useState(false)
const [error, setError] = useState(false)
const [displayName, setDisplayName] = useState(user.name)
const regexInvalidDisplayName = /^\s*$/
const changeNameField = (event: ChangeEvent<HTMLInputElement>) => {
setSubmittable(!regexInvalidDisplayName.test(event.target.value))
setDisplayName(event.target.value)
}
const doAsyncChange = async () => {
await updateDisplayName(displayName)
await getAndSetUser()
}
const changeNameSubmit = (event: FormEvent) => {
doAsyncChange().catch(() => setError(true))
event.preventDefault()
}
return (
<Card className="bg-dark mb-4">
<Card.Body>
<Card.Title>
<Trans i18nKey="profile.userProfile"/>
</Card.Title>
<Form onSubmit={changeNameSubmit} className="text-left">
<Form.Group controlId="displayName">
<Form.Label><Trans i18nKey="profile.displayName"/></Form.Label>
<Form.Control
type="text"
size="sm"
placeholder={t('profile.displayName')}
value={displayName}
className="bg-dark text-white"
onChange={changeNameField}
isValid={submittable}
isInvalid={error}
required
/>
<Form.Text><Trans i18nKey="profile.displayNameInfo"/></Form.Text>
</Form.Group>
<Button
type="submit"
variant="primary"
disabled={!submittable}>
<Trans i18nKey="common.save"/>
</Button>
</Form>
</Card.Body>
</Card>
)
}