-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from SFTtech/milo/add-transaction-from-accoun…
…t-detail feat(web): add purchase for account from account detail page
- Loading branch information
Showing
5 changed files
with
109 additions
and
57 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
112 changes: 80 additions & 32 deletions
112
frontend/apps/web/src/components/accounts/AccountTransactionList.tsx
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 |
---|---|---|
@@ -1,60 +1,108 @@ | ||
import { selectClearingAccountsInvolvingAccounts, selectTransactionsInvolvingAccount } from "@abrechnung/redux"; | ||
import { | ||
createTransaction, | ||
selectClearingAccountsInvolvingAccounts, | ||
selectTransactionsInvolvingAccount, | ||
} from "@abrechnung/redux"; | ||
import { Add as AddIcon } from "@mui/icons-material"; | ||
import { Account, Transaction } from "@abrechnung/types"; | ||
import { Alert, List } from "@mui/material"; | ||
import { Alert, Box, IconButton, List, Tooltip, Typography } from "@mui/material"; | ||
import { DateTime } from "luxon"; | ||
import * as React from "react"; | ||
import { selectAccountSlice, selectTransactionSlice, useAppSelector } from "@/store"; | ||
import { selectAccountSlice, selectTransactionSlice, useAppDispatch, useAppSelector } from "@/store"; | ||
import { AccountClearingListEntry } from "./AccountClearingListEntry"; | ||
import { AccountTransactionListEntry } from "./AccountTransactionListEntry"; | ||
import { useTranslation } from "react-i18next"; | ||
import { toast } from "react-toastify"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { PurchaseIcon } from "../style/AbrechnungIcons"; | ||
|
||
type ArrayAccountsAndTransactions = Array<Transaction | Account>; | ||
|
||
interface Props { | ||
groupId: number; | ||
accountId: number; | ||
account: Account; | ||
} | ||
|
||
export const AccountTransactionList: React.FC<Props> = ({ groupId, accountId }) => { | ||
export const AccountTransactionList: React.FC<Props> = ({ groupId, account }) => { | ||
const { t } = useTranslation(); | ||
const dispatch = useAppDispatch(); | ||
const navigate = useNavigate(); | ||
const transactions = useAppSelector((state) => | ||
selectTransactionsInvolvingAccount({ state: selectTransactionSlice(state), groupId, accountId }) | ||
selectTransactionsInvolvingAccount({ state: selectTransactionSlice(state), groupId, accountId: account.id }) | ||
); | ||
const clearingAccounts = useAppSelector((state) => | ||
selectClearingAccountsInvolvingAccounts({ state: selectAccountSlice(state), groupId, accountId }) | ||
selectClearingAccountsInvolvingAccounts({ state: selectAccountSlice(state), groupId, accountId: account.id }) | ||
); | ||
|
||
const combinedList: ArrayAccountsAndTransactions = (transactions as ArrayAccountsAndTransactions) | ||
.concat(clearingAccounts) | ||
.sort((f1, f2) => DateTime.fromISO(f2.last_changed).toMillis() - DateTime.fromISO(f1.last_changed).toMillis()); | ||
|
||
const createTransactionForAccount = () => { | ||
dispatch( | ||
createTransaction({ | ||
groupId, | ||
type: "purchase", | ||
data: { | ||
debitor_shares: { | ||
[account.id]: 1, | ||
}, | ||
}, | ||
}) | ||
) | ||
.unwrap() | ||
.then(({ transaction }) => { | ||
navigate(`/groups/${groupId}/transactions/${transaction.id}?no-redirect=true`); | ||
}) | ||
.catch(() => toast.error("Creating a transaction failed")); | ||
}; | ||
|
||
return ( | ||
<List> | ||
{combinedList.length === 0 && <Alert severity="info">None so far.</Alert>} | ||
{combinedList.map((entry) => { | ||
if (entry.type === "clearing") { | ||
<> | ||
<Box sx={{ display: "grid", gridTemplateColumns: "auto min-content", justifyContent: "space-between" }}> | ||
<Typography variant="h6">{t("accounts.transactionsInvolving", "", { account })}</Typography> | ||
<Tooltip | ||
title={t("transactions.createPurchaseForAccount", "", { | ||
accountName: account.name, | ||
})} | ||
> | ||
<Box sx={{ display: "flex", flexDirection: "row", alignItems: "center" }}> | ||
<AddIcon color="primary" /> | ||
<IconButton color="primary" onClick={createTransactionForAccount}> | ||
<PurchaseIcon /> | ||
</IconButton> | ||
</Box> | ||
</Tooltip> | ||
</Box> | ||
<List> | ||
{combinedList.length === 0 && <Alert severity="info">None so far.</Alert>} | ||
{combinedList.map((entry) => { | ||
if (entry.type === "clearing") { | ||
return ( | ||
<AccountClearingListEntry | ||
key={`clearing-${entry.id}`} | ||
accountId={account.id} | ||
groupId={groupId} | ||
clearingAccountId={entry.id} | ||
/> | ||
); | ||
} | ||
if (entry.type === "personal") { | ||
return null; | ||
} | ||
|
||
// we need to case "entry" to Transaction as typescript cant deduce that it | ||
// has to be a transaction even though we handled all other "type" cases before | ||
return ( | ||
<AccountClearingListEntry | ||
key={`clearing-${entry.id}`} | ||
accountId={accountId} | ||
<AccountTransactionListEntry | ||
key={`transaction-${entry.id}`} | ||
accountId={account.id} | ||
groupId={groupId} | ||
clearingAccountId={entry.id} | ||
transactionId={entry.id} | ||
/> | ||
); | ||
} | ||
if (entry.type === "personal") { | ||
return null; | ||
} | ||
|
||
// we need to case "entry" to Transaction as typescript cant deduce that it | ||
// has to be a transaction even though we handled all other "type" cases before | ||
return ( | ||
<AccountTransactionListEntry | ||
key={`transaction-${entry.id}`} | ||
accountId={accountId} | ||
groupId={groupId} | ||
transactionId={entry.id} | ||
/> | ||
); | ||
})} | ||
</List> | ||
})} | ||
</List> | ||
</> | ||
); | ||
}; |
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