-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mobile): [Transaction] add update and delete transaction (#123)
Resolves #112
- Loading branch information
Showing
5 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { TransactionForm } from '@/components/transaction/transaction-form' | ||
import { deleteTransaction, updateTransaction } from '@/mutations/transaction' | ||
import { transactionQueries, useTransactionDetail } from '@/queries/transaction' | ||
import { walletQueries } from '@/queries/wallet' | ||
import { t } from '@lingui/macro' | ||
import { useLingui } from '@lingui/react' | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import * as Haptics from 'expo-haptics' | ||
import { useLocalSearchParams, useRouter } from 'expo-router' | ||
import { LoaderIcon } from 'lucide-react-native' | ||
import { Alert, View } from 'react-native' | ||
|
||
export default function EditRecordScreen() { | ||
const { i18n } = useLingui() | ||
const { transactionId } = useLocalSearchParams<{ transactionId: string }>() | ||
const { data: transaction } = useTransactionDetail(transactionId!) | ||
const router = useRouter() | ||
const queryClient = useQueryClient() | ||
const { mutateAsync } = useMutation({ | ||
mutationFn: updateTransaction, | ||
onError(error) { | ||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error) | ||
Alert.alert(error.message) | ||
}, | ||
onSuccess() { | ||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success) | ||
router.back() | ||
}, | ||
async onSettled() { | ||
await queryClient.invalidateQueries({ | ||
queryKey: transactionQueries.all, | ||
}) | ||
await queryClient.invalidateQueries({ | ||
queryKey: walletQueries.list._def, | ||
}) | ||
}, | ||
}) | ||
const { mutateAsync: mutateDelete } = useMutation({ | ||
mutationFn: deleteTransaction, | ||
onMutate() { | ||
router.back() | ||
}, | ||
onError(error) { | ||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error) | ||
Alert.alert(error.message) | ||
}, | ||
onSuccess() { | ||
Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success) | ||
}, | ||
async onSettled() { | ||
await queryClient.invalidateQueries({ | ||
queryKey: transactionQueries.all, | ||
}) | ||
await queryClient.invalidateQueries({ | ||
queryKey: walletQueries.list._def, | ||
}) | ||
}, | ||
throwOnError: true, | ||
}) | ||
|
||
function handleDelete() { | ||
Haptics.selectionAsync() | ||
Alert.alert( | ||
t( | ||
i18n, | ||
)`This will delete the transaction. Are you sure you want to continue?`, | ||
'', | ||
[ | ||
{ | ||
text: t(i18n)`Cancel`, | ||
style: 'cancel', | ||
}, | ||
{ | ||
text: t(i18n)`Delete`, | ||
style: 'destructive', | ||
onPress: () => mutateDelete(transactionId!), | ||
}, | ||
], | ||
) | ||
} | ||
|
||
if (!transaction) { | ||
return ( | ||
<View className="flex-1 items-center bg-muted justify-center"> | ||
<LoaderIcon className="size-7 animate-spin text-primary" /> | ||
</View> | ||
) | ||
} | ||
|
||
return ( | ||
<TransactionForm | ||
onSubmit={(values) => mutateAsync({ id: transaction.id, data: values })} | ||
onCancel={router.back} | ||
defaultValues={{ | ||
walletAccountId: transaction.walletAccountId, | ||
currency: transaction.currency, | ||
amount: Math.abs(transaction.amount), | ||
date: transaction.date, | ||
note: transaction.note ?? '', | ||
budgetId: transaction.budgetId ?? undefined, | ||
categoryId: transaction.categoryId ?? undefined, | ||
}} | ||
onDelete={handleDelete} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters