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

199 lines
5.9 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Button from "@material-ui/core/Button";
2021-01-26 22:04:54 +00:00
import CircularProgress from "@material-ui/core/CircularProgress";
import Divider from "@material-ui/core/Divider";
2019-06-19 14:40:52 +00:00
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
2021-01-26 22:04:54 +00:00
import { AvailableExternalAuthentications_shop_availableExternalAuthentications } from "@saleor/auth/types/AvailableExternalAuthentications";
2019-06-19 14:40:52 +00:00
import { FormSpacer } from "@saleor/components/FormSpacer";
2021-01-26 22:04:54 +00:00
import { SubmitPromise } from "@saleor/hooks/useForm";
2019-08-29 10:55:56 +00:00
import { commonMessages } from "@saleor/intl";
import { makeStyles } from "@saleor/theme";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
2021-01-26 22:04:54 +00:00
import LoginForm, { LoginFormData } from "./form";
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const useStyles = makeStyles(
theme => ({
2019-06-19 14:40:52 +00:00
buttonContainer: {
display: "flex",
justifyContent: "flex-end"
2019-06-19 14:40:52 +00:00
},
link: {
color: theme.palette.primary.main,
cursor: "pointer",
2021-01-26 22:04:54 +00:00
textDecoration: "underline"
},
loading: {
alignItems: "center",
display: "flex",
height: "100vh",
justifyContent: "center"
2019-06-19 14:40:52 +00:00
},
loginButton: {
width: 140
},
panel: {
"& span": {
color: theme.palette.error.contrastText
},
background: theme.palette.error.main,
2019-10-28 16:16:49 +00:00
borderRadius: theme.spacing(),
marginBottom: theme.spacing(3),
padding: theme.spacing(1.5)
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
}),
{ name: "LoginCard" }
);
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
export interface LoginCardProps {
2019-06-19 14:40:52 +00:00
error: boolean;
2021-01-26 22:04:54 +00:00
externalError: boolean;
disabled: boolean;
loading: boolean;
externalAuthentications?: AvailableExternalAuthentications_shop_availableExternalAuthentications[];
onExternalAuthentication: (pluginId: string) => void;
2019-06-19 14:40:52 +00:00
onPasswordRecovery: () => void;
2021-01-26 22:04:54 +00:00
onSubmit?: (event: LoginFormData) => SubmitPromise;
2019-06-19 14:40:52 +00:00
}
2019-10-30 14:34:24 +00:00
const LoginCard: React.FC<LoginCardProps> = props => {
2021-01-26 22:04:54 +00:00
const {
error,
externalError,
disabled,
loading,
externalAuthentications = [],
onExternalAuthentication,
onPasswordRecovery,
onSubmit
} = props;
2019-06-19 14:40:52 +00:00
2019-10-30 14:34:24 +00:00
const classes = useStyles(props);
const intl = useIntl();
2021-01-26 22:04:54 +00:00
if (loading) {
return (
<div className={classes.loading}>
<CircularProgress size={128} />
</div>
);
2020-05-25 23:38:52 +00:00
}
2019-10-30 14:34:24 +00:00
return (
2021-01-26 22:04:54 +00:00
<LoginForm onSubmit={onSubmit}>
2019-10-30 14:34:24 +00:00
{({ change: handleChange, data, submit: handleSubmit }) => (
<>
{error && (
<div className={classes.panel} data-test="loginErrorMessage">
2019-10-30 14:34:24 +00:00
<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-10-30 14:34:24 +00:00
)}
2021-01-26 22:04:54 +00:00
{externalError && (
<div className={classes.panel} data-test="loginErrorMessage">
<Typography variant="caption">
<FormattedMessage defaultMessage="Sorry, login went wrong. Please try again." />
</Typography>
</div>
)}
2019-10-30 14:34:24 +00:00
<TextField
autoFocus
fullWidth
autoComplete="username"
label={intl.formatMessage(commonMessages.email)}
name="email"
onChange={handleChange}
value={data.email}
inputProps={{
"data-test": "email"
2019-10-30 14:34:24 +00:00
}}
2021-01-26 22:04:54 +00:00
disabled={disabled}
2019-10-30 14:34:24 +00:00
/>
<FormSpacer />
<TextField
fullWidth
autoComplete="password"
label={intl.formatMessage({
defaultMessage: "Password"
})}
name="password"
onChange={handleChange}
type="password"
value={data.password}
inputProps={{
"data-test": "password"
2019-10-30 14:34:24 +00:00
}}
2021-01-26 22:04:54 +00:00
disabled={disabled}
2019-10-30 14:34:24 +00:00
/>
<FormSpacer />
<div className={classes.buttonContainer}>
<Button
className={classes.loginButton}
color="primary"
2021-01-26 22:04:54 +00:00
disabled={disabled}
2019-10-30 14:34:24 +00:00
variant="contained"
onClick={handleSubmit}
type="submit"
data-test="submit"
2019-10-30 14:34:24 +00:00
>
<FormattedMessage defaultMessage="Login" description="button" />
</Button>
</div>
<FormSpacer />
2021-01-26 22:04:54 +00:00
<Typography>
2019-10-30 14:34:24 +00:00
<FormattedMessage
2021-01-26 22:04:54 +00:00
defaultMessage="Forgot password? {resetPasswordLink}"
description="description"
values={{
resetPasswordLink: (
<a className={classes.link} onClick={onPasswordRecovery}>
<FormattedMessage
defaultMessage="Use this link to recover it"
description="link"
/>
</a>
)
}}
2019-10-30 14:34:24 +00:00
/>
</Typography>
2021-01-26 22:04:54 +00:00
{externalAuthentications.length > 0 && (
<>
<FormSpacer />
<Divider />
<FormSpacer />
<Typography>
<FormattedMessage
defaultMessage="or login using"
description="description"
/>
</Typography>
</>
)}
{externalAuthentications.map(externalAuthentication => (
<React.Fragment key={externalAuthentication.id}>
<FormSpacer />
<Button
color="primary"
fullWidth
variant="outlined"
size="large"
onClick={() =>
onExternalAuthentication(externalAuthentication.id)
}
data-test="external-authentication"
disabled={disabled}
>
{externalAuthentication.name}
</Button>
</React.Fragment>
))}
2019-10-30 14:34:24 +00:00
</>
)}
2021-01-26 22:04:54 +00:00
</LoginForm>
2019-10-30 14:34:24 +00:00
);
};
2019-06-19 14:40:52 +00:00
LoginCard.displayName = "LoginCard";
export default LoginCard;