saleor-dashboard/src/auth/components/LoginPage/LoginPage.tsx

140 lines
4 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
import {
createStyles,
Theme,
withStyles,
WithStyles
} from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
2019-08-09 10:26:22 +00:00
import React from "react";
2019-08-29 10:55:56 +00:00
import { FormattedMessage, useIntl } from "react-intl";
2019-09-02 13:52:43 +00:00
import Layout from "../Layout";
2019-06-19 14:40:52 +00:00
import { ControlledCheckbox } from "@saleor/components/ControlledCheckbox";
import Form from "@saleor/components/Form";
import { FormSpacer } from "@saleor/components/FormSpacer";
2019-08-29 10:55:56 +00:00
import { commonMessages } from "@saleor/intl";
2019-06-19 14:40:52 +00:00
export interface FormData {
email: string;
loading: boolean;
password: string;
rememberMe: boolean;
}
const styles = (theme: Theme) =>
createStyles({
buttonContainer: {
display: "flex",
justifyContent: "space-between"
},
link: {
color: theme.palette.primary.main,
cursor: "pointer",
textAlign: "center"
},
loginButton: {
width: 140
},
panel: {
"& span": {
color: theme.palette.error.contrastText
},
background: theme.palette.error.main,
borderRadius: theme.spacing.unit,
marginBottom: theme.spacing.unit * 3,
padding: theme.spacing.unit * 1.5
}
});
export interface LoginCardProps extends WithStyles<typeof styles> {
error: boolean;
disableLoginButton: boolean;
onPasswordRecovery: () => void;
onSubmit?(event: FormData);
}
const LoginCard = withStyles(styles, { name: "LoginCard" })(
({ classes, error, disableLoginButton, onSubmit }: LoginCardProps) => {
2019-08-29 10:55:56 +00:00
const intl = useIntl();
2019-06-19 14:40:52 +00:00
return (
<Form
initial={{ email: "", password: "", rememberMe: false }}
onSubmit={onSubmit}
>
{({ change: handleChange, data, submit: handleSubmit }) => (
2019-09-02 13:52:43 +00:00
<Layout>
{error && (
<div className={classes.panel}>
<Typography variant="caption">
<FormattedMessage defaultMessage="Sorry, your username and/or password are incorrect. Please try again." />
</Typography>
2019-06-19 14:40:52 +00:00
</div>
2019-09-02 13:52:43 +00:00
)}
<TextField
autoFocus
fullWidth
autoComplete="username"
label={intl.formatMessage(commonMessages.email)}
name="email"
onChange={handleChange}
value={data.email}
inputProps={{
"data-tc": "email"
}}
/>
<FormSpacer />
<TextField
fullWidth
autoComplete="current-password"
label={intl.formatMessage({
defaultMessage: "Password"
})}
name="password"
onChange={handleChange}
type="password"
value={data.password}
data-tc="password"
/>
<FormSpacer />
<div className={classes.buttonContainer}>
<ControlledCheckbox
checked={data.rememberMe}
label={intl.formatMessage({
defaultMessage: "Remember me",
description: "login"
})}
name="rememberMe"
onChange={handleChange}
/>
<FormSpacer />
<Button
className={classes.loginButton}
color="primary"
disabled={disableLoginButton}
variant="contained"
onClick={handleSubmit}
type="submit"
data-tc="submit"
>
<FormattedMessage defaultMessage="Login" description="button" />
</Button>
2019-06-19 14:40:52 +00:00
</div>
2019-09-02 13:52:43 +00:00
<FormSpacer />
<Typography className={classes.link}>
<FormattedMessage
defaultMessage="Reset your password"
description="button"
/>
</Typography>
</Layout>
2019-06-19 14:40:52 +00:00
)}
</Form>
);
}
);
LoginCard.displayName = "LoginCard";
export default LoginCard;