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: use-query-param replaceIn instead of pushIn #60

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useQueryParam,
withDefault,
} from 'use-query-params'
import { useIsValidSelection } from './use-is-valid-selection.js'

export const PARAMS_SCHEMA = {
dataSetId: StringParam,
Expand All @@ -15,16 +16,30 @@ export const PARAMS_SCHEMA = {
sectionFilter: StringParam,
}

const useCustomQueryParam = (name, schema) => {
const [paramValue, setParamValue] = useQueryParam(name, schema)
const isValidSelection = useIsValidSelection()
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does it know whether it's a valid selection before the change has been pushed to use-query-param.
So for example a data set has a default cat combo, when the data set id and the org unit id is already present and the user selects a period, when the logic reaches this point, the useIsValidSelection hook doesn't know about the period id yet, does it?

const defaultUpdateType = isValidSelection ? 'pushIn' : 'replaceIn'

const setSelection = useCallback(
(value, updateType) => {
return setParamValue(value, updateType || defaultUpdateType)
},
[setParamValue, defaultUpdateType]
)
return [paramValue, setSelection]
}

export function useDataSetId() {
return useQueryParam('dataSetId', PARAMS_SCHEMA.dataSetId)
return useCustomQueryParam('dataSetId', PARAMS_SCHEMA.dataSetId)
}

export function useOrgUnitId() {
return useQueryParam('orgUnitId', PARAMS_SCHEMA.orgUnitId)
return useCustomQueryParam('orgUnitId', PARAMS_SCHEMA.orgUnitId)
}

export function usePeriodId() {
return useQueryParam('periodId', PARAMS_SCHEMA.periodId)
return useCustomQueryParam('periodId', PARAMS_SCHEMA.periodId)
}

/**
Expand All @@ -36,14 +51,14 @@ export function usePeriodId() {
*
*/
export function useAttributeOptionComboSelection() {
return useQueryParam(
return useCustomQueryParam(
'attributeOptionComboSelection',
PARAMS_SCHEMA.attributeOptionComboSelection
)
}

export function useSectionFilter() {
return useQueryParam('sectionFilter', PARAMS_SCHEMA.sectionFilter)
return useCustomQueryParam('sectionFilter', PARAMS_SCHEMA.sectionFilter)
}

export function useContextSelection() {
Expand Down