2020-05-14 09:30:32 +00:00
|
|
|
import { APP_MOUNT_URI } from "@saleor/config";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { useRequestPasswordResetMutation } from "@saleor/graphql";
|
2020-05-14 09:30:32 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2022-02-01 09:58:06 +00:00
|
|
|
import { extractMutationErrors } from "@saleor/misc";
|
2019-09-02 19:23:37 +00:00
|
|
|
import React from "react";
|
2019-09-04 13:09:19 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-09-02 19:23:37 +00:00
|
|
|
import urlJoin from "url-join";
|
|
|
|
|
|
|
|
import ResetPasswordPage, {
|
|
|
|
ResetPasswordPageFormData
|
|
|
|
} from "../components/ResetPasswordPage";
|
|
|
|
import { newPasswordUrl, passwordResetSuccessUrl } from "../urls";
|
|
|
|
|
|
|
|
const ResetPasswordView: React.FC = () => {
|
2019-09-04 13:09:19 +00:00
|
|
|
const [error, setError] = React.useState<string>();
|
2019-09-02 19:23:37 +00:00
|
|
|
const navigate = useNavigator();
|
2019-09-04 13:09:19 +00:00
|
|
|
const intl = useIntl();
|
2019-09-02 19:23:37 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const [
|
|
|
|
requestPasswordReset,
|
|
|
|
requestPasswordResetOpts
|
|
|
|
] = useRequestPasswordResetMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
if (data.requestPasswordReset.errors.length === 0) {
|
|
|
|
navigate(passwordResetSuccessUrl);
|
2019-09-04 13:09:19 +00:00
|
|
|
} else {
|
2022-03-09 08:56:55 +00:00
|
|
|
if (
|
|
|
|
data.requestPasswordReset.errors.find(err => err.field === "email")
|
|
|
|
) {
|
|
|
|
setError(
|
|
|
|
intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"Provided email address does not exist in our database."
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
setError(intl.formatMessage(commonMessages.somethingWentWrong));
|
|
|
|
}
|
2019-09-04 13:09:19 +00:00
|
|
|
}
|
2019-09-02 19:23:37 +00:00
|
|
|
}
|
2022-03-09 08:56:55 +00:00
|
|
|
});
|
2019-09-02 19:23:37 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const handleSubmit = (data: ResetPasswordPageFormData) =>
|
|
|
|
extractMutationErrors(
|
|
|
|
requestPasswordReset({
|
|
|
|
variables: {
|
|
|
|
email: data.email,
|
|
|
|
redirectUrl: urlJoin(
|
|
|
|
window.location.origin,
|
|
|
|
APP_MOUNT_URI === "/" ? "" : APP_MOUNT_URI,
|
|
|
|
newPasswordUrl().replace(/\?/, "")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2019-09-02 19:23:37 +00:00
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
return (
|
|
|
|
<ResetPasswordPage
|
|
|
|
disabled={requestPasswordResetOpts.loading}
|
|
|
|
error={error}
|
|
|
|
onBack={() => navigate(APP_MOUNT_URI)}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
/>
|
2019-09-02 19:23:37 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
ResetPasswordView.displayName = "ResetPasswordView";
|
|
|
|
export default ResetPasswordView;
|