refacto: UI and component for error in loginpage

This commit is contained in:
Julien Froidefond
2025-02-25 08:46:29 +01:00
parent a690a5af6f
commit 15a27005a0
3 changed files with 20 additions and 11 deletions

View File

@@ -4,6 +4,7 @@ import { useState } from "react";
import { useRouter } from "next/navigation";
import { authService } from "@/lib/services/auth.service";
import { AuthError } from "@/types/auth";
import { ErrorMessage } from "@/components/ui/ErrorMessage";
interface LoginFormProps {
from?: string;
@@ -86,11 +87,7 @@ export function LoginForm({ from }: LoginFormProps) {
Se souvenir de moi
</label>
</div>
{error && (
<div className="rounded-md bg-destructive/15 p-3 text-sm text-destructive">
{error.message}
</div>
)}
{error && <ErrorMessage message={error.message} variant="form" />}
<button
type="submit"
disabled={isLoading}

View File

@@ -6,6 +6,7 @@ import { authService } from "@/lib/services/auth.service";
import { AuthError } from "@/types/auth";
import { ERROR_CODES } from "@/constants/errorCodes";
import { ERROR_MESSAGES } from "@/constants/errorMessages";
import { ErrorMessage } from "@/components/ui/ErrorMessage";
interface RegisterFormProps {
from?: string;
@@ -96,11 +97,7 @@ export function RegisterForm({ from }: RegisterFormProps) {
className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"
/>
</div>
{error && (
<div className="rounded-md bg-destructive/15 p-3 text-sm text-destructive">
{error.message}
</div>
)}
{error && <ErrorMessage message={error.message} variant="form" />}
<button
type="submit"
disabled={isLoading}

View File

@@ -1,10 +1,25 @@
import { AlertCircle } from "lucide-react";
import { cn } from "@/lib/utils";
interface ErrorMessageProps {
message: string;
variant?: "default" | "form";
}
export const ErrorMessage = ({ message }: ErrorMessageProps) => {
export const ErrorMessage = ({ message, variant = "default" }: ErrorMessageProps) => {
if (variant === "form") {
return (
<div
role="alert"
aria-live="assertive"
className="flex items-center gap-2 rounded-md bg-destructive/15 p-2.5 text-sm text-destructive"
>
<AlertCircle className="h-4 w-4" />
<p>{message}</p>
</div>
);
}
return (
<div
role="alert"