Expensify / App

Welcome to New Expensify: a complete re-imagination of financial collaboration, centered around chat. Help us build the next generation of Expensify by sharing feedback and contributing to the code.
https://new.expensify.com
MIT License
3.58k stars 2.92k forks source link

[$250] Distance- Waypoint is saved when distance editor RHP is refreshed and dismissed without saving #52413

Open lanitochka17 opened 2 weeks ago

lanitochka17 commented 2 weeks ago

If you haven’t already, check out our contributing guidelines for onboarding and email contributors@expensify.com to request to join our Slack channel!


Version Number: 9.0.60-0 Reproducible in staging?: Y Reproducible in production?: Y If this was caught on HybridApp, is this reproducible on New Expensify Standalone?: N/A If this was caught during regression testing, add the test name, ID and link from TestRail: N/A Email or phone of affected tester (no customers): applausetester+kh011101s2@applause.expensifail.com Issue reported by: Applause - Internal Team

Action Performed:

  1. Go to staging.new.expensify.com
  2. Go to workspace chat
  3. Submit a distance expense
  4. Go to transaction thread
  5. Click Distance
  6. Add another waypoint
  7. Refresh the page while distance editor RHP is open (close and reopen the app on Android and iOS)
  8. Click outside the RHP to dismiss it
  9. Click Distance

Expected Result:

The waypoint added in Step 6 will not appear because the new distance is not saved

Actual Result:

The waypoint added in Step 6 appears even when the new distance is not saved

Workaround:

Unknown

Platforms:

Which of our officially supported platforms is this issue occurring on?

Screenshots/Videos

Add any screenshot/video evidence

https://github.com/user-attachments/assets/e05e6cae-1ada-41ef-be8f-78724d3ac55d

View all open jobs on GitHub

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~021857103747653162057
  • Upwork Job ID: 1857103747653162057
  • Last Price Increase: 2024-11-21
Issue OwnerCurrent Issue Owner: @rayane-djouah
melvin-bot[bot] commented 2 weeks ago

