Skip to content

Commit

Permalink
fix: fix the race condition resulting in switching between views not …
Browse files Browse the repository at this point in the history
…working properly
  • Loading branch information
ahmadshaheer authored and YounixM committed Oct 28, 2024
1 parent 94e0423 commit e88e24e
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions frontend/src/hooks/useUrlQueryData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import useUrlQuery from 'hooks/useUrlQuery';
import { useCallback, useMemo } from 'react';
import { useHistory, useLocation } from 'react-router-dom';

import useUrlQuery from './useUrlQuery';

const useUrlQueryData = <T>(
queryKey: string,
defaultData?: T,
Expand All @@ -10,7 +11,7 @@ const useUrlQueryData = <T>(
const location = useLocation();
const urlQuery = useUrlQuery();

const query = useMemo(() => urlQuery.get(queryKey), [queryKey, urlQuery]);
const query = useMemo(() => urlQuery.get(queryKey), [urlQuery, queryKey]);

const queryData: T = useMemo(() => (query ? JSON.parse(query) : defaultData), [
query,
Expand All @@ -21,11 +22,17 @@ const useUrlQueryData = <T>(
(newQueryData: T): void => {
const newQuery = JSON.stringify(newQueryData);

urlQuery.set(queryKey, newQuery);
const generatedUrl = `${location.pathname}?${urlQuery.toString()}`;
// Create a new URLSearchParams to get the latest URL state
const currentUrlQuery = new URLSearchParams(window.location.search);

// Update the query parameter
currentUrlQuery.set(queryKey, newQuery);

// Generate the new URL with updated parameters
const generatedUrl = `${location.pathname}?${currentUrlQuery.toString()}`;
history.replace(generatedUrl);
},
[history, location, urlQuery, queryKey],
[history, location.pathname, queryKey],
);

return {
Expand Down

0 comments on commit e88e24e

Please sign in to comment.