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

196 lines
5.4 KiB
TypeScript
Raw Normal View History

import {
Button,
CircularProgress,
Divider,
TextField,
Typography
} from "@material-ui/core";
import { UserContextError } from "@saleor/auth/types";
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";
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
import { makeStyles } from "@saleor/macaw-ui";
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";
import { getErrorMessage } from "./messages";
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",
minHeight: "80vh",
2021-01-26 22:04:54 +00:00
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 {
error?: UserContextError;
2021-01-26 22:04:54 +00:00
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,
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}>
{({ change: handleChange, data }) => (
2019-10-30 14:34:24 +00:00
<>
{error && (
<div className={classes.panel} data-test="loginErrorMessage">
2019-10-30 14:34:24 +00:00
<Typography variant="caption">
{getErrorMessage(error, intl)}
2021-01-26 22:04:54 +00:00
</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"
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}
data-test-id="reset-password-link"
>
2021-01-26 22:04:54 +00:00
<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"
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;