fix: prevent premature RUN_PAYROLL_CALCULATED on re-calculate#1401
Open
jeffredodd wants to merge 1 commit intomainfrom
Open
fix: prevent premature RUN_PAYROLL_CALCULATED on re-calculate#1401jeffredodd wants to merge 1 commit intomainfrom
jeffredodd wants to merge 1 commit intomainfrom
Conversation
When re-calculating a payroll (Edit -> Calculate -> Edit -> Calculate), the polling useEffect would immediately fire RUN_PAYROLL_CALCULATED using stale cached data from the first calculation. This caused an auto-transition to the Overview page before the new calculation completed. Root cause: payrollsCalculate is called as a plain function (not a React Query mutation), so it does not invalidate the query cache. When setIsPolling(true) triggers the useEffect, the stale cached calculatedAt satisfies isCalculatedStatus immediately. Fix: Track the calculatedAt timestamp before each calculation via a ref. The useEffect now guards against stale data by checking that calculatedAt has actually changed before firing the transition. Made-with: Cursor
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a race condition in the payroll recalculation flow where stale calculatedAt data could trigger RUN_PAYROLL_CALCULATED immediately, causing a premature transition before the new calculation finishes.
Changes:
- Track the pre-calculation
calculatedAttimestamp via a ref and require a new timestamp before firingRUN_PAYROLL_CALCULATED. - Update the polling transition logic to gate on
isNewCalculation. - Add a regression test covering re-calculation when the payroll was already previously calculated.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/components/Payroll/PayrollConfiguration/PayrollConfiguration.tsx | Adds previousCalculatedAtRef and guards the calculated transition until calculatedAt changes. |
| src/components/Payroll/PayrollConfiguration/PayrollConfiguration.test.tsx | Adds a regression test ensuring stale calculatedAt does not trigger RUN_PAYROLL_CALCULATED during re-calc. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
payrollsCalculateis a plain function (not a React Query mutation), so it doesn't invalidate the query cache. WhensetIsPolling(true)fires, stalecalculatedAtfrom the first calculation satisfiesisCalculatedStatusimmediatelycalculatedAttimestamp before each calculation via a ref and guards the transition with anisNewCalculationcheckTest plan
Made with Cursor