-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
212 lines (199 loc) · 5.04 KB
/
App.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { StatusBar } from "expo-status-bar";
import {
Alert,
SafeAreaView,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import { AntDesign } from "@expo/vector-icons";
import { Feather } from "@expo/vector-icons";
import { useEffect, useState } from "react";
import AsyncStorage from "@react-native-async-storage/async-storage";
import Tabs from "./components/Tabs";
import Todo from "./components/Todo";
import {
onSnapshot,
query,
collection,
doc,
orderBy,
addDoc,
getDoc,
getDocs,
updateDoc,
deleteDoc,
} from "firebase/firestore";
import { dbService } from "./firebase";
export default function App() {
// delete todo
// 삭제 아이콘 터치 시 해당 todo 삭제
const [todos, setTodos] = useState([]);
const [category, setCategory] = useState(""); // js, react, ct
const [text, setText] = useState("");
const [editText, setEditText] = useState("");
const newTodo = {
// id: Date.now(),
text,
isDone: false,
isEdit: false,
category,
createdAt: Date.now(),
};
const addTodo = async () => {
// setTodos((prev) => [...prev, newTodo]);
await addDoc(collection(dbService, "todos"), newTodo);
setText("");
};
const deleteTodo = (id) => {
Alert.alert("Todo 삭제", "정말 삭제하시겠습니까?", [
{
text: "취소",
style: "cancel",
onPress: () => console.log("취소 클릭!"),
},
{
text: "삭제",
style: "destructive",
onPress: async () => {
// const newTodos = todos.filter((todo) => todo.id !== id);
// setTodos(newTodos);
await deleteDoc(doc(dbService, "todos", id));
},
},
]);
};
const setEdit = async (id) => {
const idx = todos.findIndex((todo) => todo.id === id);
await updateDoc(doc(dbService, "todos", id), {
isEdit: !todos[idx].isEdit,
});
};
const editTodo = async (id) => {
await updateDoc(doc(dbService, "todos", id), {
text: editText,
isEdit: false,
});
};
const setDone = async (id) => {
const idx = todos.findIndex((todo) => todo.id === id);
await updateDoc(doc(dbService, "todos", id), {
isDone: !todos[idx].isDone,
});
};
const setCat = async (cat) => {
setCategory(cat);
// await AsyncStorage.setItem("category", cat);
await updateDoc(doc(dbService, "category", "currentCategory"), {
category: cat,
});
};
useEffect(() => {
// 1. onSnapshot API를 이용해서 todos 콜렉션에 변경이 생길 때 마다
// 2. todos 콜렉션 안의 모든 document들을 불러와서 setTodos 한다.
const q = query(
collection(dbService, "todos"),
orderBy("createdAt", "desc")
);
onSnapshot(q, (snapshot) => {
const newTodos = snapshot.docs.map((doc) => {
const newTodo = {
id: doc.id,
...doc.data(), // doc.data() : { text, createdAt, ... }
};
return newTodo;
});
setTodos(newTodos);
});
const getCategory = async () => {
const snapshot = await getDoc(
doc(dbService, "category", "currentCategory")
);
setCategory(snapshot.data().category);
};
getCategory();
}, []);
return (
<SafeAreaView style={styles.safearea}>
<StatusBar style="auto" />
<View style={styles.container}>
<Tabs setCat={setCat} category={category} />
<View style={styles.inputWrapper}>
<TextInput
onSubmitEditing={addTodo}
onChangeText={setText}
value={text}
placeholder="Enter your task"
style={styles.input}
/>
</View>
<ScrollView>
{todos.map((todo) => {
if (category === todo.category) {
return (
<Todo
key={todo.id}
setEditText={setEditText}
editText={editText}
todo={todo}
editTodo={editTodo}
setDone={setDone}
setEdit={setEdit}
deleteTodo={deleteTodo}
/>
);
}
})}
</ScrollView>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safearea: {
flex: 1,
},
container: {
flex: 1,
paddingVertical: 30,
paddingHorizontal: 20,
},
tabs: {
flexDirection: "row",
justifyContent: "space-between",
},
tab: {
backgroundColor: "#0FBCF9",
paddingHorizontal: 10,
paddingVertical: 15,
width: "30%",
alignItems: "center",
},
tabText: {
fontWeight: "600",
},
inputWrapper: {
borderTopWidth: 1,
borderBottomWidth: 1,
paddingVertical: 15,
marginTop: 15,
marginBottom: 15,
},
input: {
borderWidth: 1,
paddingVertical: 10,
paddingHorizontal: 20,
},
task: {
flexDirection: "row",
paddingVertical: 10,
paddingHorizontal: 10,
backgroundColor: "#D9D9D9",
alignItems: "center",
justifyContent: "space-between",
marginBottom: 10,
},
});