-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
132 lines (124 loc) · 2.94 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
import React, { useState, useEffect } from 'react'
import {
StyleSheet,
Text,
View,
KeyboardAvoidingView,
Platform,
TextInput,
TouchableOpacity,
Keyboard,
FlatList,
} from 'react-native'
import GrudgeItem from './components/GrudgeItem'
import { updateDataInStorage, getData } from './utils/AsyncStorageUtils'
export default function App() {
const [grudge, setGrudge] = useState('')
const [grudges, setGrudges] = useState([])
const handleAdd = () => {
// to hide keyboard
// Keyboard.dismiss()
setGrudges((prev) => [...prev, grudge])
setGrudge('')
}
const handleDelete = (index) => {
let grudgersCopy = [...grudges]
grudgersCopy.splice(index, 1)
setGrudges(grudgersCopy)
}
useEffect(() => {
const getDataFromStorage = async() => {
const _grudges = await getData()
setGrudges(_grudges)
}
getDataFromStorage()
},[])
useEffect(() => {
updateDataInStorage(grudges)
}, [grudges])
return (
<View style={styles.container}>
<View style={styles.headContainer}>
<Text style={styles.textHeading}>Today's Grudges</Text>
</View>
<View>
<FlatList
data={grudges}
keyExtractor={(item,idx) => idx.toString()}
renderItem={(data) => (
<GrudgeItem
item={data.item}
onPress={() => handleDelete(data.index)}
/>
)}
/>
</View>
{/* to make things go up when keyboard is open */}
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.writeTaskWrapper}
>
<TextInput
style={styles.textInputWrapper}
placeholder={'Write a Grudge...'}
value={grudge}
onChangeText={(text) => setGrudge(text)}
/>
<TouchableOpacity onPress={() => handleAdd()}>
<View style={styles.addWrapper}>
<Text style={styles.icon}>+</Text>
</View>
</TouchableOpacity>
</KeyboardAvoidingView>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#E8EAED',
},
headContainer: {
paddingTop: 40,
paddingHorizontal: 20,
},
textHeading: {
color: '#1A1A1A',
fontSize: 24,
fontWeight: 'bold',
},
writeTaskWrapper: {
position: 'absolute',
bottom: 60,
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'center',
width: '100%',
},
textInputWrapper: {
backgroundColor: '#FFFFFF',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 1,
},
shadowOpacity: 0.22,
shadowRadius: 2.22,
elevation: 3,
borderRadius: 60,
width: '70%',
paddingVertical: 15,
paddingHorizontal: 15,
},
addWrapper: {
width: 60,
height: 60,
backgroundColor: '#FFF',
borderRadius: 60,
justifyContent: 'center',
alignItems: 'center',
},
icon: {
fontSize: 30,
}
})