All files / laravel-saas/resources/js/Pages/Profile/Partials DeleteUserForm.tsx

0% Statements 0/81
0% Branches 0/1
0% Functions 0/1
0% Lines 0/81

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                                                 
import { Button } from '@/Components/ui/Button';
import { Input } from '@/Components/ui/Input';
import { Modal } from '@/Components/ui/Modal';
import { FormField } from '@/Components/molecules/FormField';
import { useForm } from '@inertiajs/react';
import { FormEventHandler, useRef, useState } from 'react';
 
export default function DeleteUserForm({
    className = '',
}: {
    className?: string;
}) {
    const [confirmingUserDeletion, setConfirmingUserDeletion] = useState(false);
    const passwordInput = useRef<HTMLInputElement>(null);
 
    const {
        data,
        setData,
        delete: destroy,
        processing,
        reset,
        errors,
        clearErrors,
    } = useForm({
        password: '',
    });
 
    const confirmUserDeletion = () => {
        setConfirmingUserDeletion(true);
    };
 
    const deleteUser: FormEventHandler = (e) => {
        e.preventDefault();
 
        destroy(route('profile.destroy'), {
            preserveScroll: true,
            onSuccess: () => closeModal(),
            onError: () => passwordInput.current?.focus(),
            onFinish: () => reset(),
        });
    };
 
    const closeModal = () => {
        setConfirmingUserDeletion(false);
 
        clearErrors();
        reset();
    };
 
    return (
        <section className={`space-y-6 ${className}`}>
            <header>
                <h2 className="text-lg font-medium text-foreground">
                    Delete Account
                </h2>
 
                <p className="mt-1 text-sm text-muted-foreground">
                    Once your account is deleted, all of its resources and data
                    will be permanently deleted. Before deleting your account,
                    please download any data or information that you wish to
                    retain.
                </p>
            </header>
 
            <Button variant="destructive" onClick={confirmUserDeletion}>
                Delete Account
            </Button>
 
            <Modal show={confirmingUserDeletion} onClose={closeModal}>
                <form onSubmit={deleteUser} className="p-6">
                    <h2 className="text-lg font-medium text-foreground">
                        Are you sure you want to delete your account?
                    </h2>
 
                    <p className="mt-1 text-sm text-muted-foreground">
                        Once your account is deleted, all of its resources and
                        data will be permanently deleted. Please enter your
                        password to confirm you would like to permanently delete
                        your account.
                    </p>
 
                    <div className="mt-6">
                        <FormField label="Password" error={errors.password} required>
                            <Input
                                id="password"
                                type="password"
                                name="password"
                                ref={passwordInput}
                                value={data.password}
                                onChange={(e) => setData('password', e.target.value)}
                                placeholder="Enter your password to confirm"
                                autoFocus
                                className="w-full"
                            />
                        </FormField>
                        
                    </div>
 
                    <div className="mt-6 flex justify-end">
                        <Button type="button" variant="outline" onClick={closeModal}>
                            Cancel
                        </Button>
 
                        <Button type="submit" variant="destructive" className="ms-3" disabled={processing} loading={processing}>
                            Delete Account
                        </Button>
                    </div>
                </form>
            </Modal>
        </section>
    );
}