Skip to content

Commit

Permalink
fix: registration data cache invalidation
Browse files Browse the repository at this point in the history
AB#33731

This makes sure that we invalidate cache for registration queries that rely on referenceId, as well as the queries that rely on registration.id

This was causing issues because the new "getDuplicates" endpoint (implemented in #6522) uses referenceId, and the cache for it was not being invalidated after editing personal information.
  • Loading branch information
aberonni committed Feb 18, 2025
1 parent c70238d commit 877f784
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export class AddNoteFormComponent {
this.toastService.showToast({
detail: $localize`Note successfully added.`,
});
void this.registrationApiService.invalidateCache(
this.projectId,
this.registrationId,
);
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
registration: this.registration.data(),
});
this.formVisible.set(false);
},
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,28 @@ export class RegistrationApiService extends DomainApiService {
});
}

public invalidateCache(
projectId: Signal<number | string>,
registrationId?: Signal<number | string>,
): Promise<void> {
public async invalidateCache({
projectId,
registration,
}: {
projectId: Signal<number | string>;
registration?: Registration;
}): Promise<void> {
const path = [...BASE_ENDPOINT(projectId)];

if (registrationId) {
path.push(registrationId);
if (!registration) {
return this.queryClient.invalidateQueries({
queryKey: this.pathToQueryKey(path),
});
}

return this.queryClient.invalidateQueries({
queryKey: this.pathToQueryKey(path),
});
await Promise.all([
this.queryClient.invalidateQueries({
queryKey: this.pathToQueryKey([...path, registration.id]),
}),
this.queryClient.invalidateQueries({
queryKey: this.pathToQueryKey([...path, registration.referenceId]),
}),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { ConfirmationDialogComponent } from '~/components/confirmation-dialog/co
import { FormFieldWrapperComponent } from '~/components/form-field-wrapper/form-field-wrapper.component';
import { ProjectApiService } from '~/domains/project/project.api.service';
import { RegistrationApiService } from '~/domains/registration/registration.api.service';
import { Registration } from '~/domains/registration/registration.model';
import { ComponentCanDeactivate } from '~/guards/pending-changes.guard';
import {
NormalizedRegistrationAttribute,
Expand Down Expand Up @@ -75,7 +76,7 @@ export class EditPersonalInformationComponent
readonly registrationId = input.required<string>();
readonly attributeList = input.required<NormalizedRegistrationAttribute[]>();
readonly cancelEditing = output();
readonly registrationUpdated = output();
readonly registrationUpdated = output<Registration>();

readonly projectApiService = inject(ProjectApiService);
readonly registrationApiService = inject(RegistrationApiService);
Expand Down Expand Up @@ -175,15 +176,15 @@ export class EditPersonalInformationComponent
reason,
});
},
onSuccess: () => {
onSuccess: (patchedRegistration) => {
this.toastService.showToast({
detail: $localize`Personal information edited successfully.`,
});
void this.registrationApiService.invalidateCache(
this.projectId,
this.registrationId,
);
this.registrationUpdated.emit();
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
registration: patchedRegistration,
});
this.registrationUpdated.emit(patchedRegistration);
},
}));
ngOnInit() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
[registrationId]="registrationId()"
[attributeList]="registrationAttributes.data()"
(cancelEditing)="isEditing.set(false)"
(registrationUpdated)="onRegistrationUpdated()"
(registrationUpdated)="onRegistrationUpdated($event)"
/>
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from '~/components/data-list/data-list.component';
import { RegistrationPageLayoutComponent } from '~/components/registration-page-layout/registration-page-layout.component';
import { RegistrationApiService } from '~/domains/registration/registration.api.service';
import { Registration } from '~/domains/registration/registration.model';
import { ComponentCanDeactivate } from '~/guards/pending-changes.guard';
import { EditPersonalInformationComponent } from '~/pages/project-registration-personal-information/components/edit-personal-information/edit-personal-information.component';
import { AuthService } from '~/services/auth.service';
Expand Down Expand Up @@ -121,12 +122,12 @@ export class ProjectRegistrationPersonalInformationPageComponent
),
);

onRegistrationUpdated() {
onRegistrationUpdated(registration: Registration) {
this.isEditing.set(false);
void this.registrationApiService.invalidateCache(
this.projectId,
this.registrationId,
);
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
registration,
});
void this.registrationAttributes.refetch();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,15 @@ export class ChangeStatusDialogComponent
showSpinner: true,
});
this.actionComplete.emit();
void this.registrationApiService.invalidateCache(this.projectId);
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
});

setTimeout(() => {
// invalidate the cache again after a delay to try and make the status change reflected in the UI
void this.registrationApiService.invalidateCache(this.projectId);
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
});
}, 500);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ export class ImportRegistrationsComponent {
});
},
onSuccess: () => {
void this.registrationApiService.invalidateCache(this.projectId);
void this.registrationApiService.invalidateCache({
projectId: this.projectId,
});
this.dialogVisible.set(false);
this.toastService.showToast({
summary: $localize`:@@import-registrations-success:Registration(s) imported successfully.`,
Expand Down

0 comments on commit 877f784

Please sign in to comment.