onclick still not firing

This commit is contained in:
BGV
2025-03-30 03:31:30 -04:00
parent bfaffef684
commit accc5c49e3

View File

@@ -1,11 +1,9 @@
"use client";
import { useState } from "react"; import { useState } from "react";
import { auth0 } from "../lib/auth0"; import { auth0 } from "../lib/auth0";
export default async function Home() { export default async function Home() {
const [contacts, setContacts] = useState<string[]>([]); const [contacts, setContacts] = useState<string[]>([]);
const [codeword, setCodeword] = useState(""); const [codeword, setCodeword] = useState("");
@@ -13,6 +11,38 @@ export default async function Home() {
console.log("Session:", session?.user); console.log("Session:", session?.user);
function saveToDB() {
//e.preventDefault();
alert("Saving contacts...");
// const contactInputs = document.querySelectorAll(".text-input") as NodeListOf<HTMLInputElement>;
// const contactValues = Array.from(contactInputs).map(input => input.value);
// console.log("Contact values:", contactValues);
// // save codeword and contacts to database
// fetch("/api/databaseStorage", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// email: session?.user?.email || "",
// codeword: (document.getElementById("codeword") as HTMLInputElement)?.value,
// 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 no session, show sign-up and login buttons // If no session, show sign-up and login buttons
if (!session) { if (!session) {
@@ -134,38 +164,9 @@ export default async function Home() {
className="bg-emerald-500 text-fuchsia-300" className="bg-emerald-500 text-fuchsia-300"
type="button">Add</button> type="button">Add</button>
<button <button
type="button" type="button"
onClick={(e) => { onClick={saveToDB}
e.preventDefault();
alert("Saving contacts...");
const contactInputs = document.querySelectorAll(".text-input") as NodeListOf<HTMLInputElement>;
const contactValues = Array.from(contactInputs).map(input => input.value);
console.log("Contact values:", contactValues);
// save codeword and contacts to database
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.");
})
}}
className="bg-slate-500 text-yellow-300 text-stretch-50% font-lg rounded-md p-2">Save</button> className="bg-slate-500 text-yellow-300 text-stretch-50% font-lg rounded-md p-2">Save</button>
</form> </form>
<div> <div>
@@ -181,4 +182,6 @@ export default async function Home() {
</main> </main>
</div> </div>
); );
} }