Triggered auto assignment to @joekaufmanexpensify (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details. Please add this bug to a GH project, as outlined in the SO.

FitseTLT commented 2 weeks ago

Edited by proposal-police: This proposal was edited at 2024-11-21 19:03:45 UTC.

Proposal

Please re-state the problem that we are trying to solve in this issue.

Distance- Waypoint is saved when distance editor RHP is refreshed and dismissed without saving

What is the root cause of that problem?

We already have a code that is run on unmount to restore transaction here https://github.com/Expensify/App/blob/8a83e2baa1c7ce7ee2c31103574937c0484854be/src/pages/iou/request/step/IOURequestStepDistance.tsx#L230 But it is never called for the case of a reloading the page so when it is opened after refresh we re-load the changed transaction to the backup so if we dismiss it will have the new waypoints https://github.com/Expensify/App/blob/8a83e2baa1c7ce7ee2c31103574937c0484854be/src/pages/iou/request/step/IOURequestStepDistance.tsx#L221

What changes do you think we should make in order to solve the problem?

We should update to only load backup transaction if the the transactionBackup is empty we can use Onyx.connect and run the code inside the callback in createBackupTransaction change this https://github.com/Expensify/App/blob/e9e18573960d492a29b2563237024f0dc3c37f85/src/libs/actions/TransactionEdit.ts#L25

 const conn = Onyx.connect({
        key: `${ONYXKEYS.COLLECTION.TRANSACTION_BACKUP}${transaction.transactionID}`,
        callback: (transactionBackup) => {
            Onyx.disconnect(conn);
            if (transactionBackup) {
                return;
            }
            // Use set so that it will always fully overwrite any backup transaction that could have existed before
            Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_BACKUP}${transaction.transactionID}`, newTransaction);
        },
    });

Note that this will fix the bug not only on page refresh but also for app closing and reopening for native 👍

Or optionally

we need the setTimeout b/c by the time the effect is run useOnyx will not populate transactionBackup but

 setTimeout(() => {
            if (!transactionBackup) {
                // On mount, create the backup transaction.
                TransactionEdit.createBackupTransaction(transaction);
            }
        }, 0);
huult commented 2 weeks ago

Edited by proposal-police: This proposal was edited at 2024-11-13 06:06:56 UTC.

Proposal

Please re-state the problem that we are trying to solve in this issue.

Waypoint is saved when distance editor RHP is refreshed and dismissed without saving

What is the root cause of that problem?

When the page is reloaded, we can't execute this code to restore the original transaction from the backup. https://github.com/Expensify/App/blob/3897516558d81945d20f6d0910b6f3070cd60533/src/pages/iou/request/step/IOURequestStepDistance.tsx#L222-L236

After the reload is complete, create a backup transaction with data that has not been restored to the original transaction. This causes the issue where we still see the waypoint edited but not saved https://github.com/Expensify/App/blob/3897516558d81945d20f6d0910b6f3070cd60533/src/pages/iou/request/step/IOURequestStepDistance.tsx#L214-L220

What changes do you think we should make in order to solve the problem?

To resolve this issue, we need to check if the page is reloaded, then we don't need to create a new backup with the edited data. By the following steps:

1 Create a new Onyx to indicate whether the page is reloaded or not

// src/ONYXKEYS.ts#L459
+      EDIT_DISTANCE_PAGE_RELOAD: 'editDistancePageReload_',

2 Listen for the event before the reload

// src/pages/iou/request/step/IOURequestStepDistance.tsx#L214
+    useEffect(() => {
+        window.addEventListener('beforeunload', () => {
+            Onyx.set(`${ONYXKEYS.EDIT_DISTANCE_PAGE_RELOAD}${transactionID}`, true);
+        });

+        return () => {
+            window.removeEventListener('beforeunload', () => {
+                Onyx.set(`${ONYXKEYS.EDIT_DISTANCE_PAGE_RELOAD}${transactionID}`, false);
+            });

+            Onyx.set(`${ONYXKEYS.EDIT_DISTANCE_PAGE_RELOAD}${transactionID}`, false);
+        };
+    }, [transactionID]);

3 Add loading to wait value of edit distance reload

// src/pages/iou/request/step/IOURequestStepDistance.tsx#L75
+    const [isEditDistancePageRefreshing, isEditDistancePageResult] = useOnyx(`${ONYXKEYS.EDIT_DISTANCE_PAGE_RELOAD}${transactionID}`);
// src/pages/iou/request/step/IOURequestStepDistance.tsx#L530

+    if (isLoadingOnyxValue(isEditDistancePageResult)) {
+        return <FullScreenLoadingIndicator />;
+    }

4 Add condition to check create new back up or not.

// src/pages/iou/request/step/IOURequestStepDistance.tsx#L215
 useEffect(() => {
        if (isCreatingNewRequest) {
            return () => {};
        }

        // On mount, create the backup transaction.
-        TransactionEdit.createBackupTransaction(transaction);
+        InteractionManager.runAfterInteractions(() => {
+            requestAnimationFrame(() => {
+                // On mount, create the backup transaction.
+                if (isTransactionPageRefreshing === true || isTransactionPageRefreshing === undefined) {
+                    if (transactionWasSaved.current) {
+                        TransactionEdit.removeBackupTransaction(transaction?.transactionID ?? '-1');
+                        return;
+                    }
+                    TransactionEdit.restoreOriginalTransactionFromBackup(transaction?.transactionID ?? '-1', IOUUtils.shouldUseTransactionDraft(action));

+                    if (!transaction?.reportID) {
+                        return;
+                    }
+                    Report.openReport(transaction?.reportID);
+                    return;
+                }

+                TransactionEdit.createBackupTransaction(transaction);
+            });
+        });

        return () => {
            // If the user cancels out of the modal without without saving changes, then the original transaction
            // needs to be restored from the backup so that all changes are removed.
            if (transactionWasSaved.current) {
                TransactionEdit.removeBackupTransaction(transaction?.transactionID ?? '-1');
                return;
            }
            TransactionEdit.restoreOriginalTransactionFromBackup(transaction?.transactionID ?? '-1', IOUUtils.shouldUseTransactionDraft(action));

            // If the user opens IOURequestStepDistance in offline mode and then goes online, re-open the report to fill in missing fields from the transaction backup
            if (!transaction?.reportID) {
                return;
            }
            Report.openReport(transaction?.reportID);
        };
        // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
    }, []);
POC https://github.com/user-attachments/assets/63fe3c16-8039-40a6-8b3d-b609cc7b13ca

Test Branch

What alternative solutions did you explore? (Optional)

joekaufmanexpensify commented 1 week ago

Reproduced.

melvin-bot[bot] commented 1 week ago

Job added to Upwork: https://www.upwork.com/jobs/~021857103747653162057

melvin-bot[bot] commented 1 week ago

Triggered auto assignment to Contributor-plus team member for initial proposal review - @rayane-djouah (External)

melvin-bot[bot] commented 1 week ago

@joekaufmanexpensify, @rayane-djouah Whoops! This issue is 2 days overdue. Let's get this updated quick!

rayane-djouah commented 1 week ago

Reviewing

rayane-djouah commented 1 week ago

we need the setTimeout b/c by the time the effect is run useOnyx will not populate transactionBackup but we can also optionally use Onyx.connect and run the code inside the callback in createBackupTransaction

@FitseTLT, could you please share a code example that demonstrates how to fix the bug by modifying the createBackupTransaction implementation?

FitseTLT commented 1 week ago

we need the setTimeout b/c by the time the effect is run useOnyx will not populate transactionBackup but we can also optionally use Onyx.connect and run the code inside the callback in createBackupTransaction

@FitseTLT, could you please share a code example that demonstrates how to fix the bug by modifying the createBackupTransaction implementation?

Will provide tomorrow 👍

huult commented 6 days ago

@rayane-djouah What do you think about my proposal above?

melvin-bot[bot] commented 6 days ago

📣 It's been a week! Do we have any satisfactory proposals yet? Do we need to adjust the bounty for this issue? 💸

FitseTLT commented 6 days ago

@rayane-djouah U can check it now. Updated

rayane-djouah commented 2 days ago

@joekaufmanexpensify could you please clarify the expected behavior? When we open a distance request, edit the itinerary on the Distance Editor RHP page, and then refresh the page without saving, should the changes be discarded (similar to dismissing the Distance Editor RHP)? Or should the changes persist as a draft until we either dismiss the page or click "Save"?

joekaufmanexpensify commented 2 days ago

Left thoughts here. I would expect us not to save the behavior if you refresh the page without saving.

rayane-djouah commented 2 days ago

@FitseTLT @huult Could you please update your proposals based on this?

FitseTLT commented 2 days ago

Updated according to the expectation above.

huult commented 1 day ago

@FitseTLT @huult Could you please update your proposals based on this?

I will update it today.

melvin-bot[bot] commented 1 day ago

@joekaufmanexpensify @rayane-djouah this issue was created 2 weeks ago. Are we close to approving a proposal? If not, what's blocking us from getting this issue assigned? Don't hesitate to create a thread in #expensify-open-source to align faster in real time. Thanks!

huult commented 1 day ago

Proposal update

FitseTLT commented 1 day ago

@rayane-djouah BTW I misunderstood the expected behaviour yesterday if it is to save the changes then my previous proposal was already aligned to it so I have reverted back to it. 👍

joekaufmanexpensify commented 1 day ago

@rayane-djouah as an FYI, I am OOO the rest of the week after today, back next Monday. Not reassigning as a lot of the US team will be off and I don't think anything BZ will be needed during that time. Please raise in slack if anything comes up.