Display validation errors on NewPassword page
This commit is contained in:
parent
55512b4d2c
commit
e8375d6627
3 changed files with 37 additions and 5 deletions
|
@ -9,8 +9,8 @@ storiesOf("Views / Authentication / Set up a new password", module)
|
||||||
.addDecorator(CardDecorator)
|
.addDecorator(CardDecorator)
|
||||||
.addDecorator(Decorator)
|
.addDecorator(Decorator)
|
||||||
.add("default", () => (
|
.add("default", () => (
|
||||||
<NewPasswordPage disabled={false} onSubmit={() => undefined} />
|
<NewPasswordPage error={null} disabled={false} onSubmit={() => undefined} />
|
||||||
))
|
))
|
||||||
.add("loading", () => (
|
.add("loading", () => (
|
||||||
<NewPasswordPage disabled={true} onSubmit={() => undefined} />
|
<NewPasswordPage error={null} disabled={true} onSubmit={() => undefined} />
|
||||||
));
|
));
|
||||||
|
|
|
@ -9,11 +9,20 @@ import Form from "@saleor/components/Form";
|
||||||
import FormSpacer from "@saleor/components/FormSpacer";
|
import FormSpacer from "@saleor/components/FormSpacer";
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
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: {
|
submit: {
|
||||||
width: "100%"
|
width: "100%"
|
||||||
}
|
}
|
||||||
},
|
}),
|
||||||
{
|
{
|
||||||
name: "NewPasswordPage"
|
name: "NewPasswordPage"
|
||||||
}
|
}
|
||||||
|
@ -25,6 +34,7 @@ export interface NewPasswordPageFormData {
|
||||||
}
|
}
|
||||||
export interface NewPasswordPageProps {
|
export interface NewPasswordPageProps {
|
||||||
disabled: boolean;
|
disabled: boolean;
|
||||||
|
error: string;
|
||||||
onSubmit: (data: NewPasswordPageFormData) => void;
|
onSubmit: (data: NewPasswordPageFormData) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +44,7 @@ const initialForm: NewPasswordPageFormData = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const NewPasswordPage: React.FC<NewPasswordPageProps> = props => {
|
const NewPasswordPage: React.FC<NewPasswordPageProps> = props => {
|
||||||
const { disabled, onSubmit } = props;
|
const { disabled, error, onSubmit } = props;
|
||||||
|
|
||||||
const classes = useStyles(props);
|
const classes = useStyles(props);
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
@ -47,6 +57,13 @@ const NewPasswordPage: React.FC<NewPasswordPageProps> = props => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{!!error && (
|
||||||
|
<div className={classes.panel}>
|
||||||
|
<Typography variant="caption" className={classes.errorText}>
|
||||||
|
{error}
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<Typography>
|
<Typography>
|
||||||
<FormattedMessage defaultMessage="Please set up a new password." />
|
<FormattedMessage defaultMessage="Please set up a new password." />
|
||||||
</Typography>
|
</Typography>
|
||||||
|
|
|
@ -4,6 +4,9 @@ import { RouteComponentProps } from "react-router";
|
||||||
|
|
||||||
import useNavigator from "@saleor/hooks/useNavigator";
|
import useNavigator from "@saleor/hooks/useNavigator";
|
||||||
import useUser from "@saleor/hooks/useUser";
|
import useUser from "@saleor/hooks/useUser";
|
||||||
|
import { useIntl } from "react-intl";
|
||||||
|
import { commonMessages } from "@saleor/intl";
|
||||||
|
import getAccountErrorMessage from "@saleor/utils/errors/account";
|
||||||
import NewPasswordPage, {
|
import NewPasswordPage, {
|
||||||
NewPasswordPageFormData
|
NewPasswordPageFormData
|
||||||
} from "../components/NewPasswordPage";
|
} from "../components/NewPasswordPage";
|
||||||
|
@ -14,6 +17,8 @@ import { NewPasswordUrlQueryParams } from "../urls";
|
||||||
const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
|
const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
|
||||||
const navigate = useNavigator();
|
const navigate = useNavigator();
|
||||||
const { loginByToken } = useUser();
|
const { loginByToken } = useUser();
|
||||||
|
const [error, setError] = React.useState<string>();
|
||||||
|
const intl = useIntl();
|
||||||
|
|
||||||
const params: NewPasswordUrlQueryParams = parseQs(location.search.substr(1));
|
const params: NewPasswordUrlQueryParams = parseQs(location.search.substr(1));
|
||||||
|
|
||||||
|
@ -21,6 +26,15 @@ const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
|
||||||
if (data.setPassword.errors.length === 0) {
|
if (data.setPassword.errors.length === 0) {
|
||||||
loginByToken(data.setPassword.token, data.setPassword.user);
|
loginByToken(data.setPassword.token, data.setPassword.user);
|
||||||
navigate("/", true);
|
navigate("/", true);
|
||||||
|
} else {
|
||||||
|
const error = data.setPassword.errors.find(
|
||||||
|
err => err.field === "password"
|
||||||
|
);
|
||||||
|
if (error) {
|
||||||
|
setError(getAccountErrorMessage(error, intl));
|
||||||
|
} else {
|
||||||
|
setError(intl.formatMessage(commonMessages.somethingWentWrong));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -38,6 +52,7 @@ const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<NewPasswordPage
|
<NewPasswordPage
|
||||||
|
error={error}
|
||||||
disabled={setPasswordOpts.loading}
|
disabled={setPasswordOpts.loading}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in a new issue