-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddPlant.js
178 lines (153 loc) · 3.87 KB
/
addPlant.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
import React, { useState, useEffect } from "react";
import { Alert, View, Text, Button, StyleSheet, Pressable } from "react-native";
import { TextInput } from "react-native-paper";
import {
getDatabase,
getInstance,
ref,
set,
get,
push,
child,
update
} from "firebase/database";
import { getAuth, onAuthStateChanged } from "firebase/auth";
import FeedScreen from "./feedscreen";
import firebaseApp from "./firebaseConfig";
const db = getDatabase(firebaseApp);
const auth = getAuth();
const NewPlant = ({ navigation, route }) => {
const [name, setName] = useState("");
const [img, setImg] = useState("");
const [description, setDescription] = useState("");
const { plantToEdit } = route.params;
const [userId, setUserId] = useState("");
onAuthStateChanged(auth, (user) => {
if (user) {
setUserId(user.currentUser.uid);
} else {
navigation.replace("Home");
}
});
saveData = async () => {
const newPostKey = push(child(ref(db), "Stekjes")).key;
if (plantToEdit !== null) {
const newPlant = {
id: plantToEdit.id,
name,
userId,
img,
description
}
const updates = {};
updates["Stekjes/" + plantToEdit.id ] = newPlant;
update(ref(db), updates);
} else {
set(ref(db, "Stekjes/" + newPostKey), {
id: newPostKey,
userId: userId,
name: name,
img: img,
description: description,
});
}
navigation.navigate("Feed", { userId });
};
useEffect(() => {
if (plantToEdit !== null) {
const {
name: editedName,
img: editedImg,
description: editedDescription,
} = plantToEdit;
setName(editedName);
setImg(editedImg);
setDescription(editedDescription);
}
}, [plantToEdit]);
return (
<View style={styles.main}>
<Text style={styles.text}> jouw plant </Text>
<Text style={styles.smallerText}> voeg hier de details toe van de plant die je in de ruil kas wilt zetten</Text>
<TextInput
label="Naam"
value={name}
onChangeText={setName}
placeholder="Naam"
style={styles.field}
mode="outlined"
outlineColor="#faf9f7"
activeOutlineColor="#1a1a1a"
/>
<TextInput
label="Afbeelding"
value={img}
onChangeText={setImg}
placeholder="+"
style={styles.field}
mode="outlined"
outlineColor="#faf9f7"
activeOutlineColor="#1a1a1a"
/>
<TextInput
label="Omschrijving"
value={description}
onChangeText={setDescription}
placeholder="Omschrijving"
style={styles.field}
mode="outlined"
outlineColor="#faf9f7"
activeOutlineColor="#1a1a1a"
/>
{plantToEdit !== null ?
<Pressable onPress={this.saveData}>
<View style={styles.button}>
<Text style={styles.buttonText}> Wijzig plant </Text>
</View>
</Pressable>:
<Pressable onPress={this.saveData}>
<View style={styles.button}>
<Text style={styles.buttonText}> Voeg plant toe! </Text>
</View>
</Pressable>
}
</View>
);
};
const styles = StyleSheet.create({
main: {
backgroundColor: '#e07a5f',
height: '100%',
alignItems: 'center',
justifyContent: 'center'
},
field: {
backgroundColor: '#faf9f7',
width: 225,
marginBottom: 10,
},
button: {
backgroundColor: '#f0c6ba',
height: 50,
width: 150,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
marginTop: 15
},
text: {
fontSize: 25,
fontWeight: 'bold',
},
smallerText: {
textAlign: 'center',
width: '70%',
fontSize: 15,
marginBottom: 15
},
buttonText: {
fontWeight: 'bold',
fontSize: 17
}
})
export default NewPlant;