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

139 lines
3.9 KiB
TypeScript
Raw Normal View History

2019-09-02 15:56:59 +00:00
import Button from "@material-ui/core/Button";
2019-10-28 16:16:49 +00:00
import { makeStyles } from "@material-ui/core/styles";
2019-09-02 15:56:59 +00:00
import TextField from "@material-ui/core/TextField";
import Typography from "@material-ui/core/Typography";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
import Form from "@saleor/components/Form";
import FormSpacer from "@saleor/components/FormSpacer";
import { SetPassword_setPassword_errors } from "@saleor/auth/types/SetPassword";
import getAccountErrorMessage from "@saleor/utils/errors/account";
2019-09-02 15:56:59 +00:00
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)
},
2019-09-02 15:56:59 +00:00
submit: {
width: "100%"
}
}),
2019-09-02 15:56:59 +00:00
{
name: "NewPasswordPage"
}
);
export interface NewPasswordPageFormData {
password: string;
confirmPassword: string;
}
export interface NewPasswordPageProps {
2019-09-02 19:23:37 +00:00
disabled: boolean;
errors: SetPassword_setPassword_errors[];
2019-09-02 15:56:59 +00:00
onSubmit: (data: NewPasswordPageFormData) => void;
}
const initialForm: NewPasswordPageFormData = {
confirmPassword: "",
password: ""
};
const NewPasswordPage: React.FC<NewPasswordPageProps> = props => {
const { disabled, errors, onSubmit } = props;
2019-09-02 15:56:59 +00:00
const classes = useStyles(props);
const intl = useIntl();
const error = getAccountErrorMessage(
errors.find(err => err.field === "password"),
intl
);
2019-09-02 15:56:59 +00:00
return (
<Form initial={initialForm} onSubmit={onSubmit}>
{({ change: handleChange, data, submit: handleSubmit }) => {
const passwordError =
data.password !== data.confirmPassword && data.password.length > 0;
return (
2019-09-03 13:42:15 +00:00
<>
{!!error && (
<div className={classes.panel}>
<Typography variant="caption" className={classes.errorText}>
{error}
</Typography>
</div>
)}
2019-09-02 15:56:59 +00:00
<Typography>
<FormattedMessage defaultMessage="Please set up a new password." />
</Typography>
<FormSpacer />
<TextField
autoFocus
fullWidth
autoComplete="none"
2019-09-02 19:23:37 +00:00
disabled={disabled}
2019-09-02 15:56:59 +00:00
label={intl.formatMessage({
defaultMessage: "New Password"
})}
name="password"
onChange={handleChange}
type="password"
value={data.password}
inputProps={{
"data-tc": "password"
}}
/>
<FormSpacer />
<TextField
fullWidth
error={passwordError}
autoComplete="none"
2019-09-02 19:23:37 +00:00
disabled={disabled}
2019-09-02 15:56:59 +00:00
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-tc": "confirm-password"
}}
/>
<FormSpacer />
<Button
className={classes.submit}
color="primary"
2019-09-02 19:23:37 +00:00
disabled={(passwordError && data.password.length > 0) || disabled}
2019-09-02 15:56:59 +00:00
variant="contained"
onClick={handleSubmit}
type="submit"
>
<FormattedMessage
defaultMessage="Set new password"
description="button"
/>
</Button>
2019-09-03 13:42:15 +00:00
</>
2019-09-02 15:56:59 +00:00
);
}}
</Form>
);
};
NewPasswordPage.displayName = "NewPasswordPage";
export default NewPasswordPage;