-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathform.jsx
More file actions
102 lines (89 loc) · 2.72 KB
/
Copy pathform.jsx
File metadata and controls
102 lines (89 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { React, useState, useEffect } from "react";
import { db } from "../firebase-config";
import { GoogleMap, LoadScript } from "@react-google-maps/api";
import { collection, getDocs, addDoc } from "firebase/firestore";
const containerStyle = {
width: "400px",
height: "400px",
};
const center = {
lat: -3.745,
lng: -38.523,
};
const InputForm = () => {
// ----- read data from database -----//
// const [users, setUsers] = useState([]);
const usersCollectionsRef = collection(db, "users");
useEffect(() => {
const getUsers = async () => {
const data = await getDocs(usersCollectionsRef);
console.log(data.docs);
};
getUsers();
}, []); //call the firestore database and get the information when the page is rendered
// ----------------------------------//
const [newCallSign, setCallSign] = useState("");
const [newName, setName] = useState("");
const [newPhoneNumber, setPhoneNumber] = useState("");
const [newEmail, setEmail] = useState("");
//to create user
const handleSubmit = async (event) => {
event.preventDefault(); //stay on the same form page and prevent refreshing the form
// console.log("hihihi");
try {
await addDoc(collection(db, "users"), {
callsign: newCallSign,
email: newEmail,
name: newName,
phonenumber: newPhoneNumber,
}).then((res) => {
console.log(res);
});
// onClose();
} catch (err) {
console.error("writeToDB failed. reason :", err);
}
document.getElementById("main-form").submit();
};
return (
<form onSubmit={handleSubmit} id="main-form">
<label>Call Sign</label>
<input
required
type="text"
placeholder="Enter your call sign"
onChange={(event) => setCallSign(event.target.value)}
/>
<label>Name</label>
<input
required
type="text"
placeholder="Enter your name"
onChange={(event) => setName(event.target.value)}
/>
<label>Phone Number</label>
<input
required
type="text"
placeholder="Enter your phone number"
onChange={(event) => setPhoneNumber(event.target.value)}
/>
<label>Email</label>
<input
required
type="email"
placeholder="Enter your email"
onChange={(event) => setEmail(event.target.value)}
/>
<button type="submit">Submit</button>
{/* The map */}
<LoadScript googleMapsApiKey="YOUR_API_KEY">
<GoogleMap mapContainerStyle={containerStyle} center={center} zoom={10}>
{/* Child components, such as markers, info windows, etc. */}
<></>
</GoogleMap>
</LoadScript>
</form>
);
};
export default InputForm;