LoginForm: migrate to functional component.

master
Tom Hacohen 4 years ago
parent 8bcf436b99
commit 99016871de

@ -20,51 +20,21 @@ interface FormErrors {
errorServer?: string; errorServer?: string;
} }
class LoginForm extends React.PureComponent { interface PropsType {
public state: {
showAdvanced: boolean;
errors: FormErrors;
server: string;
username: string;
password: string;
};
public props: {
onSubmit: (username: string, password: string, serviceApiUrl?: string) => void; onSubmit: (username: string, password: string, serviceApiUrl?: string) => void;
loading?: boolean; loading?: boolean;
error?: Error; 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);
} }
public handleInputChange(event: React.ChangeEvent<any>) { export default function LoginForm(props: PropsType) {
const name = event.target.name; const [username, setUsername] = React.useState("");
const value = event.target.value; const [password, setPassword] = React.useState("");
this.setState({ const [server, setServer] = React.useState("");
[name]: value, const [showAdvanced, setShowAdvanced] = React.useState(false);
}); const [errors, setErrors] = React.useState<FormErrors>({});
}
public generateEncryption(e: any) { function generateEncryption(e: React.FormEvent<any>) {
e.preventDefault(); 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 errors: FormErrors = {};
const fieldRequired = "This field is required!"; const fieldRequired = "This field is required!";
if (!username) { if (!username) {
@ -75,26 +45,21 @@ class LoginForm extends React.PureComponent {
} }
if (process.env.NODE_ENV !== "development") { 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://"; errors.errorServer = "Server URI must start with https://";
} }
} }
if (Object.keys(errors).length) { if (Object.keys(errors).length) {
this.setState({ errors }); setErrors(errors);
return; return;
} else { } 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 });
}
public render() {
const styles = { const styles = {
form: { form: {
}, },
@ -110,26 +75,31 @@ class LoginForm extends React.PureComponent {
}, },
}; };
function handleInputChange(func: (value: string) => void) {
return (event: React.ChangeEvent<any>) => {
func(event.target.value);
};
}
let advancedSettings = null; let advancedSettings = null;
if (this.state.showAdvanced) { if (showAdvanced) {
advancedSettings = ( advancedSettings = (
<React.Fragment> <React.Fragment>
<TextField <TextField
type="url" type="url"
style={styles.textField} style={styles.textField}
error={!!this.state.errors.errorServer} error={!!errors.errorServer}
helperText={this.state.errors.errorServer} helperText={errors.errorServer}
label="Server" label="Server"
name="server" value={server}
value={this.state.server} onChange={handleInputChange(setServer)}
onChange={this.handleInputChange}
/> />
<br /> <br />
</React.Fragment> </React.Fragment>
); );
} }
if (this.props.loading) { if (props.loading) {
return ( return (
<div style={{ textAlign: "center" }}> <div style={{ textAlign: "center" }}>
<LoadingIndicator /> <LoadingIndicator />
@ -140,28 +110,27 @@ class LoginForm extends React.PureComponent {
return ( return (
<React.Fragment> <React.Fragment>
{(this.props.error) && (<div>Error! {this.props.error.message}</div>)} {(props.error) && (<div>Error! {props.error.message}</div>)}
<form style={styles.form} onSubmit={this.generateEncryption}> <form style={styles.form} onSubmit={generateEncryption}>
<TextField <TextField
type="text" type="text"
style={styles.textField} style={styles.textField}
error={!!this.state.errors.errorEmail} error={!!errors.errorEmail}
helperText={this.state.errors.errorEmail} helperText={errors.errorEmail}
label="Username" label="Username"
name="username" value={username}
value={this.state.username} onChange={handleInputChange(setUsername)}
onChange={this.handleInputChange}
/> />
<br /> <br />
<TextField <TextField
type="password" type="password"
style={styles.textField} style={styles.textField}
error={!!this.state.errors.errorPassword} error={!!errors.errorPassword}
helperText={this.state.errors.errorPassword} helperText={errors.errorPassword}
label="Password" label="Password"
name="password" name="password"
value={this.state.password} value={password}
onChange={this.handleInputChange} onChange={handleInputChange(setPassword)}
/> />
<div style={styles.forgotPassword}> <div style={styles.forgotPassword}>
<ExternalLink href={C.forgotPassword}>Forgot password?</ExternalLink> <ExternalLink href={C.forgotPassword}>Forgot password?</ExternalLink>
@ -171,8 +140,8 @@ class LoginForm extends React.PureComponent {
control={ control={
<Switch <Switch
color="primary" color="primary"
checked={this.state.showAdvanced} checked={showAdvanced}
onChange={this.toggleAdvancedSettings} onChange={() => setShowAdvanced(!showAdvanced)}
/> />
} }
label="Advanced settings" label="Advanced settings"
@ -185,15 +154,12 @@ class LoginForm extends React.PureComponent {
variant="contained" variant="contained"
type="submit" type="submit"
color="secondary" color="secondary"
disabled={this.props.loading} disabled={props.loading}
> >
{this.props.loading ? "Loading…" : "Log In"} {props.loading ? "Loading…" : "Log In"}
</Button> </Button>
</div> </div>
</form> </form>
</React.Fragment> </React.Fragment>
); );
} }
}
export default LoginForm;

Loading…
Cancel
Save