2019-09-03 13:42:15 +00:00
|
|
|
import { parse as parseQs } from "qs";
|
|
|
|
import React from "react";
|
|
|
|
import { RouteComponentProps } from "react-router";
|
|
|
|
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useUser from "@saleor/hooks/useUser";
|
|
|
|
import NewPasswordPage, {
|
|
|
|
NewPasswordPageFormData
|
|
|
|
} from "../components/NewPasswordPage";
|
|
|
|
import { SetPasswordMutation } from "../mutations";
|
2020-04-07 13:29:31 +00:00
|
|
|
import {
|
|
|
|
SetPassword,
|
|
|
|
SetPassword_setPassword_errors
|
|
|
|
} from "../types/SetPassword";
|
2019-09-03 13:42:15 +00:00
|
|
|
import { NewPasswordUrlQueryParams } from "../urls";
|
|
|
|
|
|
|
|
const NewPassword: React.FC<RouteComponentProps> = ({ location }) => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const { loginByToken } = useUser();
|
2020-04-07 13:29:31 +00:00
|
|
|
const [errors, setErrors] = React.useState<
|
|
|
|
SetPassword_setPassword_errors[]
|
|
|
|
>();
|
2019-09-03 13:42:15 +00:00
|
|
|
|
|
|
|
const params: NewPasswordUrlQueryParams = parseQs(location.search.substr(1));
|
|
|
|
|
|
|
|
const handleSetPassword = async (data: SetPassword) => {
|
|
|
|
if (data.setPassword.errors.length === 0) {
|
|
|
|
loginByToken(data.setPassword.token, data.setPassword.user);
|
|
|
|
navigate("/", true);
|
2020-04-07 00:18:14 +00:00
|
|
|
} else {
|
2020-04-07 13:29:31 +00:00
|
|
|
setErrors(data.setPassword.errors);
|
2019-09-03 13:42:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SetPasswordMutation onCompleted={handleSetPassword}>
|
|
|
|
{(setPassword, setPasswordOpts) => {
|
|
|
|
const handleSubmit = (data: NewPasswordPageFormData) =>
|
|
|
|
setPassword({
|
|
|
|
variables: {
|
|
|
|
email: params.email,
|
|
|
|
password: data.password,
|
|
|
|
token: params.token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<NewPasswordPage
|
2020-04-07 13:29:31 +00:00
|
|
|
errors={errors}
|
2019-09-03 13:42:15 +00:00
|
|
|
disabled={setPasswordOpts.loading}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</SetPasswordMutation>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
NewPassword.displayName = "NewPassword";
|
|
|
|
export default NewPassword;
|