ResetPasswordContainer.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { useRouter } from 'next/router';
  2. import React from 'react';
  3. import { toast } from 'react-hot-toast';
  4. import { Button } from '../../../../components/ui/Button';
  5. import { trpc } from '../../../../utils/trpc';
  6. import { AuthFormLayout } from '../../components/AuthFormLayout';
  7. import { ResetPasswordForm } from '../../components/ResetPasswordForm';
  8. type Props = {
  9. isRequested: boolean;
  10. };
  11. type FormValues = { password: string };
  12. export const ResetPasswordContainer: React.FC<Props> = ({ isRequested }) => {
  13. const router = useRouter();
  14. const utils = trpc.useContext();
  15. const resetPassword = trpc.auth.changeOperatorPassword.useMutation({
  16. onSuccess: () => {
  17. utils.auth.checkPasswordChangeRequest.invalidate();
  18. },
  19. onError: (error) => {
  20. toast.error(`Failed to reset password ${error.message}`);
  21. },
  22. });
  23. const cancelRequest = trpc.auth.cancelPasswordChangeRequest.useMutation({
  24. onSuccess: () => {
  25. utils.auth.checkPasswordChangeRequest.invalidate();
  26. toast.success('Password change request cancelled');
  27. },
  28. });
  29. const handlerSubmit = (value: FormValues) => {
  30. resetPassword.mutate({ newPassword: value.password });
  31. };
  32. const renderContent = () => {
  33. if (resetPassword.isSuccess) {
  34. return (
  35. <>
  36. <h2 className="h2 text-center mb-3">Password reset</h2>
  37. <p>Your password has been reset. You can now login with your new password. And your email {resetPassword.data.email}</p>
  38. <Button onClick={() => router.push('/login')} type="button" className="btn btn-primary w-100">
  39. Back to login
  40. </Button>
  41. </>
  42. );
  43. }
  44. if (isRequested) {
  45. return <ResetPasswordForm onSubmit={handlerSubmit} onCancel={() => cancelRequest.mutate()} loading={resetPassword.isLoading} />;
  46. }
  47. return (
  48. <>
  49. <h2 className="h2 text-center mb-3">Reset your password</h2>
  50. <p>Run this command on your server and then refresh this page</p>
  51. <pre>
  52. <code>./scripts/reset-password.sh</code>
  53. </pre>
  54. </>
  55. );
  56. };
  57. return <AuthFormLayout>{renderContent()}</AuthFormLayout>;
  58. };