Skip to content

Commit

Permalink
5604/fix query caching (#5631)
Browse files Browse the repository at this point in the history
* fix caching issue, add revert to default button

* fix caching issue, add revert to default button

* oopsies

* changeset

* name change

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
  • Loading branch information
gwyneplaine and emmatown committed May 7, 2021
1 parent 2b0756c commit 53225b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-kings-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/admin-ui': patch
---

Fixed cache querying logic and added explicit query clearing flow to admin-ui.
33 changes: 26 additions & 7 deletions packages-next/admin-ui/src/pages/ListPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,23 @@ let listMetaGraphqlQuery: TypedDocumentNode<
function useQueryParamsFromLocalStorage(listKey: string) {
const router = useRouter();
const localStorageKey = `keystone.list.${listKey}.list.page.info`;
const storeableQueries = ['sortBy', 'fields'];

const resetToDefaults = () => {
localStorage.removeItem(`keystone.list.${listKey}.list.page.info`);
router.replace({
query: null,
});
};

// GET QUERY FROM CACHE IF CONDITIONS ARE RIGHT
// MERGE QUERY PARAMS FROM CACHE WITH QUERY PARAMS FROM ROUTER
useEffect(() => {
let hasSomeQueryParamsWhichAreAboutListPage = Object.keys(router.query).some(
x => x.startsWith('!') || x === 'page' || x === 'pageSize' || x === 'fields'
);
if (!hasSomeQueryParamsWhichAreAboutListPage) {
let hasSomeQueryParamsWhichAreAboutListPage = Object.keys(router.query).some(x => {
return x.startsWith('!') || storeableQueries.includes(x);
});

if (!hasSomeQueryParamsWhichAreAboutListPage && router.isReady) {
const queryParamsFromLocalStorage = localStorage.getItem(localStorageKey);
let parsed;
try {
Expand All @@ -96,11 +108,11 @@ function useQueryParamsFromLocalStorage(listKey: string) {
});
}
}
}, [localStorageKey]);
}, [localStorageKey, router.isReady]);
useEffect(() => {
let queryParamsToSerialize: Record<string, string> = {};
Object.keys(router.query).forEach(key => {
if (key.startsWith('!') || key === 'page' || key === 'pageSize' || key === 'fields') {
if (key.startsWith('!') || storeableQueries.includes(key)) {
queryParamsToSerialize[key] = router.query[key] as string;
}
});
Expand All @@ -113,6 +125,8 @@ function useQueryParamsFromLocalStorage(listKey: string) {
localStorage.removeItem(`keystone.list.${listKey}.list.page.info`);
}
}, [localStorageKey, router]);

return { resetToDefaults };
}

export const getListPage = (props: ListPageProps) => () => <ListPage {...props} />;
Expand All @@ -122,7 +136,7 @@ const ListPage = ({ listKey }: ListPageProps) => {

const { query } = useRouter();

useQueryParamsFromLocalStorage(listKey);
const { resetToDefaults } = useQueryParamsFromLocalStorage(listKey);

let currentPage =
typeof query.page === 'string' && !Number.isNaN(parseInt(query.page)) ? Number(query.page) : 1;
Expand Down Expand Up @@ -237,6 +251,11 @@ const ListPage = ({ listKey }: ListPageProps) => {
{showCreate && <CreateButton listKey={listKey} />}
{data.meta.count || filters.filters.length ? <FilterAdd listKey={listKey} /> : null}
{filters.filters.length ? <FilterList filters={filters.filters} list={list} /> : null}
{Boolean(Object.keys(query).length) && (
<Button size="small" onClick={resetToDefaults}>
Reset to defaults
</Button>
)}
</Stack>
{data.meta.count ? (
<Fragment>
Expand Down

1 comment on commit 53225b0

@vercel
Copy link

@vercel vercel bot commented on 53225b0 May 7, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.