Repository navigation
feat: refresh organization members and roles on history restores #1400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MrPunyapal
wants to merge
4
commits into
dev-null-sh:production
Choose a base branch
from
MrPunyapal:feat/refresh-org-members-on-history-restore
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74729f6
feat: refresh organization members and roles on history restores
MrPunyapal 63fdcbf
fix: refresh organization members after bfcache restoration
MrPunyapal d8c59cc
style: keep updated source within project formatting conventions
MrPunyapal 15776f1
fix: refresh auth identity and reload after account changes
MrPunyapal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { router } from '@inertiajs/react'; | ||
| import { currentPageComponent } from '@/lib/current-page'; | ||
|
|
||
| const authUserId = (page: { props: Record<string, unknown> }) => { | ||
| const auth = page.props.auth as | ||
| | { user?: { id: string | number } | null } | ||
| | undefined; | ||
|
|
||
| return auth?.user?.id ?? null; | ||
| }; | ||
|
|
||
| /** Another admin may change memberships or roles after this page was cached. */ | ||
| export function trackOrganizationMembersHistory(): VoidFunction { | ||
| let currentUserId: string | number | null = null; | ||
|
|
||
| const refresh = (): void => { | ||
| const previousUserId = currentUserId; | ||
|
|
||
| router.reload({ | ||
| only: ['organization', 'role', 'members', 'auth'], | ||
| preserveScroll: true, | ||
| onSuccess: (page) => { | ||
| if (authUserId(page) !== previousUserId) { | ||
| window.location.reload(); | ||
| } | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| const stop = router.on('navigate', ({ detail: { type, page } }) => { | ||
| currentUserId = authUserId(page); | ||
|
|
||
| if ( | ||
| type === 'history' && | ||
| page.component === 'organization-member/index' | ||
| ) { | ||
| refresh(); | ||
| } | ||
| }); | ||
|
|
||
| const onPageShow = (event: PageTransitionEvent): void => { | ||
| if ( | ||
| event.persisted && | ||
| currentPageComponent() === 'organization-member/index' | ||
| ) { | ||
| refresh(); | ||
| } | ||
| }; | ||
|
|
||
| window.addEventListener('pageshow', onPageShow); | ||
|
|
||
| return () => { | ||
| stop(); | ||
| window.removeEventListener('pageshow', onPageShow); | ||
| }; | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test, vi } from 'vite-plus/test'; | ||
|
|
||
| type NavigationType = 'initial' | 'visit' | 'history'; | ||
| type Navigation = { | ||
| detail: { | ||
| type: NavigationType; | ||
| page: { | ||
| component: string; | ||
| props: { auth: { user: { id: string } } }; | ||
| }; | ||
| }; | ||
| }; | ||
|
|
||
| const reload = vi.fn(); | ||
| let navigate: ((event: Navigation) => void) | undefined; | ||
| let current = 'log/show'; | ||
|
|
||
| vi.doMock('@inertiajs/react', () => ({ | ||
| router: { | ||
| on: (event: string, listener: (event: Navigation) => void) => { | ||
| assert.equal(event, 'navigate'); | ||
| navigate = listener; | ||
|
|
||
| return () => { | ||
| navigate = undefined; | ||
| }; | ||
| }, | ||
| reload, | ||
| }, | ||
| })); | ||
| vi.doMock('@/lib/current-page', () => ({ | ||
| currentPageComponent: () => current, | ||
| })); | ||
|
|
||
| const { trackOrganizationMembersHistory } = await import( | ||
| '../../resources/js/lib/organization-members-history' | ||
| ); | ||
|
|
||
| test('refreshes members and identity on history and bfcache restore', () => { | ||
| reload.mockClear(); | ||
| const events = new Map<string, (event: { persisted: boolean }) => void>(); | ||
| const reloadPage = vi.fn(); | ||
| vi.stubGlobal('window', { | ||
| location: { reload: reloadPage }, | ||
| addEventListener: (name: string, listener: (event: { persisted: boolean }) => void) => | ||
| events.set(name, listener), | ||
| removeEventListener: (name: string) => events.delete(name), | ||
| }); | ||
|
|
||
| try { | ||
| const stop = trackOrganizationMembersHistory(); | ||
| const go = (type: NavigationType, component: string) => | ||
| navigate?.({ | ||
| detail: { | ||
| type, | ||
| page: { component, props: { auth: { user: { id: '1' } } } }, | ||
| }, | ||
| }); | ||
|
|
||
| go('initial', 'organization-member/index'); | ||
| go('visit', 'organization-member/index'); | ||
| go('history', 'log/show'); | ||
| assert.equal(reload.mock.calls.length, 0); | ||
|
|
||
| go('history', 'organization-member/index'); | ||
| assert.equal(reload.mock.calls.length, 1); | ||
| const options = reload.mock.calls[0][0]; | ||
| assert.deepEqual(options.only, [ | ||
| 'organization', 'role', 'members', 'auth', | ||
| ]); | ||
| assert.equal(options.preserveScroll, true); | ||
|
|
||
| options.onSuccess({ props: { auth: { user: { id: '1' } } } }); | ||
| assert.equal(reloadPage.mock.calls.length, 0); | ||
| options.onSuccess({ props: { auth: { user: { id: '2' } } } }); | ||
| assert.equal(reloadPage.mock.calls.length, 1); | ||
|
|
||
| current = 'organization-member/index'; | ||
| events.get('pageshow')?.({ persisted: false }); | ||
| assert.equal(reload.mock.calls.length, 1); | ||
| events.get('pageshow')?.({ persisted: true }); | ||
| assert.equal(reload.mock.calls.length, 2); | ||
|
|
||
| stop(); | ||
| assert.equal(events.has('pageshow'), false); | ||
| go('history', 'organization-member/index'); | ||
| assert.equal(reload.mock.calls.length, 2); | ||
| } finally { | ||
| vi.unstubAllGlobals(); | ||
| } | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.