import { Modal } from "flowbite"; import RedirectTo from '../../lib/redirect_to'; import { useEffect, useState } from "react"; import { ApiClient } from "../../clients/api_client"; import Token from "../../lib/token"; import "../stylesheets/shared.css" import redirectTo from "../../lib/redirect_to"; export function LoginForm() { const [error_message, setErrorMessage] = useState(''); // Error rendering useEffect useEffect(() => { document.getElementById("errorMessage").innerHTML = error_message; }, [error_message]); // Modal useEffect useEffect(() => { const target = document.getElementById("loginModal"); const options = { closable: true }; const modal = new Modal(target, options); document.getElementById('closeModalButton')?.addEventListener("click", () => { modal.hide(); }); document.getElementById('loginButton')?.addEventListener("click", () => { modal.show(); }); }, []); async function handleLogin(event) { event.preventDefault(); const client = new ApiClient(); const form = event.target; const formData = new FormData(form); const headers = { "Content-Type": "multipart/form-data" }; try { const response = await client.post("/login", formData, headers); const token = new Token(); token.set(response.data.token); token.setRefresh(response.data.refresh); redirectTo('/products'); } catch(error) { setErrorMessage(error.response.data.errors.auth); } } return( <> ); }