-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add PersistentFilters component * add PersistentFilters test * add persistent filters to all list pages * update tests * clear sessionStorage on logout * fix persistent filter on wfjt detail; cleanup
- Loading branch information
1 parent
faa5df1
commit fdd5607
Showing
45 changed files
with
277 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
awx/ui/src/components/PersistentFilters/PersistentFilters.js
This file contains 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,33 @@ | ||
import { useEffect } from 'react'; | ||
import { useLocation, useHistory } from 'react-router'; | ||
import { PERSISTENT_FILTER_KEY } from '../../constants'; | ||
|
||
export default function PersistentFilters({ pageKey, children }) { | ||
const location = useLocation(); | ||
const history = useHistory(); | ||
|
||
useEffect(() => { | ||
if (!location.search.includes('restoreFilters=true')) { | ||
return; | ||
} | ||
|
||
const filterString = sessionStorage.getItem(PERSISTENT_FILTER_KEY); | ||
const filter = filterString ? JSON.parse(filterString) : { qs: '' }; | ||
|
||
if (filter.pageKey === pageKey) { | ||
history.replace(`${location.pathname}${filter.qs}`); | ||
} else { | ||
history.replace(location.pathname); | ||
} | ||
}, [history, location, pageKey]); | ||
|
||
useEffect(() => { | ||
const filter = { | ||
pageKey, | ||
qs: location.search, | ||
}; | ||
sessionStorage.setItem(PERSISTENT_FILTER_KEY, JSON.stringify(filter)); | ||
}, [location.search, pageKey]); | ||
|
||
return children; | ||
} |
111 changes: 111 additions & 0 deletions
111
awx/ui/src/components/PersistentFilters/PersistentFilters.test.js
This file contains 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,111 @@ | ||
import React from 'react'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { Router, Route } from 'react-router-dom'; | ||
import { createMemoryHistory } from 'history'; | ||
import PersistentFilters from './PersistentFilters'; | ||
|
||
const KEY = 'awx-persistent-filter'; | ||
|
||
describe('PersistentFilters', () => { | ||
test('should initialize filter in sessionStorage', () => { | ||
expect(sessionStorage.getItem(KEY)).toEqual(null); | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/templates'], | ||
}); | ||
render( | ||
<Router history={history}> | ||
<PersistentFilters pageKey="templates">test</PersistentFilters> | ||
</Router> | ||
); | ||
|
||
expect(JSON.parse(sessionStorage.getItem(KEY))).toEqual({ | ||
pageKey: 'templates', | ||
qs: '', | ||
}); | ||
}); | ||
|
||
test('should restore filters from sessionStorage', () => { | ||
expect( | ||
sessionStorage.setItem( | ||
KEY, | ||
JSON.stringify({ | ||
pageKey: 'templates', | ||
qs: '?page=2&name=foo', | ||
}) | ||
) | ||
); | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/templates?restoreFilters=true'], | ||
}); | ||
render( | ||
<Router history={history}> | ||
<PersistentFilters pageKey="templates">test</PersistentFilters> | ||
</Router> | ||
); | ||
|
||
expect(history.location.search).toEqual('?page=2&name=foo'); | ||
}); | ||
|
||
test('should not restore filters without restoreFilters query param', () => { | ||
expect( | ||
sessionStorage.setItem( | ||
KEY, | ||
JSON.stringify({ | ||
pageKey: 'templates', | ||
qs: '?page=2&name=foo', | ||
}) | ||
) | ||
); | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/templates'], | ||
}); | ||
render( | ||
<Router history={history}> | ||
<PersistentFilters pageKey="templates">test</PersistentFilters> | ||
</Router> | ||
); | ||
|
||
expect(history.location.search).toEqual(''); | ||
}); | ||
|
||
test("should not restore filters if page key doesn't match", () => { | ||
expect( | ||
sessionStorage.setItem( | ||
KEY, | ||
JSON.stringify({ | ||
pageKey: 'projects', | ||
qs: '?page=2&name=foo', | ||
}) | ||
) | ||
); | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/templates?restoreFilters=true'], | ||
}); | ||
render( | ||
<Router history={history}> | ||
<PersistentFilters pageKey="templates">test</PersistentFilters> | ||
</Router> | ||
); | ||
|
||
expect(history.location.search).toEqual(''); | ||
}); | ||
|
||
test('should update stored filters when qs changes', async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: ['/templates'], | ||
}); | ||
render( | ||
<Router history={history}> | ||
<PersistentFilters pageKey="templates">test</PersistentFilters> | ||
</Router> | ||
); | ||
|
||
history.push('/templates?page=3'); | ||
await waitFor(() => true); | ||
|
||
expect(JSON.parse(sessionStorage.getItem(KEY))).toEqual({ | ||
pageKey: 'templates', | ||
qs: '?page=3', | ||
}); | ||
}); | ||
}); |
This file contains 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 @@ | ||
export { default } from './PersistentFilters'; |
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.