-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp5.js
185 lines (160 loc) · 5.24 KB
/
App5.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
import React, { useState } from "react";
import {
View,
Text,
TextInput,
Button,
FlatList,
StyleSheet
} from "react-native";
import {CheckBox} from '@rneui/themed';
import { TouchableOpacity } from "react-native";
const { MongoClient, ServerApiVersion } = require("mongodb");
const uri = "mongodb+srv://13dpn13:[email protected]/SimpleList?retryWrites=true&w=majority";
const dboper = require('./operations');
const URL = uri;
const dbname = 'SimpleList';
const db = client.db(dbname);
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
async function run() {
try {
// Connect the client to the server (optional starting in v4.7)
await client.connect();
// Send a ping to confirm a successful connection
await db.command({ ping: 1 });
console.log(
"Pinged your deployment. You successfully connected to MongoDB!"
);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
run().catch(console.dir);
const ShoppingList = (() => {
return dboper.findDocuments(db, 'ShoppingList');
});
const App = () => {
const [item, setItem] = useState("");
const [notes, setNotes] = useState("");
const [shoppingList, setShoppingList] = useState([ShoppingList]);
const handleAddItem = () => {
const newItem = { item, notes };
setShoppingList([...shoppingList, newItem]);
setItem("");
setNotes("");
dboper.insertDocument(db, { item, notes}, 'ShoppingList').then (result => {
console.log('Insert Document:', result.ops);
return dboper.findDocuments(db, 'ShoppingList');
})
};
const handleDeleteSelectedItems = () => {
const filteredList = shoppingList.filter((item) => {
!item.selected;
dboper.removeDocument(db, {item, notes}, 'ShoppingList')
.then(result => {
console.log('Deleted Document Count:' , result.deletedCount);
});
});
setShoppingList(filteredList);
};
const handleToggleItem = (index) => {
const updatedList = [...shoppingList];
updatedList[index].selected = !updatedList[index].selected;
setShoppingList(updatedList);
};
return (
<TouchableOpacity>
<View style={styles.container}>
<Text style={styles.label} >Item:</Text>
<TextInput style={styles.input} value={item} onChangeText={setItem} />
<Text style={styles.label} >Notes:</Text>
<TextInput style={styles.input} value={notes} onChangeText={setNotes} />
<Button title="Add Item" onPress={handleAddItem} />
</View>
<View style={styles.container}>
<Text style={styles.label} >Shopping List:</Text>
<FlatList
data={shoppingList}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item, index }) => (
<View style={styles.input4}>
<CheckBox
value={!!item.selected}
onPress={() => handleToggleItem(index)}
iconType="material-community"
checkedIcon="checkbox-marked"
uncheckedIcon="checkbox-blank-outline"
checkedColor="red"
/>
<View style={{flexDirection: 'column', paddingLeft: 10}}>
<Text style={styles.input4} >{item.item}</Text>
<Text style={styles.input3} >{item.notes}</Text>
</View>
</View>
)}
/>
<Button
title="Delete Selected Items"
onPress={handleDeleteSelectedItems}
/>
</View>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
margin: 10,
justifyContent: 'flex-start',
padding: 20
},
label: {
fontSize: 18,
fontStyle: 'italic',
fontWeight: 'normal',
color: 'purple',
paddingBottom: 5,
paddingTop: 10
},
input: {
fontSize: 20,
color: 'blue',
padding: 5
},
label2: {
fontSize: 18,
fontStyle: 'italic',
fontWeight: 700,
color: 'purple',
paddingBottom: 5,
paddingTop: 10
},
input2: {
fontSize: 20,
color: 'blue',
fontWeight: 500,
paddingLeft: 10,
},
input3: {
fontSize: 18,
color: 'black',
fontWeight: 300,
padding: 5,
},
input4: {
fontSize: 20,
color: 'blue',
fontWeight: 500,
padding: 5,
flexDirection: 'row'
},
});
export default App;