Skip to content

Commit

Permalink
refactor: use react-query hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Dec 15, 2023
1 parent 30b53e6 commit 91c56e7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { useProgramFromIndexedDB } from '../../../../../utils/cachedDataHooks/us
import { useDataElementsFromIndexedDB } from '../../../../../utils/cachedDataHooks/useDataElementsFromIndexedDB';
import { useOptionSetsFromIndexedDB } from '../../../../../utils/cachedDataHooks/useOptionSetsFromIndexedDB';

const queryKey = 'useProgramMetadata';

export const useProgramMetadata = (programId: string) => {
const { program, isLoading, isError } = useProgramFromIndexedDB(programId, { enabled: !!programId });

Expand All @@ -17,10 +19,10 @@ export const useProgramMetadata = (programId: string) => {
new Set) : undefined), [program]);

const {
loading: loadingDataElements,
isLoading: loadingDataElements,
dataElements,
error: dataElementsError,
} = useDataElementsFromIndexedDB(dataElementIds);
isError: dataElementsError,
} = useDataElementsFromIndexedDB([queryKey, programId], dataElementIds);

const derivedDataElementValues = useMemo(() =>
(dataElements ? ({
Expand All @@ -37,10 +39,10 @@ export const useProgramMetadata = (programId: string) => {
}) : undefined), [dataElements]);

const {
loading: loadingOptionSets,
isLoading: loadingOptionSets,
optionSets,
error: optionSetsError,
} = useOptionSetsFromIndexedDB(derivedDataElementValues && derivedDataElementValues.optionSetIds);
isError: optionSetsError,
} = useOptionSetsFromIndexedDB([queryKey, programId], derivedDataElementValues && derivedDataElementValues.optionSetIds);

const optionSetDictionary = useMemo(
() => (optionSets ? optionSets.reduce(
Expand Down
33 changes: 0 additions & 33 deletions src/core_modules/capture-core/utils/api/useQueryStyleEvaluation.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
// @flow
import { useCallback } from 'react';
import type { UseQueryOptions } from 'react-query';
import { userStores, getUserStorageController } from '../../storageControllers';
import { useQueryStyleEvaluation } from '../api/useQueryStyleEvaluation';
import { useIndexedDBQuery } from '../reactQueryHelpers';
import type { CachedDataElement } from '../../storageControllers/';

export const useDataElementsFromIndexedDB = (dataElementIds: ?Set<string>): {
export const useDataElementsFromIndexedDB = (queryKey: Array<string | number>, dataElementIds: ?Set<string>, queryOptions?: UseQueryOptions<>): {
dataElements: ?Array<CachedDataElement>,
loading: boolean,
error: any,
isLoading: boolean,
isError: boolean,
} => {
const storageController = getUserStorageController();
const { enabled = !!dataElementIds } = queryOptions ?? {};

const getDataElements = useCallback(requestedIds =>
storageController.getAll(
const { data, isLoading, isError } = useIndexedDBQuery(
['dataElements', ...queryKey],
() => storageController.getAll(
userStores.DATA_ELEMENTS, {
predicate: dataElement => requestedIds.has(dataElement.id),
// $FlowIgnore - the enabled prop guarantees that dataElementIds will be defined
predicate: dataElement => dataElementIds.has(dataElement.id),
},
), [storageController]);

const { loading, data, error } = useQueryStyleEvaluation(getDataElements, dataElementIds);
), {
enabled,
},
);

return {
dataElements: data,
loading,
error,
isLoading,
isError,
};
};
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
// @flow
import { useCallback } from 'react';
import type { UseQueryOptions } from 'react-query';
import { userStores, getUserStorageController } from '../../storageControllers';
import { useQueryStyleEvaluation } from '../api/useQueryStyleEvaluation';
import { useIndexedDBQuery } from '../reactQueryHelpers';
import type { CachedOptionSet } from '../../storageControllers/';

export const useOptionSetsFromIndexedDB = (optionSetIds: ?Set<string>): {
export const useOptionSetsFromIndexedDB = (queryKey: Array<string | number>, optionSetIds: ?Set<string>, queryOptions?: UseQueryOptions<>): {
optionSets: ?Array<CachedOptionSet>,
loading: boolean,
error: any,
isLoading: boolean,
isError: boolean,
} => {
const storageController = getUserStorageController();
const { enabled = !!optionSetIds } = queryOptions ?? {};

const getOptionSets = useCallback(requestedIds =>
storageController.getAll(
const { data, isLoading, isError } = useIndexedDBQuery(
['optionSets', ...queryKey],
() => storageController.getAll(
userStores.OPTION_SETS, {
predicate: optionSet => requestedIds.has(optionSet.id),
// $FlowIgnore - the enabled prop guarantees that optionSetIds will be defined
predicate: optionSet => optionSetIds.has(optionSet.id),
},
), [storageController]);

const { loading, data, error } = useQueryStyleEvaluation(getOptionSets, optionSetIds);
), {
enabled,
},
);

return {
optionSets: data,
loading,
error,
isLoading,
isError,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useDataEngine, type ResourceQuery } from '@dhis2/app-runtime';
import type { QueryFunction, UseQueryOptions } from 'react-query';
import { IndexedDBError } from '../../../../capture-core-utils/storage/IndexedDBError/IndexedDBError';
import type { Result } from './useMetadataQuery.types';
import { ReactQueryAppNamespace } from '../reactQueryHelpers.const';
import { ReactQueryAppNamespace, IndexedDBNamespace } from '../reactQueryHelpers.const';

const throwErrorForIndexedDB = (error) => {
if (error instanceof IndexedDBError) {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const useIndexedDBQuery = <TResultData>(
queryFn: QueryFunction<TResultData>,
queryOptions?: UseQueryOptions<TResultData>,
): Result<TResultData> =>
useAsyncMetadata(queryKey, queryFn, {
useAsyncMetadata([IndexedDBNamespace, ...queryKey], queryFn, {
cacheTime: 0,
...queryOptions,
onError: (error) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// @flow

export const ReactQueryAppNamespace = 'capture';
export const IndexedDBNamespace = 'indexedDB';

0 comments on commit 91c56e7

Please sign in to comment.