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";
|
|
|
|
|
2019-09-03 13:42:15 +00:00
|
|
|
import { APP_MOUNT_URI } from "@saleor/config";
|
2019-09-02 19:23:37 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
2019-09-04 13:09:19 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2019-09-02 19:23:37 +00:00
|
|
|
import ResetPasswordPage, {
|
|
|
|
ResetPasswordPageFormData
|
|
|
|
} from "../components/ResetPasswordPage";
|
|
|
|
import { RequestPasswordResetMutation } from "../mutations";
|
|
|
|
import { RequestPasswordReset } from "../types/RequestPasswordReset";
|
|
|
|
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
|
|
|
|
|
|
|
const handleRequestPasswordReset = (data: RequestPasswordReset) => {
|
|
|
|
if (data.requestPasswordReset.errors.length === 0) {
|
|
|
|
navigate(passwordResetSuccessUrl);
|
2019-09-04 13:09:19 +00:00
|
|
|
} else {
|
|
|
|
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-02 19:23:37 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RequestPasswordResetMutation onCompleted={handleRequestPasswordReset}>
|
|
|
|
{(requestPasswordReset, requestPasswordResetOpts) => {
|
|
|
|
const handleSubmit = (data: ResetPasswordPageFormData) =>
|
|
|
|
requestPasswordReset({
|
|
|
|
variables: {
|
|
|
|
email: data.email,
|
2019-09-03 13:42:15 +00:00
|
|
|
redirectUrl: urlJoin(
|
|
|
|
window.location.origin,
|
2019-09-04 13:30:42 +00:00
|
|
|
APP_MOUNT_URI === "/" ? "" : APP_MOUNT_URI,
|
2019-09-03 13:42:15 +00:00
|
|
|
newPasswordUrl().replace(/\?/, "")
|
|
|
|
)
|
2019-09-02 19:23:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ResetPasswordPage
|
|
|
|
disabled={requestPasswordResetOpts.loading}
|
2019-09-04 13:09:19 +00:00
|
|
|
error={error}
|
2019-09-02 19:23:37 +00:00
|
|
|
onSubmit={handleSubmit}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</RequestPasswordResetMutation>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
ResetPasswordView.displayName = "ResetPasswordView";
|
|
|
|
export default ResetPasswordView;
|