-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
166 lines (158 loc) · 3.94 KB
/
App.tsx
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
import React, { useEffect, useState } from 'react';
import type {PropsWithChildren} from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
TouchableOpacity,
TextInput,
Alert
} from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { theme } from './color';
type SectionProps = PropsWithChildren<{
title: string;
}>;
const STORAGE_KEY = "@toDos"
interface IAsync {
[key: string]: string | null;
}
function App(): React.JSX.Element {
const [working, setWorking] = useState(true);
const travel = () => setWorking(false);
const work = () => setWorking(true);
const [text, setText] = useState("");
const [todos, setTodos] = useState<any>({});
const onChangeText = (payload: string) => setText(payload);
const saveTodos = async (toSave:object) => {
const s = JSON.stringify(toSave)
await AsyncStorage.setItem(STORAGE_KEY, s)
};
const addTodo = async () => {
if (text === ""){
return
}
const newToDos = {
...todos,
[Date.now()]: { text, working },
};
setTodos(newToDos);
await saveTodos(newToDos);
setText("");
}
const loadTodo = async () => {
try {
const s = await AsyncStorage.getItem(STORAGE_KEY);
if (s !== null) {
const parsedData: IAsync = JSON.parse(s);
setTodos(parsedData);
}
} catch (error) {
console.error('Error loading todos:', error);
}
}
useEffect(() => {
loadTodo()
}, [])
const deleteTodo = async (key: string) => {
Alert.alert(
"정말 삭제하시겠습니까?",
"",
[
{
text: "취소",
style: "cancel"
},
{
text: "확인",
style:"destructive",
onPress: async () => {
const newTodos = {...todos}
delete newTodos[key]
setTodos(newTodos);
await saveTodos(newTodos);
}
}
],
{ cancelable: true }
);
};
return (
<View style={styles.container}>
<View style={styles.header}>
<TouchableOpacity onPress={work}>
<Text style={{...styles.btnText, color : working ? "white": theme.grey}}>Work</Text>
</TouchableOpacity>
<TouchableOpacity onPress={travel}>
<Text style={{...styles.btnText, color : working ? theme.grey: "white"}}>Travel</Text>
</TouchableOpacity>
</View>
<View>
<TextInput
onSubmitEditing={addTodo}
placeholder={working ? "Add a To do" : "Where do you want to go?"}
onChangeText={onChangeText}
returnKeyType="done"
clearTextOnFocus
style={styles.input} />
</View>
<ScrollView showsVerticalScrollIndicator={false}>
{Object.keys(todos).map((key) => (
todos[key].working === working ?
<View style={styles.todo} key={key as string}>
<Text style={styles.todoText}>{todos[key].text}</Text>
<TouchableOpacity onPress={() => deleteTodo(key)}>
<Text>❌</Text>
</TouchableOpacity>
</View>
: null
))}
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container:{
flex: 1,
backgroundColor: theme.background,
paddingHorizontal: 25
},
header:{
justifyContent: "space-between",
flexDirection:"row",
marginTop:100,
},
btnText:{
fontSize:40,
color: "white",
fontWeight:"600",
},
input:{
backgroundColor:"white",
paddingVertical: 15,
paddingHorizontal: 20,
borderRadius: 20,
marginTop: 15,
fontSize: 15,
marginBottom:25,
},
todo:{
backgroundColor: theme.todoBg,
marginVertical:6,
padding:25,
borderRadius: 17,
flexDirection: "row",
alignItems:"center",
justifyContent:"space-between",
},
todoText:{
color: "white",
fontWeight: "500",
fontSize: 15,
}
});
export default App;