|
| 1 | +import { useAppDispatch, useAppSelector } from "@/store"; |
| 2 | +import { selectTransactionPositionById, wipPositionUpdated } from "@abrechnung/redux"; |
| 3 | +import { Account, FrontendSplitMode, Transaction, TransactionShare } from "@abrechnung/types"; |
| 4 | +import { |
| 5 | + AppBar, |
| 6 | + Button, |
| 7 | + Checkbox, |
| 8 | + Dialog, |
| 9 | + DialogContent, |
| 10 | + DialogTitle, |
| 11 | + FormControlLabel, |
| 12 | + InputAdornment, |
| 13 | + Slide, |
| 14 | + Stack, |
| 15 | + Toolbar, |
| 16 | + Typography, |
| 17 | +} from "@mui/material"; |
| 18 | +import * as React from "react"; |
| 19 | +import { useTranslation } from "react-i18next"; |
| 20 | +import { PositionValidationError } from "./types"; |
| 21 | +import { AccountSelect, ShareSelect, TextInput } from "@/components"; |
| 22 | +import { NumericInput } from "@abrechnung/components"; |
| 23 | +import { getCurrencySymbolForIdentifier } from "@abrechnung/core"; |
| 24 | +import { TransitionProps } from "@mui/material/transitions"; |
| 25 | +import { useState } from "react"; |
| 26 | + |
| 27 | +export type PositionEditDialogProps = { |
| 28 | + transaction: Transaction; |
| 29 | + open: boolean; |
| 30 | + onClose: () => void; |
| 31 | + positionId?: number; |
| 32 | + validationError?: PositionValidationError; |
| 33 | + shownAccountIds: number[]; |
| 34 | + updateShownAccountIds: (value: number[]) => void; |
| 35 | +}; |
| 36 | + |
| 37 | +const Transition = React.forwardRef(function Transition( |
| 38 | + props: TransitionProps & { |
| 39 | + children: React.ReactElement<unknown>; |
| 40 | + }, |
| 41 | + ref: React.Ref<unknown> |
| 42 | +) { |
| 43 | + return <Slide direction="up" ref={ref} {...props} />; |
| 44 | +}); |
| 45 | + |
| 46 | +export const PositionEditDialog: React.FC<PositionEditDialogProps> = ({ |
| 47 | + transaction, |
| 48 | + open, |
| 49 | + onClose, |
| 50 | + positionId, |
| 51 | + validationError, |
| 52 | + shownAccountIds, |
| 53 | + updateShownAccountIds, |
| 54 | +}) => { |
| 55 | + const { t } = useTranslation(); |
| 56 | + const dispatch = useAppDispatch(); |
| 57 | + const position = useAppSelector((state) => |
| 58 | + selectTransactionPositionById(state, transaction.group_id, transaction.id, positionId) |
| 59 | + ); |
| 60 | + |
| 61 | + const [showAddAccountModal, setShowAddAccountModal] = useState(false); |
| 62 | + |
| 63 | + const error = validationError !== undefined; |
| 64 | + |
| 65 | + const updatePositionUsage = (shares: TransactionShare) => { |
| 66 | + if (!position) { |
| 67 | + return; |
| 68 | + } |
| 69 | + dispatch( |
| 70 | + wipPositionUpdated({ |
| 71 | + groupId: transaction.group_id, |
| 72 | + transactionId: transaction.id, |
| 73 | + position: { ...position, usages: shares }, |
| 74 | + }) |
| 75 | + ); |
| 76 | + }; |
| 77 | + |
| 78 | + const updatePosition = (update: { name?: string; price?: number; communist_shares?: number }) => { |
| 79 | + if (!position) { |
| 80 | + return; |
| 81 | + } |
| 82 | + dispatch( |
| 83 | + wipPositionUpdated({ |
| 84 | + groupId: transaction.group_id, |
| 85 | + transactionId: transaction.id, |
| 86 | + position: { ...position, ...update }, |
| 87 | + }) |
| 88 | + ); |
| 89 | + }; |
| 90 | + |
| 91 | + const addShownAccount = (account: Account) => { |
| 92 | + setShowAddAccountModal(false); |
| 93 | + updateShownAccountIds(Array.from(new Set<number>([...shownAccountIds, account.id]))); |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <Dialog |
| 99 | + open={open} |
| 100 | + onClose={onClose} |
| 101 | + fullScreen={true} |
| 102 | + slots={{ |
| 103 | + transition: Transition, |
| 104 | + }} |
| 105 | + > |
| 106 | + <AppBar sx={{ position: "relative" }}> |
| 107 | + <Toolbar> |
| 108 | + <Typography sx={{ ml: 2, flex: 1 }} variant="h6" component="div"> |
| 109 | + {t("transactions.positions.editPosition")} |
| 110 | + </Typography> |
| 111 | + <Button autoFocus color="inherit" onClick={onClose}> |
| 112 | + {t("common.ok")} |
| 113 | + </Button> |
| 114 | + </Toolbar> |
| 115 | + </AppBar> |
| 116 | + <DialogContent> |
| 117 | + <Stack spacing={2}> |
| 118 | + <TextInput |
| 119 | + label={t("common.name")} |
| 120 | + value={position?.name} |
| 121 | + error={validationError && !!validationError.fieldErrors["name"]} |
| 122 | + helperText={validationError && validationError.fieldErrors["name"]} |
| 123 | + onChange={(value) => updatePosition({ name: value })} |
| 124 | + /> |
| 125 | + <NumericInput |
| 126 | + label={t("common.price")} |
| 127 | + value={position?.price} |
| 128 | + isCurrency={true} |
| 129 | + error={validationError && !!validationError.fieldErrors["price"]} |
| 130 | + helperText={validationError && validationError.fieldErrors["price"]} |
| 131 | + onChange={(value) => updatePosition({ price: value })} |
| 132 | + slotProps={{ |
| 133 | + input: { |
| 134 | + endAdornment: ( |
| 135 | + <InputAdornment position="end"> |
| 136 | + {getCurrencySymbolForIdentifier(transaction.currency_identifier)} |
| 137 | + </InputAdornment> |
| 138 | + ), |
| 139 | + }, |
| 140 | + }} |
| 141 | + /> |
| 142 | + <ShareSelect |
| 143 | + groupId={transaction.group_id} |
| 144 | + label={""} |
| 145 | + value={position?.usages ?? {}} |
| 146 | + error={validationError && !!validationError.fieldErrors["usages"]} |
| 147 | + helperText={validationError && validationError.fieldErrors["usages"]} |
| 148 | + onChange={updatePositionUsage} |
| 149 | + splitMode={"shares"} |
| 150 | + allowedSplitModes={["evenly", "shares"]} |
| 151 | + currencyIdentifier={transaction.currency_identifier} |
| 152 | + editable={transaction.is_wip} |
| 153 | + shouldDisplayAccount={(accountId) => shownAccountIds.includes(accountId)} |
| 154 | + hideShowEventsFilter |
| 155 | + communistShares={position?.communist_shares ?? 0} |
| 156 | + onChangeCommunistShares={(shares) => updatePosition({ communist_shares: shares })} |
| 157 | + /> |
| 158 | + <Button onClick={() => setShowAddAccountModal(true)}>Add account</Button> |
| 159 | + </Stack> |
| 160 | + </DialogContent> |
| 161 | + </Dialog> |
| 162 | + <Dialog open={showAddAccountModal} onClose={() => setShowAddAccountModal(false)} fullWidth> |
| 163 | + <DialogTitle>{t("transactions.positions.addAccount")}</DialogTitle> |
| 164 | + <DialogContent> |
| 165 | + <AccountSelect |
| 166 | + label={t("accounts.account")} |
| 167 | + groupId={transaction.group_id} |
| 168 | + exclude={shownAccountIds} |
| 169 | + onChange={addShownAccount} |
| 170 | + /> |
| 171 | + </DialogContent> |
| 172 | + </Dialog> |
| 173 | + </> |
| 174 | + ); |
| 175 | +}; |
0 commit comments