-
-
Notifications
You must be signed in to change notification settings - Fork 112
✨ Add multiple expenses via bank transactions at the same time #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexanderwassbjer
wants to merge
5
commits into
oss-apps:main
Choose a base branch
from
alexanderwassbjer:feature/multiple-transactions-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+582
−323
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e7fdbf4
Fix bug when adding expense
alexanderwassbjer ae91506
Add multiple transactions add with add one by one and add all
alexanderwassbjer e2be8b1
Fix add all bug
alexanderwassbjer 7d0d7f6
Some small improvements
alexanderwassbjer c36a1c2
Add check
alexanderwassbjer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,151 +1,140 @@ | ||
| import React, { useCallback } from 'react'; | ||
| import { useAddExpenseStore } from '~/store/addStore'; | ||
| import { calculateParticipantSplit, useAddExpenseStore } from '~/store/addStore'; | ||
| import type { TransactionAddInputModel } from '~/types'; | ||
| import { BankingTransactionList } from './BankTransactions/BankingTransactionList'; | ||
| import { isCurrencyCode } from '~/lib/currency'; | ||
| import { useRouter } from 'next/router'; | ||
| import { api } from '~/utils/api'; | ||
| import { type CreateExpense } from '~/types/expense.types'; | ||
|
|
||
| const AddBankTransactions: React.FC<{ | ||
| // clearFields: () => void; | ||
| clearFields: () => void; | ||
| bankConnectionEnabled: boolean; | ||
| children: React.ReactNode; | ||
| }> = ({ bankConnectionEnabled, children }) => { | ||
| // const participants = useAddExpenseStore((s) => s.participants); | ||
| // const group = useAddExpenseStore((s) => s.group); | ||
| // const category = useAddExpenseStore((s) => s.category); | ||
| // const isExpenseSettled = useAddExpenseStore((s) => s.canSplitScreenClosed); | ||
| // const splitShares = useAddExpenseStore((s) => s.splitShares); | ||
| // const paidBy = useAddExpenseStore((s) => s.paidBy); | ||
| // const splitType = useAddExpenseStore((s) => s.splitType); | ||
| // const fileKey = useAddExpenseStore((s) => s.fileKey); | ||
| // const multipleTransactions = useAddExpenseStore((s) => s.multipleTransactions); | ||
| // const isTransactionLoading = useAddExpenseStore((s) => s.isTransactionLoading); | ||
|
|
||
| const { | ||
| setCurrency, | ||
| setDescription, | ||
| // resetState, | ||
| // setSplitScreenOpen, | ||
| setExpenseDate, | ||
| setTransactionId, | ||
| setAmountStr, | ||
| // setMultipleTransactions, | ||
| // setIsTransactionLoading, | ||
| } = useAddExpenseStore((s) => s.actions); | ||
|
|
||
| // const addExpenseMutation = api.expense.addOrEditExpense.useMutation(); | ||
|
|
||
| // const router = useRouter(); | ||
|
|
||
| // const addMultipleExpenses = useCallback(async () => { | ||
| // setIsTransactionLoading(true); | ||
|
|
||
| // if (!paidBy) { | ||
| // return; | ||
| // } | ||
|
|
||
| // if (!isExpenseSettled) { | ||
| // setSplitScreenOpen(true); | ||
| // return; | ||
| // } | ||
|
|
||
| // const seen = new Set(); | ||
| // const deduplicated = multipleTransactions.filter((item) => { | ||
| // if (seen.has(item.transactionId)) { | ||
| // return false; | ||
| // } | ||
| // seen.add(item.transactionId); | ||
| // return true; | ||
| // }); | ||
|
|
||
| // const expensePromises = deduplicated.map(async (tempItem) => { | ||
| // if (tempItem) { | ||
| // const normalizedAmount = tempItem.amount.replace(',', '.'); | ||
| // const _amtBigInt = BigInt(Math.round(Number(normalizedAmount) * 100)); | ||
|
|
||
| // const { participants: tempParticipants } = calculateParticipantSplit( | ||
| // _amtBigInt, | ||
| // participants, | ||
| // splitType, | ||
| // splitShares, | ||
| // paidBy, | ||
| // ); | ||
|
|
||
| // return addExpenseMutation.mutateAsync({ | ||
| // name: tempItem.description, | ||
| // currency: tempItem.currency, | ||
| // amount: _amtBigInt, | ||
| // groupId: group?.id ?? null, | ||
| // splitType, | ||
| // participants: tempParticipants.map((p) => ({ | ||
| // userId: p.id, | ||
| // amount: p.amount ?? 0n, | ||
| // })), | ||
| // paidBy: paidBy.id, | ||
| // category, | ||
| // fileKey, | ||
| // expenseDate: tempItem.date, | ||
| // expenseId: tempItem.expenseId, | ||
| // transactionId: tempItem.transactionId, | ||
| // }); | ||
| // } | ||
| // return Promise.resolve(); | ||
| // }); | ||
|
|
||
| // await Promise.all(expensePromises); | ||
|
|
||
| // setMultipleTransactions([]); | ||
| // setIsTransactionLoading(false); | ||
| // router.back(); | ||
| // resetState(); | ||
| // }, [ | ||
| // setSplitScreenOpen, | ||
| // router, | ||
| // resetState, | ||
| // addExpenseMutation, | ||
| // group, | ||
| // paidBy, | ||
| // splitType, | ||
| // fileKey, | ||
| // isExpenseSettled, | ||
| // multipleTransactions, | ||
| // participants, | ||
| // category, | ||
| // setIsTransactionLoading, | ||
| // splitShares, | ||
| // setMultipleTransactions, | ||
| // ]); | ||
|
|
||
| const addViaBankTransaction = useCallback( | ||
| (obj: TransactionAddInputModel) => { | ||
| setExpenseDate(obj.date); | ||
| setDescription(obj.description); | ||
| if (isCurrencyCode(obj.currency)) { | ||
| setCurrency(obj.currency); | ||
| } else { | ||
| console.warn(`Invalid currency code: ${obj.currency}`); | ||
| } | ||
| setAmountStr(obj.amount); | ||
| setTransactionId(obj.transactionId); | ||
| addViaBankTransaction: (obj: TransactionAddInputModel) => void; | ||
| }> = ({ bankConnectionEnabled, children, clearFields, addViaBankTransaction }) => { | ||
| const participants = useAddExpenseStore((s) => s.participants); | ||
| const group = useAddExpenseStore((s) => s.group); | ||
| const category = useAddExpenseStore((s) => s.category); | ||
| const isExpenseSettled = useAddExpenseStore((s) => s.canSplitScreenClosed); | ||
| const splitShares = useAddExpenseStore((s) => s.splitShares); | ||
| const paidBy = useAddExpenseStore((s) => s.paidBy); | ||
| const splitType = useAddExpenseStore((s) => s.splitType); | ||
| const fileKey = useAddExpenseStore((s) => s.fileKey); | ||
| const multipleTransactions = useAddExpenseStore((s) => s.multipleTransactions); | ||
| const isTransactionLoading = useAddExpenseStore((s) => s.isTransactionLoading); | ||
|
|
||
| const { resetState, setSplitScreenOpen, setMultipleTransactions, setIsTransactionLoading } = | ||
| useAddExpenseStore((s) => s.actions); | ||
|
|
||
| const addExpenseMutation = api.expense.addOrEditExpense.useMutation(); | ||
|
|
||
| const router = useRouter(); | ||
|
|
||
| const addAllMultipleExpenses = useCallback(async () => { | ||
| setIsTransactionLoading(true); | ||
|
|
||
| if (!paidBy) { | ||
| return; | ||
| } | ||
|
|
||
| if (!isExpenseSettled) { | ||
| setSplitScreenOpen(true); | ||
| return; | ||
| } | ||
|
|
||
| const seen = new Set(); | ||
|
|
||
| const expenses = multipleTransactions | ||
| .filter((item) => { | ||
| if (seen.has(item.transactionId)) { | ||
| return false; | ||
| } | ||
| seen.add(item.transactionId); | ||
| return true; | ||
| }) | ||
| .map((tempItem) => { | ||
| const { participants: tempParticipants } = calculateParticipantSplit( | ||
| tempItem.amount, | ||
| participants, | ||
| splitType, | ||
| splitShares, | ||
| paidBy, | ||
| ); | ||
|
|
||
| return { | ||
| name: tempItem.description, | ||
| currency: tempItem.currency, | ||
| amount: tempItem.amount, | ||
| groupId: group?.id ?? null, | ||
| splitType, | ||
| participants: tempParticipants.map((p) => ({ | ||
| userId: p.id, | ||
| amount: p.amount ?? 0n, | ||
| })), | ||
| paidBy: paidBy.id, | ||
| category, | ||
| fileKey, | ||
| expenseDate: tempItem.date, | ||
| expenseId: tempItem.expenseId, | ||
| transactionId: tempItem.transactionId, | ||
| }; | ||
| }) as CreateExpense[]; | ||
|
|
||
| await addExpenseMutation.mutateAsync(expenses, { | ||
| onSuccess: () => { | ||
| setMultipleTransactions([]); | ||
| setIsTransactionLoading(false); | ||
| router.back(); | ||
| resetState(); | ||
| }, | ||
| onError: () => { | ||
| setIsTransactionLoading(false); | ||
| }, | ||
| }); | ||
| }, [ | ||
| setSplitScreenOpen, | ||
| router, | ||
| resetState, | ||
| addExpenseMutation, | ||
| group, | ||
| paidBy, | ||
| splitType, | ||
| fileKey, | ||
| isExpenseSettled, | ||
| multipleTransactions, | ||
| participants, | ||
| category, | ||
| setIsTransactionLoading, | ||
| splitShares, | ||
| setMultipleTransactions, | ||
| ]); | ||
|
|
||
| const addOneByOneMultipleExpenses = useCallback(async () => { | ||
| const allTransactions = [...multipleTransactions]; | ||
| const transactionToAdd = allTransactions.pop(); | ||
| if (transactionToAdd) { | ||
| setMultipleTransactions(allTransactions); | ||
| addViaBankTransaction(transactionToAdd); | ||
| } | ||
| }, [multipleTransactions, setMultipleTransactions, addViaBankTransaction]); | ||
|
|
||
| const handleSetMultipleTransactions = useCallback( | ||
| (a: TransactionAddInputModel[]) => { | ||
| setMultipleTransactions(a); | ||
| }, | ||
| [setExpenseDate, setDescription, setCurrency, setAmountStr, setTransactionId], | ||
| [setMultipleTransactions], | ||
| ); | ||
|
|
||
| // const handleSetMultipleTransactions = useCallback( | ||
| // (a: TransactionAddInputModel[]) => { | ||
| // setMultipleTransactions(a); | ||
| // }, | ||
| // [setMultipleTransactions], | ||
| // ); | ||
|
|
||
| return ( | ||
| <BankingTransactionList | ||
| add={addViaBankTransaction} | ||
| // addMultipleExpenses={addMultipleExpenses} | ||
| // multipleTransactions={multipleTransactions} | ||
| // setMultipleTransactions={handleSetMultipleTransactions} | ||
| // isTransactionLoading={isTransactionLoading} | ||
| addAllMultipleExpenses={addAllMultipleExpenses} | ||
| addOneByOneMultipleExpenses={addOneByOneMultipleExpenses} | ||
| multipleTransactions={multipleTransactions} | ||
| setMultipleTransactions={handleSetMultipleTransactions} | ||
| isTransactionLoading={isTransactionLoading} | ||
| bankConnectionEnabled={bankConnectionEnabled} | ||
| // clearFields={clearFields} | ||
| clearFields={clearFields} | ||
| > | ||
| {children} | ||
| </BankingTransactionList> | ||
|
Comment on lines
128
to
140
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really get the point of this component. It's just prop drilling with no UI added? Why not merge it with |
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh? Any reasoning behind this?