All files / src/features/auth/components signin-form.tsx

80% Statements 8/10
66.66% Branches 6/9
75% Functions 3/4
80% Lines 8/10

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                            2x               24x           24x                     24x 2x 2x           24x                                                                                                       2x                                                                                    
import { useForm } from 'react-hook-form';
import { Button } from '@/components/button';
import { Input } from '@/components/input';
import { PasswordInput } from '@/components/password-input';
import { Checkbox } from '@/components/checkbox';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/card';
import { FormMessage } from '@/components/form-message';
import { Text } from '@/components/text';
import type { SigninFormData } from '../schemas/auth.schema';
import { signinSchema } from '../schemas/auth.schema';
import { useFormFieldError } from '../hooks/use-auth-form';
import { createSafeZodResolver } from '@/lib/form-utils';
 
type SigninFormProps = {
  onSubmit: (data: SigninFormData) => void;
  onToggleToSignup: () => void;
  isLoading?: boolean;
  error?: string | null;
  rememberMe?: boolean;
  onRememberMeChange?: (checked: boolean) => void;
};
 
export const SigninForm = ({
  onSubmit,
  onToggleToSignup,
  isLoading = false,
  error = null,
  rememberMe = false,
  onRememberMeChange,
}: SigninFormProps) => {
  const { handleFieldFocus } = useFormFieldError();
 
  const {
    control,
    handleSubmit,
    formState: { isValid },
  } = useForm<SigninFormData>({
    resolver: createSafeZodResolver(signinSchema),
    mode: 'onSubmit', // Validate only on form submission
    reValidateMode: 'onChange', // After first validation, revalidate on change
    defaultValues: {
      email: '',
      password: '',
    },
    shouldUseNativeValidation: false,
  });
 
  const handleFormSubmit = (data: SigninFormData) => {
    try {
      onSubmit(data);
    } catch (error) {
      console.error('Form submission error:', error);
    }
  };
 
  return (
    <Card className="w-full max-w-md mx-auto">
      <CardHeader className="text-center">
        <CardTitle className="text-2xl font-bold text-default">
          Welcome Back
        </CardTitle>
        <Text size="sm" variant="muted">
          Sign in to your account
        </Text>
      </CardHeader>
      <CardContent>
        {/* Error Message */}
        {error && (
          <FormMessage
            type="error"
            message={error}
            className="mb-4"
            onDismiss={undefined}
          />
        )}
 
        <form
          onSubmit={handleSubmit(handleFormSubmit, errors => {
            console.error('Validation errors:', errors);
          })}
          className="space-y-4"
        >
          <Input
            name="email"
            control={control as any}
            label="Email"
            placeholder="Enter your email"
            className="h-[50px]"
            disabled={isLoading}
            onFocus={handleFieldFocus}
            required
          />
 
          <PasswordInput
            name="password"
            control={control as any}
            label="Password"
            placeholder="Enter your password"
            className="h-[50px]"
            disabled={isLoading}
            onFocus={handleFieldFocus}
            required
          />
 
          <div className="flex items-center justify-between">
            <Checkbox
              checked={rememberMe}
              onChange={e => onRememberMeChange?.(e.target.checked)}
              label="Remember me"
              disabled={isLoading}
            />
            <Button
              type="button"
              variant="link"
              size="sm"
              className="text-sm h-auto p-0"
              disabled={isLoading}
            >
              Forgot password?
            </Button>
          </div>
 
          <Button
            type="submit"
            className="w-full bg-gradient-vertical hover:opacity-90 text-white h-[50px]"
            disabled={isLoading || !isValid}
          >
            {isLoading ? 'Signing in...' : 'Sign In'}
          </Button>
        </form>
 
        <div className="mt-6 text-center">
          <Text size="sm" variant="muted">
            Don&apos;t have an account?{' '}
            <Button
              onClick={onToggleToSignup}
              variant="link"
              size="sm"
              className="text-sm font-medium h-auto p-0"
              disabled={isLoading}
            >
              Sign up
            </Button>
          </Text>
        </div>
      </CardContent>
    </Card>
  );
};