import { FC } from "react"; import React from "react"; import { SwitchProps, useSwitch } from "@heroui/switch"; import axios from "axios"; import { useTranslation } from 'react-i18next'; import { useUserStore } from "@/stores/userStore"; import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button, useDisclosure, Checkbox, Input, Link, } from "@heroui/react"; export const MailIcon = (props) => { return ( ); }; export const LockIcon = (props) => { return ( ); }; export const LoginDialog:FC = () => { const { t } = useTranslation('common') const setUser = useUserStore((s) => s.setUser); const {isOpen, onOpen, onOpenChange} = useDisclosure() const [email, setEmail] = React.useState("") const [password, setPassword] = React.useState("") const [rememberMe, setRememberMe] = React.useState(false) const [errorMessage, setErrorMessage] = React.useState(null) const [isSubmitting, setIsSubmitting] = React.useState(false) const handleSubmit = async () => { if (isSubmitting) { return; } const trimmedEmail = email.trim(); if (!trimmedEmail || !password) { setErrorMessage(t('email_and_password_required')); return; } setIsSubmitting(true); setErrorMessage(null); try { // Laravel's stateful API expects the XSRF-TOKEN cookie before posting credentials await axios.get("/sanctum/csrf-cookie", { withCredentials: true }); await axios.post( "/api/login", { email: trimmedEmail, password, remember: rememberMe, }, { headers: { "Content-Type": "application/json", Accept: "application/json", }, withCredentials: true, withXSRFToken: true, } ); const meResponse = await axios.get("/api/user", { headers: { Accept: "application/json", }, withCredentials: true, }); setUser(meResponse.data); //window.location.href = "/"; // pokud chceĆĄ full reload window.location.assign("/contests"); } catch (error) { if (axios.isAxiosError(error)) { const responseError = error.response?.data?.errors?.email || error.response?.data?.message; setErrorMessage(responseError || t("unable_to_sign_in")); } else { setErrorMessage(t("unable_to_sign_in")); } } finally { setIsSubmitting(false); } } return ( <> {t('login_dialog_label')} } label={t("email")} placeholder={t("enter_email")} variant="bordered" autoFocus={true} type="email" value={email} onValueChange={setEmail} autoComplete="email" /> } label={t("password")} placeholder={t("enter_password")} type="password" variant="bordered" value={password} onValueChange={setPassword} autoComplete="current-password" />
{t('remember_me')} {t('forgot_password')}
{errorMessage && (

{errorMessage}

)}
) } export default LoginDialog