Skip to content
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

fix(web): pagination search params on activity feed page #5231

Merged
merged 3 commits into from
Feb 26, 2024

Conversation

LetItRock
Copy link
Contributor

What change does this PR introduce?

This PR fixes a few problems:

  • the paginated search params on the Activity Feed page from the useTemplates hook
  • query cache issue when navigating between Workflows and Activity Feed pages (on slow network)

Why was this change needed?

Other information (Screenshots)

Before:

Screen.Recording.2024-02-23.at.17.34.13.mov

Now:

Screen.Recording.2024-02-23.at.17.34.43.mov

Copy link

netlify bot commented Feb 23, 2024

Deploy Preview for dev-web-novu ready!

Name Link
🔨 Latest commit 07196f9
🔍 Latest deploy log https://app.netlify.com/sites/dev-web-novu/deploys/65dc7a9716669a00083aa8c4
😎 Deploy Preview https://deploy-preview-5231--dev-web-novu.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines 29 to 32
const { templates = [], loading: templatesLoading } = useTemplates({
pageIndex: 0,
pageSize: 100,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The templates are used here to check if the Digest Demo Workflow already exists or not. I changed this hook to reuse the same query cache as in the other places. See the one below.

Comment on lines 60 to 63
const { templates = [], loading: templatesLoading } = useTemplates({
pageIndex: 0,
pageSize: 100,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The templates are used here to check if the Onboarding Workflow already exists or not. Use same query cache.

export function useTemplates({
pageIndex,
pageSize,
areSearchParamsEnabled = false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the flag that is used to control if the query params will be shown in the URL. For example, on the Activity Feed page, we don't want to show it, but the hook is used there for the filter field data.

Comment on lines +43 to +45
areSearchParamsEnabled,
startingPageNumber: (pageIndex ?? 0) + 1,
startingPageSize: pageSize,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

pass the initial options to usePaginationState hook

Copy link
Contributor Author

@LetItRock LetItRock Feb 23, 2024

Choose a reason for hiding this comment

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

The pageIndex and pageSize from the args of useTemplates hook were not used to initialize the usePaginationState hook, resulting in defaults used which created the query key like ['notification-templates', env.id, 1, 20]. But when the request was made they were passed to API request, which as a result filled the same cache used on the Workflows page but with more data.

@@ -30,11 +34,16 @@ export function useTemplates(pageIndex?: number, pageSize?: number) {
buildQueryFn:
({ pageIndex: ctxPageIndex, pageSize: ctxPageSize }) =>
() =>
getNotificationsList(pageIndex ?? ctxPageIndex, pageSize ?? ctxPageSize),
getNotificationsList(ctxPageIndex, ctxPageSize),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the only source of truth is usePaginationState hook

Comment on lines -56 to -58
const debouncedTransactionIdChange = useDebounce((transactionId: string) => {
onFiltersChange({ transactionId });
}, 500);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

not used

@@ -163,7 +163,7 @@ function WorkflowListPage() {
setCurrentPageNumber,
setPageSize,
pageSize,
} = useTemplates();
} = useTemplates({ areSearchParamsEnabled: true });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

enable the search params for the Workflows List page

Comment on lines 23 to 28
const mockUseSearchParams = (): ReturnType<typeof useSearchParams> => {
const params = new URLSearchParams();
return [params, setSearchParams];
};

vi.spyOn(ReactRouterDOM, 'useSearchParams').mockImplementation(mockUseSearchParams);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

used to verify that the setSearchParams function was called in the hook

Comment on lines +66 to +68
return pageSizes.includes(sizeValUnchecked) && sizeValUnchecked > 0
? sizeValUnchecked
: startingPageSize ?? pageSizes[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

use the startingPageSize if search params don't contain the value or default page size value

);
const [currentPageNumber, setCurrentPageNumber] = useState<number>(
getValidatedPageNumberFromUrl(searchParams.get(pageNumberParamName), startingPageNumber)
);

useEffect(() => {
if (!areSearchParamsEnabled) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if not enabled then do not set the search params

Copy link
Contributor

@antonjoel82 antonjoel82 left a comment

Choose a reason for hiding this comment

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

Awesome, thanks so much for catching and fixing this! And the tests to boot

libs/design-system/src/pagination/usePaginationState.ts Outdated Show resolved Hide resolved
pageIndex,
pageSize,
areSearchParamsEnabled = false,
}: { pageIndex?: number; pageSize?: number; areSearchParamsEnabled?: boolean } = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

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

💣 chore: Please derive these params from the UsePaginationOptions instead ‏

@LetItRock LetItRock merged commit 100795e into next Feb 26, 2024
24 checks passed
@LetItRock LetItRock deleted the fix-pagination-search-params branch February 26, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants