Skip to content

Conversation

@m4dm4rtig4n
Copy link
Contributor

Summary

Fix issue where selected energy offer was not displayed on Dashboard page load. When navigating back to a PDL card with a pre-selected offer (cached by React Query), the cascading selectors showed 'Sélectionner...' instead of the saved values.

Changes

  • Add synced state tracking to OfferSelector to prevent selector resets during manual user selection
  • Ensure cascading selectors initialize correctly when offers data is already cached
  • Handle both initial mount and external offer changes

Testing

  • Verify selected offer displays correctly on Dashboard load
  • Test manual selection still works properly
  • Test offer clearing and re-selection flows

Initialize cascading selectors with synced state tracking to ensure selected
offer displays correctly when navigating to Dashboard with cached offers data.
Prevents dropdown from showing 'Sélectionner...' on page load.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Copilot AI review requested due to automatic review settings December 5, 2025 08:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where selected energy offers were not displaying correctly on the Dashboard page when navigating back to a PDL card with a pre-selected offer that's cached by React Query. The fix adds state tracking to synchronize cascading selectors with the selected offer, handling both initial load scenarios and external offer changes.

Key changes:

  • Introduced lastSyncedOfferId state to track which offer has been synced to the selectors
  • Enhanced the useEffect to conditionally sync selectors only when the offer changes externally
  • Added logic to distinguish between initial mount, external changes, and offer clearing

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 105 to +123
useEffect(() => {
if (selectedOffer) {
setSelectedProviderId(selectedOffer.provider_id)
setSelectedOfferType(selectedOffer.offer_type)
// We have a valid offer - sync the selectors if not already synced to this offer
if (lastSyncedOfferId !== selectedOffer.id) {
setSelectedProviderId(selectedOffer.provider_id)
setSelectedOfferType(selectedOffer.offer_type)
setLastSyncedOfferId(selectedOffer.id)
}
} else if (selectedOfferId === null || selectedOfferId === undefined) {
// Offer was cleared externally - reset selectors
if (lastSyncedOfferId !== null) {
setSelectedProviderId(null)
setSelectedOfferType(null)
setLastSyncedOfferId(null)
}
}
}, [selectedOffer])
// Note: when selectedOfferId is set but selectedOffer is null (offers loading),
// we wait - the effect will re-run when offers finish loading
}, [selectedOffer, selectedOfferId, lastSyncedOfferId])
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical bug: This effect will interfere with manual user selection. When a user manually changes selectedProviderId via handleProviderChange (line 127), the effect will immediately re-sync the selectors back to the old selectedOffer values, preventing the user from changing their selection.

The issue occurs because:

  1. User changes provider → handleProviderChange sets selectedProviderId to new value
  2. selectedOfferId prop hasn't changed yet (still old offer)
  3. Effect runs → selectedOffer is still the old offer and lastSyncedOfferId !== selectedOffer.id is true
  4. Effect resets selectedProviderId back to old offer's provider

To fix this, you need to update lastSyncedOfferId when users manually interact with the selectors (in handleProviderChange, handleOfferTypeChange, and handleClear). This tells the effect that the user is actively changing the selection and prevents it from overwriting their changes. For example:

const handleProviderChange = (providerId: string | null) => {
  setSelectedProviderId(providerId)
  setSelectedOfferType(null)
  setLastSyncedOfferId(undefined) // Signal that user is manually selecting
}

Copilot uses AI. Check for mistakes.
Comment on lines +115 to +119
if (lastSyncedOfferId !== null) {
setSelectedProviderId(null)
setSelectedOfferType(null)
setLastSyncedOfferId(null)
}
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The condition lastSyncedOfferId !== null will fail to reset selectors when lastSyncedOfferId is undefined (its initial state).

This can happen on initial render if selectedOfferId is null or undefined from the start. In this case, lastSyncedOfferId would be undefined, but the condition checks for !== null, so the reset won't happen.

Change the condition to:

if (lastSyncedOfferId !== null && lastSyncedOfferId !== undefined) {

Or more concisely:

if (lastSyncedOfferId != null) { // using != instead of !== checks both null and undefined

Copilot uses AI. Check for mistakes.
@m4dm4rtig4n m4dm4rtig4n merged commit fc910ed into main Dec 5, 2025
15 of 19 checks passed
@m4dm4rtig4n m4dm4rtig4n deleted the fix-provider-display-init branch December 18, 2025 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants