-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
123 lines (111 loc) · 3.45 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
import { View, TextInput, Text, TouchableOpacity, ScrollView, StyleSheet, Alert } from 'react-native'
import React, { useEffect, useState, useRef } from 'react'
import colorStyle from './src/styles/colorStyle';
import { database, createTable } from './src/config/database';
import Lottie from 'lottie-react-native';
const App = () => {
let styles = StyleSheet.create(colorStyle);
const [task, setTask] = useState("");
const [status] = useState(0);
const [tugas, setTugas] = useState([]);
const addTask = () => {
if (!task) {
alert('Masukan tugas!');
return false
};
database.transaction(txn => {
txn.executeSql(`INSERT INTO tugas (nama, status) VALUES (?,?)`, [task, status],
(sqlTxn, res) => {getTasks()}
);
});
}
const getTasks = () => {
database.transaction(txn => {
txn.executeSql(
`SELECT * FROM tugas ORDER BY id DESC`,
[],
(sqlTxn, res) => {
let len = res.rows.length;
if (len > 0) {
let result = [];
for (let i = 0; i < len; i++) {
let item = res.rows.item(i);
result.push({ id: item.id, nama: item.nama, status: item.status });
};
setTugas(result);
setTask('');
} else if (len == 0) {
setTugas([]);
};
}
);
});
}
const handleUpdate = (e) => {
database.transaction(txn => {
txn.executeSql(`UPDATE tugas SET status = 1 WHERE id = ${e.id};`,[],() => {getTasks()});
});
}
const handleDelete = (e) => {
database.transaction(txn => {
txn.executeSql(
`DELETE FROM tugas WHERE id = ${e.id}`, []
, () => {
getTasks();
},
);
});
}
useEffect(() => {
createTable();
getTasks();
}, []);
const listTugas = tugas.map((result, index) => {
return (
<View key={result.id}>
<TouchableOpacity
onPress={
() => Alert.alert(
'Tugas ini gimana?',
'Ubah statusnya boleh hapus atau sudah.',
[
{
text: 'Batal'
},
{
text: 'Hapus',
onPress: () => handleDelete(result)
},
{
text: 'Sudah',
onPress: () => handleUpdate(result)
}
]
)} style={styles.resultBar}>
<Text style={{ textAlign: 'center', textDecorationLine: result.status === 0 ? '' : 'line-through' }}>{result.nama}</Text>
</TouchableOpacity>
</View>
);
});
return (
<ScrollView style={styles.container}>
<View style={styles.animaCont}>
<Lottie source={require('./src/animation/typing-animation.json')} autoPlay loop />
</View>
<View>
<Text style={styles.title}>To Do List</Text>
<Text style={styles.subTitle}>Tulis apa saja kegiatanmu hari ini. Supaya tidak lupa!</Text>
</View>
<View style={{ paddingTop: 20, alignItems: 'center' }}>
<TextInput onChangeText={setTask} value={task} placeholder='Masukan tugas kamu...' style={styles.inputBar} />
<TouchableOpacity onPress={addTask} style={{ backgroundColor: '#4299E1', marginTop: 20, paddingHorizontal: 25, paddingVertical: 10, borderRadius: 10 }}>
<Text style={{ color: '#FFFFFF' }}>Tambah</Text>
</TouchableOpacity>
<View style={styles.listContainer}>
{listTugas}
</View>
</View>
</ScrollView>
)
}
export default App