"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import './styles.css'; export default function Home() { const [contacts, setContacts] = useState([]); const [codeword, setCodeword] = useState(""); const [session, setSession] = useState(null); const [loading, setLoading] = useState(true); const router = useRouter(); useEffect(() => { // Fetch session data from an API route fetch("/auth/session") .then((response) => response.json()) .then((data) => { console.log("Session data received:", data); setSession(data.session); setLoading(false); }) .catch((error) => { console.error("Failed to fetch session:", error); setLoading(false); }); }, []); function saveToDB() { alert("Saving contacts..."); const contactInputs = document.querySelectorAll( ".text-input" ) as NodeListOf; const contactValues = Array.from(contactInputs).map((input) => input.value); fetch("/api/databaseStorage", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: session?.user?.email || "", codeword: codeword, contacts: contactValues, }), }) .then((response) => { if (response.ok) { alert("Contacts saved successfully!"); } else { alert("Error saving contacts."); } }) .catch((error) => { console.error("Error:", error); alert("Error saving contacts."); }); } if (loading) { return
Loading...
; } // If no session, show sign-up and login buttons if (!session) { return (

Welcome to Fauxcall

We empower you to feel safe, whenever and wherever.

Make an account to begin.

Already have an account?

); } return (

welcome to Fauxcall, {session.user.nickname}!

To begin, set a codeword and emergency contacts. If you stop speaking or say the codeword, these contacts will be notified.

{/* form for setting codeword */}
e.preventDefault()} > setCodeword(e.target.value)} placeholder="Codeword" className="border border-gray-300 rounded-md p-2" />
{/* form for adding contacts */}
e.preventDefault()} > setContacts(e.target.value.split(","))} placeholder="Write down an emergency contact" className="border border-gray-300 rounded-md p-2" />

); }