From cb00826f5173ed9404d9e9c4d255d7085e4f8af1 Mon Sep 17 00:00:00 2001
From: Joel Jeremy Marquez <joeljeremy.marquez@gmail.com>
Date: Mon, 4 Sep 2023 23:17:03 -0700
Subject: [PATCH] Fix mobile page "back" behaviors (#1648)

* Fix mobile page "back" behaviors

* Release notes
---
 .../accounts/MobileAccountDetails.js          |  2 +-
 .../transactions/MobileTransaction.js         | 38 ++++++++++---------
 upcoming-release-notes/1648.md                |  6 +++
 3 files changed, 28 insertions(+), 18 deletions(-)
 create mode 100644 upcoming-release-notes/1648.md

diff --git a/packages/desktop-client/src/components/accounts/MobileAccountDetails.js b/packages/desktop-client/src/components/accounts/MobileAccountDetails.js
index 0e300076e..611b87778 100644
--- a/packages/desktop-client/src/components/accounts/MobileAccountDetails.js
+++ b/packages/desktop-client/src/components/accounts/MobileAccountDetails.js
@@ -109,7 +109,7 @@ export default function AccountDetails({
           }}
         >
           <Link
-            to="/accounts"
+            to={-1}
             style={{
               color: theme.formLabelText,
               alignItems: 'center',
diff --git a/packages/desktop-client/src/components/transactions/MobileTransaction.js b/packages/desktop-client/src/components/transactions/MobileTransaction.js
index a5d0cb536..65aa9a3e2 100644
--- a/packages/desktop-client/src/components/transactions/MobileTransaction.js
+++ b/packages/desktop-client/src/components/transactions/MobileTransaction.js
@@ -226,6 +226,7 @@ class TransactionEditInner extends PureComponent {
 
     if (transactions.find(t => t.account == null)) {
       // Ignore transactions if any of them don't have an account
+      // TODO: Should we display validation error?
       return;
     }
 
@@ -243,7 +244,7 @@ class TransactionEditInner extends PureComponent {
     }
 
     this.props.onSave(transactions);
-    this.props.navigation(`/accounts/${account.id}`);
+    this.props.navigate(`/accounts/${account.id}`, { replace: true });
   };
 
   onSaveChild = childTransaction => {
@@ -293,16 +294,22 @@ class TransactionEditInner extends PureComponent {
     });
   };
 
+  onDelete = () => {
+    this.props.onDelete();
+
+    const { transactions } = this.state;
+    const [transaction, ..._childTransactions] = transactions;
+    const { account: accountId } = transaction;
+    if (accountId) {
+      this.props.navigate(`/accounts/${accountId}`, { replace: true });
+    } else {
+      this.props.navigate(-1);
+    }
+  };
+
   render() {
-    const {
-      adding,
-      categories,
-      accounts,
-      payees,
-      renderChildEdit,
-      navigation,
-      onDelete,
-    } = this.props;
+    const { adding, categories, accounts, payees, renderChildEdit, navigate } =
+      this.props;
     const { editingChild } = this.state;
     const transactions = this.serializeTransactions(
       this.state.transactions || [],
@@ -374,7 +381,7 @@ class TransactionEditInner extends PureComponent {
             }}
           >
             <Link
-              to={account ? `/accounts/${account.id}` : '/budget'}
+              to={-1}
               style={{
                 alignItems: 'center',
                 display: 'flex',
@@ -583,7 +590,7 @@ class TransactionEditInner extends PureComponent {
             {!adding && (
               <View style={{ alignItems: 'center' }}>
                 <Button
-                  onClick={() => onDelete()}
+                  onClick={() => this.onDelete()}
                   style={{
                     borderWidth: 0,
                     paddingVertical: 5,
@@ -662,7 +669,7 @@ class TransactionEditInner extends PureComponent {
               editingChild && transactions.find(t => t.id === editingChild),
             amountSign: forcedSign,
             getCategoryName: id => (id ? lookupName(categories, id) : null),
-            navigation: navigation,
+            navigate: navigate,
             onEdit: this.onEdit,
             onStartClose: this.onSaveChild,
           })}
@@ -804,9 +811,6 @@ function TransactionEditUnconnected(props) {
   };
 
   const onDelete = async () => {
-    // Eagerly go back
-    navigate(`/accounts/${accountId}`);
-
     if (adding) {
       // Adding a new transactions, this disables saving when the component unmounts
       deleted = true;
@@ -833,7 +837,7 @@ function TransactionEditUnconnected(props) {
         accounts={accounts}
         payees={payees}
         pushModal={props.pushModal}
-        navigation={navigate}
+        navigate={navigate}
         // TODO: ChildEdit is complicated and heavily relies on RN
         // renderChildEdit={props => <ChildEdit {...props} />}
         renderChildEdit={props => {}}
diff --git a/upcoming-release-notes/1648.md b/upcoming-release-notes/1648.md
new file mode 100644
index 000000000..37aafa5e6
--- /dev/null
+++ b/upcoming-release-notes/1648.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [joel-jeremy]
+---
+
+Fix mobile pages "back" behaviors.
-- 
GitLab