fix multiple emergency contact imput

This commit is contained in:
BGV
2025-03-30 08:40:31 -04:00
parent 7fd520fd86
commit d2bc4731f7

View File

@@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
export default function Home() { export default function Home() {
const [contacts, setContacts] = useState<string[]>([]); const [contacts, setContacts] = useState<string[]>([""]);
const [codeword, setCodeword] = useState(""); const [codeword, setCodeword] = useState("");
const [session, setSession] = useState<any>(null); const [session, setSession] = useState<any>(null);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@@ -23,6 +23,16 @@ export default function Home() {
}); });
}, []); }, []);
const handleInputChange = (index: number, value: string) => {
const updatedContacts = [...contacts];
updatedContacts[index] = value; // Update the specific input value
setContacts(updatedContacts);
};
const addContactInput = () => {
setContacts([...contacts, ""]); // Add a new empty input
};
function saveToDB() { function saveToDB() {
alert("Saving contacts..."); alert("Saving contacts...");
const contactInputs = document.querySelectorAll( const contactInputs = document.querySelectorAll(
@@ -181,45 +191,20 @@ export default function Home() {
className="space-y-5 flex flex-col gap-[32px] row-start-2 items-center sm:items-start" className="space-y-5 flex flex-col gap-[32px] row-start-2 items-center sm:items-start"
onSubmit={(e) => e.preventDefault()} onSubmit={(e) => e.preventDefault()}
> >
<input {contacts.map((contact, index) => (
type="text" <input
value={contacts} key={index}
onChange={(e) => setContacts(e.target.value.split(","))} type="text"
placeholder="Write down an emergency contact" value={contact}
className="border border-gray-300 rounded-md p-2" onChange={(e) => handleInputChange(index, e.target.value)}
/> placeholder={`Contact ${index + 1}`}
<input className="border border-gray-300 rounded-md p-2"
type="text" />
value={contacts} ))}
onChange={(e) => setContacts(e.target.value.split(","))}
placeholder="Write down an emergency contact"
className="border border-gray-300 rounded-md p-2"
/>
<input
type="text"
value={contacts}
onChange={(e) => setContacts(e.target.value.split(","))}
placeholder="Write down an emergency contact"
className="border border-gray-300 rounded-md p-2"
/>
<input
type="text"
value={contacts}
onChange={(e) => setContacts(e.target.value.split(","))}
placeholder="Write down an emergency contact"
className="text-input border border-gray-300 rounded-md p-2"
/>
<button <button
onClick={() => { onClick={addContactInput}
alert("Adding contact..."); className="bg-emerald-500 text-white
let elem = document.getElementsByClassName( font-semibold font-lg rounded-md p-2"
"text-input"
)[0] as HTMLElement;
console.log("Element:", elem);
let d = elem.cloneNode(true) as HTMLElement;
document.getElementById("Contacts")?.appendChild(d);
}}
className="bg-emerald-500 text-fuchsia-300"
type="button" type="button"
> >
Add Add