Skip to content

Commit ce0cbba

Browse files
committed
feat(examples/to-do-app): add removeCategory functionality with confirmation alert
1 parent 07e9c9a commit ce0cbba

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

examples/to-do-app/hooks/useCategories.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ const useCategories = () => {
3030
}
3131
}
3232

33+
const removeCategory = async categoryName => {
34+
try {
35+
await db.execute('DELETE FROM tags WHERE name = ?', [categoryName])
36+
db.execute('SELECT cloudsync_network_send_changes();')
37+
setMoreCategories(prevCategories => prevCategories.filter(cat => cat !== categoryName))
38+
} catch (error) {
39+
console.error('Error removing category', error)
40+
}
41+
}
42+
3343
const initializeTables = async () => {
3444
let extensionPath;
3545

@@ -86,6 +96,7 @@ const useCategories = () => {
8696
return {
8797
moreCategories,
8898
addCategory,
99+
removeCategory,
89100
getCategories
90101
}
91102
}

examples/to-do-app/screens/Categories.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from "react";
2-
import { ScrollView, StyleSheet, View } from "react-native";
2+
import { ScrollView, StyleSheet, View, Alert } from "react-native";
33
import { Avatar, Card, Text, Modal, Portal, Button, TextInput } from "react-native-paper";
44
import { useFocusEffect } from '@react-navigation/native';
55
import useCategories from "../hooks/useCategories";
@@ -22,7 +22,7 @@ const Categories = ({ navigation }) => {
2222
day: "numeric",
2323
});
2424

25-
const { moreCategories, addCategory } = useCategories();
25+
const { moreCategories, addCategory, removeCategory } = useCategories();
2626
const { setSync } = useSyncContext();
2727

2828
const [newCategory, setNewCategory] = useState("");
@@ -45,6 +45,24 @@ const Categories = ({ navigation }) => {
4545
hideModal();
4646
}
4747

48+
function handleRemoveCategory(categoryName) {
49+
Alert.alert(
50+
"Delete Category",
51+
`Are you sure you want to delete "${categoryName}"?`,
52+
[
53+
{
54+
text: "Cancel",
55+
style: "cancel"
56+
},
57+
{
58+
text: "Delete",
59+
style: "destructive",
60+
onPress: () => removeCategory(categoryName)
61+
}
62+
]
63+
);
64+
}
65+
4866
return (
4967
<>
5068
<Portal>
@@ -127,6 +145,7 @@ const Categories = ({ navigation }) => {
127145
key={index}
128146
style={styles.card}
129147
onPress={() => navigation.navigate("Tasks", { category })}
148+
onLongPress={() => handleRemoveCategory(category)}
130149
mode="contained"
131150
>
132151
<Card.Title

0 commit comments

Comments
 (0)