-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor selecting feature to be a hook
- Loading branch information
1 parent
f183a6c
commit 8587705
Showing
5 changed files
with
58 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useDeleteFeature } from './useDeleteFeature'; | ||
export { useFeatures } from './useFeatures'; | ||
export { useFeatureSelection } from './useFeatureSelection'; |
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,45 @@ | ||
import { useCallback } from 'react'; | ||
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom'; | ||
|
||
const SELECTED_FEATURE_PARAM = 'selectedFeature'; | ||
|
||
interface UseFeatureSelectionReturn { | ||
selectedFeatureId: number | null; | ||
setSelectedFeature: (featureId: number) => void; | ||
} | ||
|
||
export function useFeatureSelection(): UseFeatureSelectionReturn { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const [searchParams] = useSearchParams(); | ||
|
||
const selectedFeatureId = searchParams.get(SELECTED_FEATURE_PARAM) | ||
? Number(searchParams.get(SELECTED_FEATURE_PARAM)) | ||
: null; | ||
|
||
const setSelectedFeature = useCallback( | ||
(featureId: number) => { | ||
const newSearchParams = new URLSearchParams(searchParams); | ||
|
||
if (selectedFeatureId === Number(featureId)) { | ||
newSearchParams.delete(SELECTED_FEATURE_PARAM); | ||
} else { | ||
newSearchParams.set(SELECTED_FEATURE_PARAM, String(featureId)); | ||
} | ||
|
||
navigate( | ||
{ | ||
pathname: location.pathname, | ||
search: newSearchParams.toString(), | ||
}, | ||
{ replace: true } | ||
); | ||
}, | ||
[navigate, location.pathname, searchParams, selectedFeatureId] | ||
); | ||
|
||
return { | ||
selectedFeatureId, | ||
setSelectedFeature, | ||
}; | ||
} |