saleor-dashboard/src/auth/components/NewPasswordPage/NewPasswordPage.tsx
Dawid Tarasiuk 4880093f63
Use Auth SDK (#1474)
* Use Auth SDK

* Update auth provider hook

* Update sdk module mapping

* Update setting password

* Fix no user details on first login

* Update auth tests

* Cleanups

* Update SDK

Update SDK

Update SDK

Update test recordings

Update SDK

* Implement SDK External Auth

Update new password view

Hnalde external logout

Update SDK

Fix logout external redirect

* Fix login page style

* Update SDK

* Auth Provider cleanups

Update and refactor auth

Auth types cleanups and refactor

* Update channel context provider

* Fix login error handling

* Logout immidiatelly non-staff user

* Update test snapshots

* Trigger CI

* Update to SDK v0.4, remove duplicated UserContext hook

* Handle server errors during login

* Fix wrong login page form submition handling

* Update login error messages

Co-authored-by: Jakub Majorek <majorek.jakub@gmail.com>
2021-12-17 12:10:54 +01:00

136 lines
3.8 KiB
TypeScript

import { Button, TextField, Typography } from "@material-ui/core";
import Form from "@saleor/components/Form";
import FormSpacer from "@saleor/components/FormSpacer";
import { makeStyles } from "@saleor/macaw-ui";
import { SetPasswordData } from "@saleor/sdk";
import getAccountErrorMessage from "@saleor/utils/errors/account";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
const useStyles = makeStyles(
theme => ({
errorText: {
color: theme.palette.error.contrastText
},
panel: {
background: theme.palette.error.main,
borderRadius: theme.spacing(),
marginBottom: theme.spacing(3),
padding: theme.spacing(1.5)
},
submit: {
width: "100%"
}
}),
{
name: "NewPasswordPage"
}
);
export interface NewPasswordPageFormData {
password: string;
confirmPassword: string;
}
export interface NewPasswordPageProps {
disabled: boolean;
errors: SetPasswordData["errors"];
onSubmit: (data: NewPasswordPageFormData) => void;
}
const initialForm: NewPasswordPageFormData = {
confirmPassword: "",
password: ""
};
const NewPasswordPage: React.FC<NewPasswordPageProps> = props => {
const { disabled, errors, onSubmit } = props;
const classes = useStyles(props);
const intl = useIntl();
const error = getAccountErrorMessage(
errors.find(err => err.field === "password"),
intl
);
return (
<Form initial={initialForm} onSubmit={onSubmit}>
{({ change: handleChange, data, submit: handleSubmit }) => {
const passwordError =
data.password !== data.confirmPassword && data.password.length > 0;
return (
<>
{!!error && (
<div className={classes.panel}>
<Typography variant="caption" className={classes.errorText}>
{error}
</Typography>
</div>
)}
<Typography>
<FormattedMessage defaultMessage="Please set up a new password." />
</Typography>
<FormSpacer />
<TextField
autoFocus
fullWidth
autoComplete="none"
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "New Password"
})}
name="password"
onChange={handleChange}
type="password"
value={data.password}
inputProps={{
"data-test": "password"
}}
/>
<FormSpacer />
<TextField
fullWidth
error={passwordError}
autoComplete="none"
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "Confirm Password"
})}
name="confirmPassword"
onChange={handleChange}
type="password"
value={data.confirmPassword}
helperText={
passwordError &&
intl.formatMessage({
defaultMessage: "Passwords do not match"
})
}
inputProps={{
"data-test": "confirm-password"
}}
/>
<FormSpacer />
<Button
data-test="button-bar-confirm"
className={classes.submit}
color="primary"
disabled={(passwordError && data.password.length > 0) || disabled}
variant="contained"
onClick={handleSubmit}
type="submit"
>
<FormattedMessage
defaultMessage="Set new password"
description="button"
/>
</Button>
</>
);
}}
</Form>
);
};
NewPasswordPage.displayName = "NewPasswordPage";
export default NewPasswordPage;