Skip to content
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

[Maintenance] Update TransactionEdit conponent onEdit function to use serialized transactions #2555

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,10 @@ const TransactionEditInner = memo(function TransactionEditInner({
};

const onSave = async () => {
const [transaction] = unserializedTransactions;
const [unserializedTransaction] = unserializedTransactions;

const onConfirmSave = async () => {
const { account: accountId } = transaction;
const { account: accountId } = unserializedTransaction;
const account = accountsById[accountId];

if (unserializedTransactions.find(t => t.account == null)) {
Expand All @@ -516,7 +516,7 @@ const TransactionEditInner = memo(function TransactionEditInner({
navigate(`/accounts/${account.id}`, { replace: true });
};

if (transaction.reconciled) {
if (unserializedTransaction.reconciled) {
// On mobile any save gives the warning.
// On the web only certain changes trigger a warning.
// Should we bring that here as well? Or does the nature of the editing form
Expand All @@ -536,31 +536,22 @@ const TransactionEditInner = memo(function TransactionEditInner({
onSave();
};

const onEdit = async (transaction, name, value) => {
const newTransaction = { ...transaction, [name]: value };
const onEdit = async (serializedTransaction, name, value) => {
const newTransaction = { ...serializedTransaction, [name]: value };
await props.onEdit(newTransaction);
onClearActiveEdit();
};

const onClick = (transactionId, name) => {
onRequestActiveEdit?.(getFieldName(transaction.id, name), () => {
const transaction = unserializedTransactions.find(
t => t.id === transactionId,
);
const transactionToEdit = transactions.find(t => t.id === transactionId);
switch (name) {
case 'category':
dispatch(
pushModal('category-autocomplete', {
categoryGroups,
onSelect: categoryId => {
// This is a deficiency of this API, need to fix. It
// assumes that it receives a serialized transaction,
// but we only have access to the raw transaction
onEdit(
serializeTransaction(transaction, dateFormat),
name,
categoryId,
);
onEdit(transactionToEdit, name, categoryId);
},
onClose: () => {
onClearActiveEdit();
Expand All @@ -572,14 +563,7 @@ const TransactionEditInner = memo(function TransactionEditInner({
dispatch(
pushModal('account-autocomplete', {
onSelect: accountId => {
// This is a deficiency of this API, need to fix. It
// assumes that it receives a serialized transaction,
// but we only have access to the raw transaction
onEdit(
serializeTransaction(transaction, dateFormat),
name,
accountId,
);
onEdit(transactionToEdit, name, accountId);
},
onClose: () => {
onClearActiveEdit();
Expand All @@ -591,14 +575,7 @@ const TransactionEditInner = memo(function TransactionEditInner({
dispatch(
pushModal('payee-autocomplete', {
onSelect: payeeId => {
// This is a deficiency of this API, need to fix. It
// assumes that it receives a serialized transaction,
// but we only have access to the raw transaction
onEdit(
serializeTransaction(transaction, dateFormat),
name,
payeeId,
);
onEdit(transactionToEdit, name, payeeId);
},
onClose: () => {
onClearActiveEdit();
Expand All @@ -611,14 +588,7 @@ const TransactionEditInner = memo(function TransactionEditInner({
pushModal('edit-field', {
name,
onSubmit: (name, value) => {
// This is a deficiency of this API, need to fix. It
// assumes that it receives a serialized transaction,
// but we only have access to the raw transaction
onEdit(
serializeTransaction(transaction, dateFormat),
name,
value,
);
onEdit(transactionToEdit, name, value);
},
onClose: () => {
onClearActiveEdit();
Expand All @@ -631,26 +601,26 @@ const TransactionEditInner = memo(function TransactionEditInner({
};

const onDelete = id => {
const [transaction, ..._childTransactions] = unserializedTransactions;
const [unserializedTransaction] = unserializedTransactions;

const onConfirmDelete = () => {
props.onDelete(id);

if (transaction.id !== id) {
if (unserializedTransaction.id !== id) {
// Only a child transaction was deleted.
onClearActiveEdit();
return;
}

const { account: accountId } = transaction;
const { account: accountId } = unserializedTransaction;
if (accountId) {
navigate(`/accounts/${accountId}`, { replace: true });
} else {
navigate(-1);
}
};

if (transaction.reconciled) {
if (unserializedTransaction.reconciled) {
dispatch(
pushModal('confirm-transaction-edit', {
onConfirm: onConfirmDelete,
Expand Down Expand Up @@ -683,9 +653,11 @@ const TransactionEditInner = memo(function TransactionEditInner({
};

useEffect(() => {
const noAmountTransaction = childTransactions.find(t => t.amount === 0);
if (noAmountTransaction) {
scrollChildTransactionIntoView(noAmountTransaction.id);
const noAmountChildTransaction = childTransactions.find(
t => t.amount === 0,
);
if (noAmountChildTransaction) {
scrollChildTransactionIntoView(noAmountChildTransaction.id);
}
}, [childTransactions]);

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2555.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Update TransactionEdit component onEdit function to use serialized transactions.
Loading