Skip to content

Commit

Permalink
fix cache updates when adding/removing agent episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukavdplas committed Sep 19, 2024
1 parent 44aeda8 commit ba911fd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { FormStatus } from '../../shared/types';
import { MutationResult } from 'apollo-angular';
import { differenceBy } from '@shared/utils';

const REFETCH_QUERIES = ['DataEntryAgentEpisodes'];

@Component({
selector: 'lc-agent-episodes-form',
Expand Down Expand Up @@ -83,7 +82,7 @@ export class AgentEpisodesFormComponent implements OnDestroy {
entityType: Entity.Agent,
};
this.addMutation.mutate({ input }, {
refetchQueries: REFETCH_QUERIES,
update: (cache) => this.updateCache(episodeID, agentID, cache),
}).pipe(
tap(() => this.status$.next('loading'))
).subscribe(this.mutationObserver);
Expand All @@ -95,7 +94,9 @@ export class AgentEpisodesFormComponent implements OnDestroy {
episode: episodeID,
entityType: Entity.Agent,
};
this.removeMutation.mutate(data, { refetchQueries: REFETCH_QUERIES }).pipe(
this.removeMutation.mutate(data, {
update: (cache) => this.updateCache(episodeID, agentID, cache),
}).pipe(
tap(() => this.status$.next('loading'))
).subscribe(this.mutationObserver)
}
Expand All @@ -109,6 +110,12 @@ export class AgentEpisodesFormComponent implements OnDestroy {
this.status$.next('error');
}

private updateCache(episodeID: string, agentID: string, cache: any) {
cache.evict({ id: cache.identify({ __typename: "EpisodeType", id: episodeID }) });
cache.evict({ id: cache.identify({ __typename: "AgentDescriptionType", id: agentID }) });
cache.gc();
}

private availableEpisodes(
data: DataEntryAgentEpisodesQuery
): { name: string, id: string }[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ export class EpisodeEntitiesFormComponent implements OnChanges, OnDestroy {
onError(error: any) {
console.error(error);
this.toastService.show({
header: 'Creating agent failed',
body: 'Could not create agent',
header: `Adding ${this.entityName} failed`,
body: `Could not add ${this.entityName}`,
type: 'danger',
})
this.status$.next('error');
Expand Down

0 comments on commit ba911fd

Please sign in to comment.