import { useForm } from "@tanstack/react-form"; import { Button, Input } from "@mui/joy"; import { useMutation } from "@tanstack/react-query"; import { signInUser } from "../utils/api/auth"; import { useTranslation } from "react-i18next"; import { useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; import type { AlertInterface } from "../misc/interfaces"; import { MyAlert } from "./MyAlert.tsx"; export const LoginCard = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [alert, setAlert] = useState({ isAlert: false, type: "neutral", header: "", text: "", }); useEffect(() => { setAlert({ isAlert: true, type: "primary", header: t("success"), text: t("logout-success-text"), }); }, []); const form = useForm({ defaultValues: { username: "", password: "", }, onSubmit: async ({ value }) => { mutate({ username: value.username, password: value.password }); }, }); const { mutate, isPending } = useMutation({ mutationFn: ({ username, password, }: { username: string; password: string; }) => signInUser(username, password, t), onSuccess: (result) => { if (result.ok) { setAlert({ isAlert: false, type: "neutral", header: "", text: "", }); navigate({ to: "/app/inventory" }); } }, onError: (error: unknown) => { const errorCode = (error as { code?: string })?.code; setAlert({ isAlert: true, type: "danger", header: t("error"), text: errorCode ? t(errorCode) : t("unknown-error"), }); }, }); return (

Stockhome

{t("login")}

{ e.preventDefault(); form.handleSubmit(); }} > {alert.isAlert && ( )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("username")} variant="outlined" size="lg" className="rounded-2xl" sx={{ bgcolor: "var(--joy-palette-background-surface)", boxShadow: "0 10px 24px var(--joy-palette-divider)", }} /> )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("password")} variant="outlined" size="lg" className="rounded-2xl" sx={{ bgcolor: "var(--joy-palette-background-surface)", boxShadow: "0 10px 24px var(--joy-palette-divider)", }} /> )}
); };