-
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.
update/add hook for selected feature (#288)
* Refactor selecting feature to be a hook * Rename set selected feature method * Add current features hook and extend selected feature hook * Fix so that we find first query with data This would handle when we have queries that are disabled. * Add simple tests for useFeatures and useCurrentFeatures * Use static string for query key * Fix linting
- Loading branch information
1 parent
cccce3e
commit 3fb9d6e
Showing
8 changed files
with
183 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { useDeleteFeature } from './useDeleteFeature'; | ||
export { useFeatures } from './useFeatures'; | ||
export { useFeatures, useCurrentFeatures } 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
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,76 @@ | ||
import { useCallback } from 'react'; | ||
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom'; | ||
import { useCurrentFeatures } from '.'; | ||
import { Feature, FeatureCollection } from '@hazmapper/types'; | ||
|
||
const SELECTED_FEATURE_PARAM = 'selectedFeature'; | ||
|
||
interface UseFeatureSelectionReturn { | ||
selectedFeatureId: number | null; | ||
selectedFeature: Feature | null; | ||
setSelectedFeatureId: (featureId: number) => void; | ||
} | ||
|
||
const findFeatureById = ( | ||
featureCollection: FeatureCollection, | ||
selectedFeatureId: number | null | ||
): Feature | null => { | ||
if (selectedFeatureId === null) return null; | ||
|
||
return ( | ||
featureCollection.features.find( | ||
(feature) => feature.id === selectedFeatureId | ||
) || null | ||
); | ||
}; | ||
|
||
/** | ||
* A custom hook that manages feature selection state through URL search parameters. | ||
* | ||
* This hook provides functionality to: | ||
* - Get the currently selected feature ID from URL parameters | ||
* - Get the corresponding Feature object for the selected ID ( *if* it is in the current filtered feature response ) | ||
* - Set/unset the selected feature ID in the URL | ||
* | ||
* */ | ||
export function useFeatureSelection(): UseFeatureSelectionReturn { | ||
const currentFeatures = useCurrentFeatures(); | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const [searchParams] = useSearchParams(); | ||
|
||
const selectedFeatureId = searchParams.get(SELECTED_FEATURE_PARAM) | ||
? Number(searchParams.get(SELECTED_FEATURE_PARAM)) | ||
: null; | ||
|
||
const selectedFeature = currentFeatures.data | ||
? findFeatureById(currentFeatures.data, selectedFeatureId) | ||
: null; | ||
|
||
const setSelectedFeatureId = 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, | ||
selectedFeature, | ||
setSelectedFeatureId, | ||
}; | ||
} |
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,55 @@ | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import { useFeatures, useCurrentFeatures } from './useFeatures'; | ||
import { featureCollection } from '@hazmapper/__fixtures__/featuresFixture'; | ||
import { TestWrapper, testQueryClient } from '@hazmapper/test/testUtil'; | ||
|
||
describe('Feature Hooks', () => { | ||
afterEach(() => { | ||
testQueryClient.clear(); | ||
}); | ||
|
||
it('useFeatures should fetch features', async () => { | ||
const { result } = renderHook( | ||
() => | ||
useFeatures({ | ||
projectId: 80, | ||
isPublicView: false, | ||
assetTypes: ['image'], | ||
}), | ||
{ wrapper: TestWrapper } | ||
); | ||
|
||
expect(result.current.isLoading).toBe(true); | ||
|
||
await waitFor(() => { | ||
expect(result.current.isSuccess).toBe(true); | ||
}); | ||
|
||
expect(result.current.data).toEqual(featureCollection); | ||
}); | ||
|
||
it('useCurrentFeatures should return cached features', async () => { | ||
// First, populate the cache with a features query | ||
const { result: featuresResult } = renderHook( | ||
() => | ||
useFeatures({ | ||
projectId: 80, | ||
isPublicView: false, | ||
assetTypes: ['image'], | ||
}), | ||
{ wrapper: TestWrapper } | ||
); | ||
|
||
await waitFor(() => { | ||
expect(featuresResult.current.isSuccess).toBe(true); | ||
}); | ||
|
||
// Now test useCurrentFeatures | ||
const { result: currentResult } = renderHook(() => useCurrentFeatures(), { | ||
wrapper: TestWrapper, | ||
}); | ||
|
||
expect(currentResult.current.isLoading).toBe(false); | ||
expect(currentResult.current.data).toEqual(featureCollection); | ||
}); | ||
}); |
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