2021-07-31 17:22:48 +00:00
|
|
|
import type { FunctionComponent } from "react";
|
|
|
|
import { useState } from "react";
|
2021-09-24 23:09:20 +00:00
|
|
|
import { useMutation } from "blitz";
|
2021-07-31 17:22:48 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
2021-09-25 12:58:28 +00:00
|
|
|
import Alert from "../../core/components/alert";
|
2021-07-31 17:22:48 +00:00
|
|
|
import Button from "./button";
|
|
|
|
import SettingsSection from "./settings-section";
|
|
|
|
|
|
|
|
import appLogger from "../../../integrations/logger";
|
2021-09-24 23:09:20 +00:00
|
|
|
import changePassword from "../mutations/change-password";
|
2021-07-31 17:22:48 +00:00
|
|
|
|
|
|
|
const logger = appLogger.child({ module: "update-password" });
|
|
|
|
|
|
|
|
type Form = {
|
2021-09-24 23:09:20 +00:00
|
|
|
currentPassword: string;
|
2021-07-31 17:22:48 +00:00
|
|
|
newPassword: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const UpdatePassword: FunctionComponent = () => {
|
2021-09-24 23:09:20 +00:00
|
|
|
const changePasswordMutation = useMutation(changePassword)[0];
|
2021-07-31 17:22:48 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
formState: { isSubmitting, isSubmitSuccessful },
|
|
|
|
} = useForm<Form>();
|
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
|
2021-09-24 23:09:20 +00:00
|
|
|
const onSubmit = handleSubmit(async ({ currentPassword, newPassword }) => {
|
2021-07-31 17:22:48 +00:00
|
|
|
if (isSubmitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-24 23:09:20 +00:00
|
|
|
setErrorMessage("");
|
2021-07-31 17:22:48 +00:00
|
|
|
|
|
|
|
try {
|
2021-09-24 23:09:20 +00:00
|
|
|
await changePasswordMutation({ currentPassword, newPassword });
|
2021-08-27 18:05:44 +00:00
|
|
|
} catch (error: any) {
|
2021-09-24 23:09:20 +00:00
|
|
|
logger.error(error, "error updating user infos");
|
|
|
|
setErrorMessage(error.message);
|
2021-07-31 17:22:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SettingsSection
|
|
|
|
title="Update Password"
|
|
|
|
description="Make sure you are using a long, random password to stay secure."
|
|
|
|
>
|
|
|
|
<form onSubmit={onSubmit}>
|
|
|
|
{errorMessage ? (
|
|
|
|
<div className="mb-8">
|
2021-08-01 14:03:49 +00:00
|
|
|
<Alert title="Oops, there was an issue" message={errorMessage} variant="error" />
|
2021-07-31 17:22:48 +00:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
2021-09-24 23:09:20 +00:00
|
|
|
{!isSubmitting && isSubmitSuccessful && !errorMessage ? (
|
2021-07-31 17:22:48 +00:00
|
|
|
<div className="mb-8">
|
2021-08-01 14:03:49 +00:00
|
|
|
<Alert title="Saved successfully" message="Your changes have been saved." variant="success" />
|
2021-07-31 17:22:48 +00:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
<div className="shadow sm:rounded-md sm:overflow-hidden">
|
|
|
|
<div className="px-4 py-5 bg-white space-y-6 sm:p-6">
|
|
|
|
<div>
|
|
|
|
<label
|
2021-09-24 23:09:20 +00:00
|
|
|
htmlFor="currentPassword"
|
2021-07-31 17:22:48 +00:00
|
|
|
className="flex justify-between text-sm font-medium leading-5 text-gray-700"
|
|
|
|
>
|
2021-09-24 23:09:20 +00:00
|
|
|
<div>Current password</div>
|
2021-07-31 17:22:48 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1 rounded-md shadow-sm">
|
|
|
|
<input
|
2021-09-24 23:09:20 +00:00
|
|
|
id="currentPassword"
|
2021-07-31 17:22:48 +00:00
|
|
|
type="password"
|
|
|
|
tabIndex={3}
|
|
|
|
className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md placeholder-gray-400 focus:outline-none focus:shadow-outline-primary focus:border-primary-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5"
|
2021-09-24 23:09:20 +00:00
|
|
|
{...register("currentPassword")}
|
2021-07-31 17:22:48 +00:00
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label
|
2021-09-24 23:09:20 +00:00
|
|
|
htmlFor="newPassword"
|
2021-07-31 17:22:48 +00:00
|
|
|
className="flex justify-between text-sm font-medium leading-5 text-gray-700"
|
|
|
|
>
|
2021-09-24 23:09:20 +00:00
|
|
|
<div>New password</div>
|
2021-07-31 17:22:48 +00:00
|
|
|
</label>
|
|
|
|
<div className="mt-1 rounded-md shadow-sm">
|
|
|
|
<input
|
2021-09-24 23:09:20 +00:00
|
|
|
id="newPassword"
|
2021-07-31 17:22:48 +00:00
|
|
|
type="password"
|
|
|
|
tabIndex={4}
|
|
|
|
className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md placeholder-gray-400 focus:outline-none focus:shadow-outline-primary focus:border-primary-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5"
|
2021-09-24 23:09:20 +00:00
|
|
|
{...register("newPassword")}
|
2021-07-31 17:22:48 +00:00
|
|
|
required
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="px-4 py-3 bg-gray-50 text-right text-sm font-medium sm:px-6">
|
|
|
|
<Button variant="default" type="submit" isDisabled={isSubmitting}>
|
|
|
|
{isSubmitting ? "Saving..." : "Save"}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</SettingsSection>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UpdatePassword;
|