You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.6 KiB

  1. import { useRef, useState, FormEventHandler } from 'react';
  2. import DangerButton from '@/Components/DangerButton';
  3. import InputError from '@/Components/InputError';
  4. import InputLabel from '@/Components/InputLabel';
  5. import Modal from '@/Components/Modal';
  6. import SecondaryButton from '@/Components/SecondaryButton';
  7. import TextInput from '@/Components/TextInput';
  8. import { useForm } from '@inertiajs/react';
  9. export default function DeleteUserForm({ className = '' }: { className?: string }) {
  10. const [confirmingUserDeletion, setConfirmingUserDeletion] = useState(false);
  11. const passwordInput = useRef<HTMLInputElement>(null);
  12. const {
  13. data,
  14. setData,
  15. delete: destroy,
  16. processing,
  17. reset,
  18. errors,
  19. } = useForm({
  20. password: '',
  21. });
  22. const confirmUserDeletion = () => {
  23. setConfirmingUserDeletion(true);
  24. };
  25. const deleteUser: FormEventHandler = (e) => {
  26. e.preventDefault();
  27. destroy(route('profile.destroy'), {
  28. preserveScroll: true,
  29. onSuccess: () => closeModal(),
  30. onError: () => passwordInput.current?.focus(),
  31. onFinish: () => reset(),
  32. });
  33. };
  34. const closeModal = () => {
  35. setConfirmingUserDeletion(false);
  36. reset();
  37. };
  38. return (
  39. <section className={`space-y-6 ${className}`}>
  40. <header>
  41. <h2 className="text-lg font-medium text-gray-900">Delete Account</h2>
  42. <p className="mt-1 text-sm text-gray-600">
  43. Once your account is deleted, all of its resources and data will be permanently deleted. Before
  44. deleting your account, please download any data or information that you wish to retain.
  45. </p>
  46. </header>
  47. <DangerButton onClick={confirmUserDeletion}>Delete Account</DangerButton>
  48. <Modal show={confirmingUserDeletion} onClose={closeModal}>
  49. <form onSubmit={deleteUser} className="p-6">
  50. <h2 className="text-lg font-medium text-gray-900">
  51. Are you sure you want to delete your account?
  52. </h2>
  53. <p className="mt-1 text-sm text-gray-600">
  54. Once your account is deleted, all of its resources and data will be permanently deleted. Please
  55. enter your password to confirm you would like to permanently delete your account.
  56. </p>
  57. <div className="mt-6">
  58. <InputLabel htmlFor="password" value="Password" className="sr-only" />
  59. <TextInput
  60. id="password"
  61. type="password"
  62. name="password"
  63. ref={passwordInput}
  64. value={data.password}
  65. onChange={(e) => setData('password', e.target.value)}
  66. className="mt-1 block w-3/4"
  67. isFocused
  68. placeholder="Password"
  69. />
  70. <InputError message={errors.password} className="mt-2" />
  71. </div>
  72. <div className="mt-6 flex justify-end">
  73. <SecondaryButton onClick={closeModal}>Cancel</SecondaryButton>
  74. <DangerButton className="ms-3" disabled={processing}>
  75. Delete Account
  76. </DangerButton>
  77. </div>
  78. </form>
  79. </Modal>
  80. </section>
  81. );
  82. }