Skip to content

Commit

Permalink
feat: update caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Nov 4, 2024
1 parent 09f985c commit 3639ab7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const useBulkCompleteEvents = ({
},
{
enabled: Object.keys(selectedRows).length > 0 && isCompleteDialogOpen,
staleTime: 0,
cacheTime: 0,
select: (data: any) => {
const apiEvents = handleAPIResponse(REQUESTED_ENTITIES.events, data);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { useEffect, useMemo } from 'react';
import { useAlert, useConfig, useDataEngine } from '@dhis2/app-runtime';
import { useMutation } from 'react-query';
import { useMutation, useQueryClient } from 'react-query';
import i18n from '@dhis2/d2-i18n';
import log from 'loglevel';
import { useApiDataQuery } from '../../../../../../../utils/reactQueryHelpers';
import { ReactQueryAppNamespace, useApiDataQuery } from '../../../../../../../utils/reactQueryHelpers';
import { handleAPIResponse, REQUESTED_ENTITIES } from '../../../../../../../utils/api';
import { errorCreator, FEATURES, hasAPISupportForFeature } from '../../../../../../../../capture-core-utils';
import type { ProgramStage } from '../../../../../../../metaData';
Expand Down Expand Up @@ -91,11 +91,24 @@ export const useCompleteBulkEnrollments = ({
}: Props) => {
const { serverVersion: { minor } } = useConfig();
const dataEngine = useDataEngine();
const queryClient = useQueryClient();
const { show: showAlert } = useAlert(
({ message }) => message,
{ critical: true },
);

const removeQueries = () => {
queryClient.removeQueries(
[
ReactQueryAppNamespace,
'WorkingLists',
'BulkActionBar',
'CompleteAction',
'trackedEntities',
],
);
};

const {
data: trackedEntities,
isError: isTrackedEntitiesError,
Expand Down Expand Up @@ -149,8 +162,10 @@ export const useCompleteBulkEnrollments = ({
{
onSuccess: () => {
onUpdateList();
removeQueries();
},
onError: (serverResponse, variables) => {
removeQueries();
showAlert({ message: i18n.t('An error occurred when completing the enrollments') });
log.error(
errorCreator('An error occurred when completing enrollments')({
Expand All @@ -172,6 +187,7 @@ export const useCompleteBulkEnrollments = ({
onSuccess: (serverResponse, { enrollments }) => {
const enrollmentIds = enrollments.map(enrollment => enrollment.trackedEntity);
removeRowsFromSelection(enrollmentIds);
removeQueries();
onUpdateList(true);
},
onError: (serverResponse, variables) => {
Expand Down

0 comments on commit 3639ab7

Please sign in to comment.