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

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

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 114 115 116 117 118 119                                                                                                                                                                                                                                             
import { Button } from '@/Components/ui/Button';
import { Input } from '@/Components/ui/Input';
import { FormField } from '@/Components/molecules/FormField';
import { Transition } from '@headlessui/react';
import { useForm } from '@inertiajs/react';
import { FormEventHandler, useRef } from 'react';
 
export default function UpdatePasswordForm({
    className = '',
}: {
    className?: string;
}) {
    const passwordInput = useRef<HTMLInputElement>(null);
    const currentPasswordInput = useRef<HTMLInputElement>(null);
 
    const {
        data,
        setData,
        errors,
        put,
        reset,
        processing,
        recentlySuccessful,
    } = useForm({
        current_password: '',
        password: '',
        password_confirmation: '',
    });
 
    const updatePassword: FormEventHandler = (e) => {
        e.preventDefault();
 
        put(route('password.update'), {
            preserveScroll: true,
            onSuccess: () => reset(),
            onError: (errors) => {
                if (errors.password) {
                    reset('password', 'password_confirmation');
                    passwordInput.current?.focus();
                }
 
                if (errors.current_password) {
                    reset('current_password');
                    currentPasswordInput.current?.focus();
                }
            },
        });
    };
 
    return (
        <section className={className}>
            <header>
                <h2 className="text-lg font-medium text-foreground">
                    Update Password
                </h2>
 
                <p className="mt-1 text-sm text-muted-foreground">
                    Ensure your account is using a long, random password to stay secure.
                </p>
            </header>
 
            <form onSubmit={updatePassword} className="mt-6 space-y-6">
                <FormField label="Current Password" error={errors.current_password} required>
                    <Input
                        id="current_password"
                        ref={currentPasswordInput}
                        type="password"
                        value={data.current_password}
                        onChange={(e) => setData('current_password', e.target.value)}
                        autoComplete="current-password"
                        className="w-full"
                    />
                </FormField>
 
                <FormField label="New Password" error={errors.password} required>
                    <Input
                        id="password"
                        ref={passwordInput}
                        type="password"
                        value={data.password}
                        onChange={(e) => setData('password', e.target.value)}
                        autoComplete="new-password"
                        className="w-full"
                    />
                </FormField>
 
                <FormField label="Confirm Password" error={errors.password_confirmation} required>
                    <Input
                        id="password_confirmation"
                        type="password"
                        value={data.password_confirmation}
                        onChange={(e) => setData('password_confirmation', e.target.value)}
                        autoComplete="new-password"
                        className="w-full"
                    />
                </FormField>
 
                <div className="flex items-center gap-4">
                    <Button type="submit" disabled={processing} loading={processing}>
                        {processing ? 'Saving...' : 'Save'}
                    </Button>
 
                    <Transition
                        show={recentlySuccessful}
                        enter="transition ease-in-out"
                        enterFrom="opacity-0"
                        leave="transition ease-in-out"
                        leaveTo="opacity-0"
                    >
                        <p className="text-sm font-medium text-green-600 dark:text-green-400">
                            Password updated successfully!
                        </p>
                    </Transition>
                </div>
            </form>
        </section>
    );
}