fix: restore location when navigates from a nested route to a new tab#781
fix: restore location when navigates from a nested route to a new tab#781
Conversation
…a new tab Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
📝 WalkthroughWalkthroughReworks tab ↔ URL-fragment handling on the sponsor edit page: introduces Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Tabs as Tabs(UI)
participant Edit as EditSponsorPage
participant Router as History/Router
participant Location as BrowserLocation
User->>Tabs: select tab (e.g., "forms")
Tabs->>Edit: onChange(fragment)
alt nested form route active (match.params.form_id)
Edit->>Router: history.push("/app/summits/.../sponsors/:id/sponsor-forms/:form_id/items#forms")
Router->>Location: update URL (push)
else same-path
Edit->>Router: history.replace(currentPath + "#forms")
Router->>Location: update hash (replace)
end
Location->>Edit: location.hash updated
Edit->>Tabs: useEffect sync selectedTab from location.hash
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
| @@ -122,7 +122,16 @@ const EditSponsorPage = (props) => { | |||
|
|
|||
| const handleTabChange = (event, newValue) => { | |||
There was a problem hiding this comment.
This string-matching against location.pathname is fragile and bypasses React `Router entirely. The app already has two separate entries in sponsor-id-layout.js:144-150 that render
EditSponsorPage:
<Switch>
<Route exact path={match.url} component={EditSponsorPage}/>
<Route
path={`${match.url}/sponsor-forms/:form_id/items`}
component={EditSponsorPage}
/>
</Switch>
The nested route already captures :form_id as a route param. The proper fix is to use match.params:
const sponsorFormItemRoute = !!match.params?.form_id;
This eliminates the URL sniffing entirely and leverages React Router as intended.
| setSelectedTab(newValue); | ||
| window.location.hash = getFragmentFromValue(newValue); | ||
|
|
||
| const basePath = `/app/summits/${currentSummit.id}/sponsors/${entity.id}`; |
There was a problem hiding this comment.
just use a variable
const isNestedFormItemRoute = !!match.params?.form_id;
u could use it to at https://github.com/fntechgit/summit-admin/pull/781/changes#r2775041275
and then this method could be simple
const handleTabChange = (event, newValue) => {
setSelectedTab(newValue);
const fragment = getFragmentFromValue(newValue);
if (match.params?.form_id) {
history.push(
/app/summits/${currentSummit.id}/sponsors/${entity.id}#${fragment}
);
} else {
history.replace({ ...location, hash: #${fragment} });
}
}
|
@tomrndom here already fires handleTabChange when a child is clicked. The additional onClick on each causes handleTabChange to fire twice per click, which now |
|
react router already provide these props use location react router instead of windows.location refactor it and here lets refactor to This way:
|
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@src/pages/sponsors/__tests__/edit-sponsor-page.test.js`:
- Around line 83-100: The first test uses a nested shape for
currentSummitSponsorshipListState (an object with a sponsorships property
containing the list and pagination) but the reducer expects a flat shape; update
the test's state for currentSummitSponsorshipListState to the flat structure
(sponsorships, currentPage, lastPage, perPage, order, orderDir,
totalSponsorships) to match the reducer and the second test; ensure
currentSponsorState and sponsorPageFormsListState remain unchanged.
🧹 Nitpick comments (2)
src/pages/sponsors/edit-sponsor-page.js (1)
193-193: Remove duplicate variable.
sponsorFormItemRouteis identical toisNestedFormItemRoutedefined at line 123. Use the existing variable instead.♻️ Suggested fix
- const sponsorFormItemRoute = !!match.params?.form_id; - return ( <Box> ... <CustomTabPanel value={selectedTab} index={4}> - {sponsorFormItemRoute ? ( + {isNestedFormItemRoute ? ( <SponsorFormsManageItems match={match} />src/pages/sponsors/__tests__/edit-sponsor-page.test.js (1)
105-105: Misleading variable name.The variable is named
usersTabReferencebut it selects"edit_sponsor.tab.forms". Rename toformsTabReferencefor clarity.♻️ Suggested fix
- const usersTabReference = screen.getByText("edit_sponsor.tab.forms"); + const formsTabReference = screen.getByText("edit_sponsor.tab.forms"); await act(async () => { - await userEvent.click(usersTabReference); + await userEvent.click(formsTabReference); });
Signed-off-by: Tomás Castillo <tcastilloboireau@gmail.com>
ref: https://app.clickup.com/t/86b8d9fdp
Signed-off-by: Tomás Castillo tcastilloboireau@gmail.com
Summary by CodeRabbit