Skip to content

Commit

Permalink
fix: refactor useLocationSearchState name
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Oct 21, 2024
1 parent 4a32088 commit 88077f2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
10 changes: 3 additions & 7 deletions src/components/sectionList/listActions/SectionListActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {
} from '@dhis2/ui'
import React, { useRef, useState } from 'react'
import { useHref, useLinkClickHandler } from 'react-router-dom'
import {
TOOLTIPS,
BaseListModel,
useCreateLocationSearchState,
} from '../../../lib'
import { TOOLTIPS, BaseListModel, useLocationSearchState } from '../../../lib'
import { LinkButton } from '../../LinkButton'
import { TooltipWrapper } from '../../tooltip'
import { DeleteAction } from './DeleteAction'
Expand All @@ -32,7 +28,7 @@ export const ListActions = ({ children }: React.PropsWithChildren) => {
}

export const ActionEdit = ({ modelId }: { modelId: string }) => {
const preservedSearchState = useCreateLocationSearchState()
const preservedSearchState = useLocationSearchState()
return (
<LinkButton
small
Expand Down Expand Up @@ -70,7 +66,7 @@ export const ActionMore = ({
const [open, setOpen] = useState(false)
const ref = useRef(null)
const href = useHref(model.id, { relative: 'path' })
const preservedSearchState = useCreateLocationSearchState()
const preservedSearchState = useLocationSearchState()

const handleEditClick = useLinkClickHandler(
{
Expand Down
4 changes: 2 additions & 2 deletions src/components/sectionList/toolbar/ToolbarNormal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Link } from 'react-router-dom'
import {
routePaths,
useCanCreateModelInSection,
useCreateLocationSearchState,
useLocationSearchState,
useModelSectionHandleOrThrow,
} from '../../../lib'
import { ManageListViewDialog } from '../listView/ManageListViewDialog'
Expand All @@ -22,7 +22,7 @@ export const ToolbarNormal = ({
const [manageColumnsOpen, setManageColumnsOpen] = React.useState(false)
const section = useModelSectionHandleOrThrow()
const canCreateModel = useCanCreateModelInSection(section)
const locationState = useCreateLocationSearchState()
const locationState = useLocationSearchState()

const handleClose = () => setManageColumnsOpen(false)
return (
Expand Down
10 changes: 5 additions & 5 deletions src/lib/routeUtils/useLocationSearchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ type LocationSearchState = {
search?: string
} | null

export const createLocationState = (search: string): LocationSearchState => ({
const createLocationState = (search: string): LocationSearchState => ({
search,
})

const applySearchState = (to: To, location: LocationSearchState) => {
const applySearchState = (to: To, locationSearchState: LocationSearchState) => {
if (typeof to === 'string') {
return { pathname: to, search: location?.search }
return { pathname: to, search: locationSearchState?.search }
}
return { ...to, search: to.search || location?.search }
return { ...to, search: to.search || locationSearchState?.search }
}

/**
* Use this to preserve the current search-state between pages.
* Pass the returned state to Link components or as options to navigate as "state"-property.
* @returns an object that holds the current search-state.
*/
export const useCreateLocationSearchState = () => {
export const useLocationSearchState = () => {
const location = useLocation()

return useMemo(
Expand Down

0 comments on commit 88077f2

Please sign in to comment.