/
new.tsx
64 lines (62 loc) · 1.9 KB
/
new.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
import { useRouter, useSearchParams } from "expo-router";
import React, { useState } from "react";
import { View } from "react-native";
import { IconButton } from "react-native-paper";
import { useAccount } from "../../../../components/AccountContext";
import { Screen } from "../../../../components/Screen";
import { TransactionForm } from "../../../../components/TransactionForm";
import { createTransaction } from "../../../../lib/account";
import { createEvent } from "../../../../lib/api";
export default function TransactionNew(): JSX.Element {
const params = useSearchParams();
const accountId = `${params.id}`;
const [account, _setAccount] = useAccount(accountId, []);
const [amount, setAmount] = useState<string>("");
const [categoryId, setCategoryId] = useState<string>("");
const [comment, setComment] = useState<string>("");
const [date, setDate] = useState<string>(
new Date().toISOString().substring(0, 10)
);
const router = useRouter();
const onClickOk = () => {
if (account === null) return;
const [_, event] = createTransaction(account, {
amount,
categoryId,
comment,
date,
});
createEvent(event).then((_) => {
router.back();
});
};
return (
<Screen
options={{
title: "Add Transaction",
headerRight: () => (
<IconButton
accessibilityLabel="Save"
icon="check"
onPress={onClickOk}
size={28}
/>
),
}}
>
<View style={{ flex: 1, width: "100%" }}>
<TransactionForm
amount={amount}
categories={account?.categories ?? []}
categoryId={categoryId}
comment={comment}
date={date}
onChangeAmount={setAmount}
onChangeCategoryId={setCategoryId}
onChangeComment={setComment}
onChangeDate={setDate}
/>
</View>
</Screen>
);
}