From 0ff6bd339ef6e2ce4175e396fef17cf569ebf8eb Mon Sep 17 00:00:00 2001 From: bouzuya Date: Fri, 31 Mar 2023 17:41:11 +0900 Subject: [PATCH] Add category name to transaction list --- app/accounts/[id]/transactions/index.tsx | 91 ++++++++---------------- components/TransactionList.tsx | 81 +++++++++++++++++++++ 2 files changed, 112 insertions(+), 60 deletions(-) create mode 100644 components/TransactionList.tsx diff --git a/app/accounts/[id]/transactions/index.tsx b/app/accounts/[id]/transactions/index.tsx index ca87921..8911163 100644 --- a/app/accounts/[id]/transactions/index.tsx +++ b/app/accounts/[id]/transactions/index.tsx @@ -1,11 +1,17 @@ import { usePathname, useRouter, useSearchParams } from "expo-router"; import { useState } from "react"; -import { FlatList, StyleSheet, View } from "react-native"; -import { FAB, List, Text } from "react-native-paper"; +import { StyleSheet } from "react-native"; +import { FAB, Text } from "react-native-paper"; import { useAccount } from "../../../../components/AccountContext"; import { DeleteTransactionDialog } from "../../../../components/DeleteTransactionDialog"; import { Screen } from "../../../../components/Screen"; -import { deleteTransaction, getLastEventId } from "../../../../lib/account"; +import { TransactionList } from "../../../../components/TransactionList"; +import { + deleteTransaction, + getLastEventId, + listCategory, + Transaction, +} from "../../../../lib/account"; import { storeEvent } from "../../../../lib/api"; export default function Transactions(): JSX.Element { @@ -30,64 +36,29 @@ export default function Transactions(): JSX.Element { Register a new transaction ) ) : ( - { - const ensureDescription = (s: string): string => - s.length === 0 ? " " : s; - return ( - ( - - {transaction.date} - - {transaction.amount} - - - )} - onLongPress={() => { - setDate(transaction.date); - setAmount(transaction.amount); - setComment(transaction.comment); - setTransactionId(transaction.id); - setDeleteModalVisible(true); - }} - onPress={() => { - router.push({ - pathname: - "/accounts/[id]/transactions/[transactionId]/edit", - params: { - id: accountId, - categoryId: transaction.categoryId, - transactionId: transaction.id, - date: transaction.date, - amount: transaction.amount, - comment: encodeURIComponent(transaction.comment), - }, - }); - }} - title="" - /> - ); + { + setDate(transaction.date); + setAmount(transaction.amount); + setComment(transaction.comment); + setTransactionId(transaction.id); + setDeleteModalVisible(true); + }} + onPressTransaction={(transaction: Transaction) => { + router.push({ + pathname: "/accounts/[id]/transactions/[transactionId]/edit", + params: { + id: accountId, + categoryId: transaction.categoryId, + transactionId: transaction.id, + date: transaction.date, + amount: transaction.amount, + comment: encodeURIComponent(transaction.comment), + }, + }); }} - style={{ flex: 1, width: "100%" }} /> )} {(account?.categories ?? []).length === 0 ? null : ( diff --git a/components/TransactionList.tsx b/components/TransactionList.tsx new file mode 100644 index 0000000..caaaf61 --- /dev/null +++ b/components/TransactionList.tsx @@ -0,0 +1,81 @@ +import { FlatList, FlatListProps, View } from "react-native"; +import { List, Text } from "react-native-paper"; +import { Category, Transaction } from "../lib/account"; + +type Props = Omit< + FlatListProps, + "data" | "keyExtractor" | "renderItem" | "style" +> & { + categories: Category[]; + onLongPressTransaction: (item: Transaction) => void; + onPressTransaction: (item: Transaction) => void; + transactions: Transaction[]; +}; + +export function TransactionList({ + categories, + onLongPressTransaction, + onPressTransaction, + transactions, + ...props +}: Props): JSX.Element { + const categoryNames = Object.fromEntries( + categories.map(({ id, name, deletedAt }) => [ + id, + name + (deletedAt === null ? "" : "(deleted)"), + ]) + ); + return ( + item.id} + renderItem={({ item: transaction }) => { + const ensureDescription = (s: string): string => + s.length === 0 ? " " : s; + return ( + ( + + {transaction.date} + + {categoryNames[transaction.categoryId]} + + + {transaction.amount} + + + )} + onLongPress={() => onLongPressTransaction(transaction)} + onPress={() => onPressTransaction(transaction)} + title="" + /> + ); + }} + style={{ flex: 1, width: "100%" }} + /> + ); +}