Skip to content

Commit

Permalink
refactor(option set select): use hooks from lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Nov 15, 2023
1 parent ba37aa4 commit 4b47400
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import i18n from '@dhis2/d2-i18n'
import React, { forwardRef } from 'react'
import { useOptionSetsQuery } from '../../../lib'
import { ModelSingleSelect } from '../ModelSingleSelect'
import { useInitialOptionQuery } from './useInitialOptionQuery'
import { useOptionsQuery } from './useOptionsQuery'

interface OptionSetSelectProps {
onChange: ({ selected }: { selected: string }) => void
Expand Down Expand Up @@ -31,7 +31,7 @@ export const OptionSetSelect = forwardRef(function OptionSetSelect(
ref={ref}
invalid={invalid}
useInitialOptionQuery={useInitialOptionQuery}
useOptionsQuery={useOptionsQuery}
useOptionsQuery={useOptionSetsQuery}
placeholder={placeholder}
showAllOption={showAllOption}
onChange={onChange}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { useDataQuery } from '@dhis2/app-runtime'
import { useRef } from 'react'
import { useOptionSetQuery } from '../../../lib'
import { SelectOption } from '../../../types'
import { FilteredOptionSet } from './types'

type InitialOptionSetQueryResult = {
optionSet: FilteredOptionSet
}

const INITIAL_OPTION_QUERY = {
optionSet: {
resource: 'optionSets',
id: (variables: Record<string, string>) => variables.id,
params: {
fields: ['id', 'displayName'],
},
},
}

export function useInitialOptionQuery({
selected,
onComplete,
Expand All @@ -25,10 +15,10 @@ export function useInitialOptionQuery({
selected?: string
}) {
const initialSelected = useRef(selected)
return useDataQuery<InitialOptionSetQueryResult>(INITIAL_OPTION_QUERY, {
return useOptionSetQuery<InitialOptionSetQueryResult>({
lazy: !initialSelected.current,
variables: { id: selected },
onComplete: (data) => {
onComplete: (data: InitialOptionSetQueryResult) => {
const optionSet = data.optionSet
const { id: value, displayName: label } = optionSet
onComplete({ value, label })
Expand Down

This file was deleted.

0 comments on commit 4b47400

Please sign in to comment.