2021-05-24 12:13:24 +00:00
|
|
|
import React, { useCallback, useState } from "react"
|
2021-05-11 21:27:42 +00:00
|
|
|
import FormLabelled from "../base/FormLabelled"
|
|
|
|
import FormLabel from "../base/formparts/FormLabel"
|
|
|
|
import InputWithIcon from "../base/InputWithIcon"
|
|
|
|
import { faEnvelope, faKey, faPlus, faUser } from "@fortawesome/free-solid-svg-icons"
|
|
|
|
import FormButton from "../base/formparts/FormButton"
|
|
|
|
import BoxFull from "../base/BoxFull"
|
|
|
|
import FormAlert from "../base/formparts/FormAlert"
|
2021-05-24 12:13:24 +00:00
|
|
|
import useStrings from "../../hooks/useStrings"
|
2021-05-11 21:27:42 +00:00
|
|
|
|
|
|
|
|
2021-05-23 03:03:41 +00:00
|
|
|
/**
|
|
|
|
* A {@link BoxFull} allowing an administrator user to create a new user.
|
|
|
|
*
|
|
|
|
* @param createUser - Async function to call to create an user.
|
|
|
|
* @param running - Whether another request is currently running.
|
|
|
|
* @param props - Additional props to pass to the box.
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
* @constructor
|
|
|
|
*/
|
2021-05-12 02:10:36 +00:00
|
|
|
export default function BoxUserCreate({ createUser, running, ...props }) {
|
2021-05-11 21:27:42 +00:00
|
|
|
const [username, setUsername] = useState("")
|
|
|
|
const [email, setEmail] = useState("")
|
|
|
|
const [password, setPassword] = useState("")
|
2021-05-12 02:10:36 +00:00
|
|
|
const [error, setError] = useState(undefined)
|
2021-05-24 12:13:24 +00:00
|
|
|
const strings = useStrings()
|
2021-05-11 21:27:42 +00:00
|
|
|
|
2021-05-12 02:10:36 +00:00
|
|
|
const onButtonClick = useCallback(
|
|
|
|
async () => {
|
|
|
|
const result = await createUser({
|
2021-05-11 21:27:42 +00:00
|
|
|
"email": email,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2021-05-12 02:10:36 +00:00
|
|
|
})
|
|
|
|
setError(result.error)
|
2021-05-11 21:27:42 +00:00
|
|
|
},
|
2021-05-12 02:10:36 +00:00
|
|
|
[createUser, email, username, password],
|
2021-05-11 21:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2021-05-18 00:04:06 +00:00
|
|
|
<BoxFull header={strings.userCreate} {...props}>
|
2021-05-11 21:27:42 +00:00
|
|
|
<FormLabelled>
|
2021-05-18 00:04:06 +00:00
|
|
|
<FormLabel text={strings.userName}>
|
2021-05-11 21:27:42 +00:00
|
|
|
<InputWithIcon
|
|
|
|
icon={faUser}
|
|
|
|
type={"text"}
|
|
|
|
value={username}
|
|
|
|
onChange={event => setUsername(event.target.value)}
|
|
|
|
/>
|
|
|
|
</FormLabel>
|
2021-05-18 00:04:06 +00:00
|
|
|
<FormLabel text={strings.email}>
|
2021-05-11 21:27:42 +00:00
|
|
|
<InputWithIcon
|
|
|
|
icon={faEnvelope}
|
|
|
|
type={"text"}
|
|
|
|
value={email}
|
|
|
|
onChange={event => setEmail(event.target.value)}
|
|
|
|
/>
|
|
|
|
</FormLabel>
|
2021-05-18 00:04:06 +00:00
|
|
|
<FormLabel text={strings.passwd}>
|
2021-05-11 21:27:42 +00:00
|
|
|
<InputWithIcon
|
|
|
|
icon={faKey}
|
|
|
|
type={"password"}
|
|
|
|
value={password}
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
|
|
|
/>
|
|
|
|
</FormLabel>
|
|
|
|
{error ?
|
|
|
|
<FormAlert color={"Red"}>
|
|
|
|
{error.toString()}
|
|
|
|
</FormAlert>
|
|
|
|
: null}
|
|
|
|
<FormButton
|
|
|
|
color={"Green"}
|
|
|
|
icon={faPlus}
|
2021-05-12 02:10:36 +00:00
|
|
|
onClick={onButtonClick}
|
|
|
|
disabled={running}
|
2021-05-11 21:27:42 +00:00
|
|
|
>
|
2021-05-18 00:04:06 +00:00
|
|
|
{strings.create}
|
2021-05-11 21:27:42 +00:00
|
|
|
</FormButton>
|
|
|
|
</FormLabelled>
|
|
|
|
</BoxFull>
|
|
|
|
)
|
|
|
|
}
|