-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCrudscreen.js
190 lines (181 loc) · 4.49 KB
/
FCrudscreen.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import {
View,
Text,
StyleSheet,
SafeAreaView,
Button,
TouchableOpacity,
} from "react-native";
import React, { useEffect, useState } from "react";
import { TextInput } from "react-native-paper";
import {
collection,
doc,
setDoc,
addDoc,
updateDoc,
deleteDoc,
getDoc,
getDocs,
where,
query,
} from "firebase/firestore";
import { db } from "../firebase";
const FCrudscreen = () => {
const [username, setName] = useState("");
const [email, setEmail] = useState("");
function createDcumt() {
setDoc(doc(db, "users", "LA"), {
username: username,
email: email,
})
.then(() => {
// Data saved successfully!
console.log("data submitted");
})
.catch((error) => {
// The write failed...
console.log(error);
});
}
//add new document
function addDcumt() {
addDoc(collection(db, "users"), {
username: username,
email: email,
})
.then(() => {
// Data saved successfully!
console.log("data submitted");
})
.catch((error) => {
// The write failed...
console.log(error);
});
}
//update document
function updateDcumt() {
updateDoc(doc(db, "users", "LA"), {
username: username,
email: email,
})
.then(() => {
// Data saved successfully!
console.log("data submitted");
})
.catch((error) => {
// The write failed...
console.log(error);
});
}
//delete document
function deleteDcumt() {
deleteDoc(doc(db, "users", "1xqbg9DP0mLsNPHXK9jG"));
}
//Read document
function readDcumt() {
const docRef = doc(db, "users", "LA");
getDoc(docRef)
.then((docData) => {
if (docData.exists()) {
// console.log("Document data:", docData.data());
setName(docData.data().username);
setEmail(docData.data().email);
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
})
.catch((error) => {
console.log("Error getting document:", error);
});
}
//Read all documents
function readAllDcumt() {
const q = collection(db, "users");
getDocs(q).then((docSnap) => {
let users = [];
docSnap.forEach((doc) => {
users.push({ ...doc.data(), id: doc.id });
});
console.log("Document data: ", users);
});
}
//get data using a query
function readDcumtUsingQuery() {
getDocs(
query(collection(db, "users"), where("email", "==", "[email protected]"))
).then((docSnap) => {
let users = [];
docSnap.forEach((doc) => {
users.push({ ...doc.data(), id: doc.id });
});
console.log("Document data: ", users[0].username);
});
}
return (
<SafeAreaView style={styles.container}>
<Text style={styles.text}>Firebase crud!</Text>
<TextInput
value={username}
onChangeText={(text) => setName(text)}
placeholder="Username"
style={styles.textBoxes}
/>
<TextInput
value={email}
onChangeText={(text) => setEmail(text)}
placeholder="Email"
style={styles.textBoxes}
/>
<TouchableOpacity style={styles.selectButton} onPress={deleteDcumt}>
<Text style={styles.buttonText}>Submit Data</Text>
</TouchableOpacity>
<TouchableOpacity
style={styles.selectButton}
onPress={readDcumtUsingQuery}
>
<Text style={styles.buttonText}>Fetch Data</Text>
</TouchableOpacity>
{/* <button onPress={create}>Submit Data </button> */}
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
text: {
color: "black",
fontSize: 20,
fontWeight: "bold",
},
textBoxes: {
width: "80%",
fontSize: 18,
padding: 4,
// borderColor: "Gray",
// borderWidth: 0.2,
// borderRadius: 10,
marginBottom: 10,
marginTop: 10,
},
selectButton: {
borderRadius: 5,
width: 150,
height: 50,
backgroundColor: "blue",
alignItems: "center",
justifyContent: "center",
marginBottom: 10,
},
buttonText: {
color: "white",
fontSize: 18,
fontWeight: "bold",
},
});
export default FCrudscreen;