diff --git a/src/components/LoginForm.tsx b/src/components/LoginForm.tsx index d240c72..1c05f6c 100644 --- a/src/components/LoginForm.tsx +++ b/src/components/LoginForm.tsx @@ -20,51 +20,21 @@ interface FormErrors { errorServer?: string; } -class LoginForm extends React.PureComponent { - public state: { - showAdvanced: boolean; - errors: FormErrors; - - server: string; - username: string; - password: string; - }; - - public props: { - onSubmit: (username: string, password: string, serviceApiUrl?: string) => void; - loading?: boolean; - error?: Error; - }; - - constructor(props: any) { - super(props); - this.state = { - showAdvanced: false, - errors: {}, - server: "", - username: "", - password: "", - }; - this.generateEncryption = this.generateEncryption.bind(this); - this.toggleAdvancedSettings = this.toggleAdvancedSettings.bind(this); - this.handleInputChange = this.handleInputChange.bind(this); - } +interface PropsType { + onSubmit: (username: string, password: string, serviceApiUrl?: string) => void; + loading?: boolean; + error?: Error; +} - public handleInputChange(event: React.ChangeEvent) { - const name = event.target.name; - const value = event.target.value; - this.setState({ - [name]: value, - }); - } +export default function LoginForm(props: PropsType) { + const [username, setUsername] = React.useState(""); + const [password, setPassword] = React.useState(""); + const [server, setServer] = React.useState(""); + const [showAdvanced, setShowAdvanced] = React.useState(false); + const [errors, setErrors] = React.useState({}); - public generateEncryption(e: any) { + function generateEncryption(e: React.FormEvent) { e.preventDefault(); - const server = this.state.showAdvanced ? this.state.server : undefined; - - const username = this.state.username; - const password = this.state.password; - const errors: FormErrors = {}; const fieldRequired = "This field is required!"; if (!username) { @@ -75,125 +45,121 @@ class LoginForm extends React.PureComponent { } if (process.env.NODE_ENV !== "development") { - if (this.state.showAdvanced && !this.state.server.startsWith("https://")) { + if (showAdvanced && !server.startsWith("https://")) { errors.errorServer = "Server URI must start with https://"; } } if (Object.keys(errors).length) { - this.setState({ errors }); + setErrors(errors); return; } else { - this.setState({ errors: {} }); + setErrors({}); } - this.props.onSubmit(username, password, server); + props.onSubmit(username, password, (showAdvanced) ? server : undefined); } - public toggleAdvancedSettings() { - this.setState({ showAdvanced: !this.state.showAdvanced }); - } + const styles = { + form: { + }, + forgotPassword: { + paddingTop: 20, + }, + textField: { + marginTop: 20, + }, + submit: { + marginTop: 40, + textAlign: "right" as any, + }, + }; - public render() { - const styles = { - form: { - }, - forgotPassword: { - paddingTop: 20, - }, - textField: { - marginTop: 20, - }, - submit: { - marginTop: 40, - textAlign: "right" as any, - }, + function handleInputChange(func: (value: string) => void) { + return (event: React.ChangeEvent) => { + func(event.target.value); }; + } - let advancedSettings = null; - if (this.state.showAdvanced) { - advancedSettings = ( - - -
-
- ); - } - - if (this.props.loading) { - return ( -
- -

Deriving encryption data...

-
- ); - } - - return ( + let advancedSettings = null; + if (showAdvanced) { + advancedSettings = ( - {(this.props.error) && (
Error! {this.props.error.message}
)} -
- -
- -
- Forgot password? -
- - - } - label="Advanced settings" - /> - - {advancedSettings} - -
- -
- + +
); } -} -export default LoginForm; + if (props.loading) { + return ( +
+ +

Deriving encryption data...

+
+ ); + } + + return ( + + {(props.error) && (
Error! {props.error.message}
)} +
+ +
+ +
+ Forgot password? +
+ + setShowAdvanced(!showAdvanced)} + /> + } + label="Advanced settings" + /> + + {advancedSettings} + +
+ +
+ +
+ ); +}