-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
100 lines (96 loc) · 2.87 KB
/
index.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
import { usePathname, useRouter, useSearchParams } from "expo-router";
import { useEffect, useState } from "react";
import { StyleSheet, View } from "react-native";
import { FAB } from "react-native-paper";
import { CategoryList } from "../../../../components/CategoryList";
import { DeleteCategoryDialog } from "../../../../components/DeleteCategoryDialog";
import { Screen } from "../../../../components/Screen";
import {
Account,
deleteCategory,
restoreAccount,
} from "../../../../lib/account";
import { createEvent, getEvents } from "../../../../lib/api";
export default function Categories(): JSX.Element {
const pathname = usePathname();
const params = useSearchParams();
const router = useRouter();
const accountId = `${params.id}`;
const [account, setAccount] = useState<Account | null>(null);
const [name, setName] = useState<string>("");
const [categoryId, setCategoryId] = useState<string | null>(null);
const [deleteDialogVisible, setDeleteDialogVisible] =
useState<boolean>(false);
useEffect(() => {
getEvents(accountId)
.then((events) => restoreAccount(events))
.then((account) => setAccount(account));
}, [pathname]);
return (
<Screen>
<CategoryList
data={account?.categories ?? []}
onLongPressCategory={(category) => {
setName(category.name);
setCategoryId(category.id);
setDeleteDialogVisible(true);
}}
onPressCategory={(category) => {
router.push({
pathname: "/accounts/[id]/categories/[categoryId]/edit",
params: {
id: accountId,
categoryId: category.id,
name: encodeURIComponent(category.name),
},
});
}}
/>
<FAB
icon="plus"
style={styles.fab}
onPress={() => {
router.push({
pathname: "/accounts/[id]/categories/new",
params: {
id: accountId,
},
});
}}
/>
<DeleteCategoryDialog
id={categoryId}
name={name}
onClickCancel={() => {
setName("");
setCategoryId(null);
setDeleteDialogVisible(false);
}}
onClickOk={() => {
if (account === null) return;
if (categoryId !== null) {
// update local state
const [newAccount, newEvent] = deleteCategory(account, categoryId);
// update remote state
setAccount(newAccount);
createEvent(newEvent).catch((_) => {
setAccount(account);
});
}
setName("");
setCategoryId(null);
setDeleteDialogVisible(false);
}}
visible={deleteDialogVisible}
/>
</Screen>
);
}
const styles = StyleSheet.create({
fab: {
bottom: 0,
margin: 16,
position: "absolute",
right: 0,
},
});