import LoginForm, { type LoginFormValue } from "./LoginForm";

export default function LoginFormData() {
	function handleOnFormSubmit(data: LoginFormValue) {
		const searchParams = new URLSearchParams(window.location.search);

		fetch("/api/accounts/login", {
			method: "POST",
			body: JSON.stringify(data),
			credentials: "include",
			headers: {
				Accept: "application/json",
				"Content-Type": "application/json",
			},
		})
			.then((res) => res.json())
			.then((json) => {
				window.location.href = searchParams.get("redirect") || "/home";
			})
			.catch((err) => console.log(err));
	}

	return <LoginForm onSubmit={handleOnFormSubmit} />;
}
