Skip to content

Commit

Permalink
Merge pull request #569 from prezly/feature/care-6013-coverage-card-d…
Browse files Browse the repository at this point in the history
…ata-not-updated-immediately

[CARE-6013] Feature - Refetch coverage when update event happens
  • Loading branch information
kudlajz authored Sep 17, 2024
2 parents 0dd7b05 + 8d6df46 commit a58c9b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ export function CoverageElement({
}
}

useEffect(() => {
function handleCoverageUpdated({ coverage }: { coverage: Pick<CoverageEntry, 'id'>}) {
if (element.coverage.id === coverage.id) {
loadCoverage();
}
}

return EventsEditor.addEventListener(editor, 'coverage-updated', handleCoverageUpdated);
}, []);

return (
<EditorBlock
{...attributes}
Expand Down
8 changes: 6 additions & 2 deletions packages/slate-editor/src/modules/events/EventsEditor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { RemoveListener } from '@prezly/events';
import { Events } from '@prezly/events';
import { noop } from '@technically/lodash';
import { Editor } from 'slate';

import { EVENTS_PROPERTY } from './constants';
Expand All @@ -10,10 +12,12 @@ export abstract class EventsEditor {
editor: Editor,
event: Event,
listener: EditorEventHandlers[Event],
): void {
): RemoveListener {
if (EventsEditor.isEventsEditor(editor)) {
editor[EVENTS_PROPERTY].addEventListener(event, listener);
return editor[EVENTS_PROPERTY].addEventListener(event, listener);
}

return noop;
}

static dispatchEvent<Event extends keyof EditorEventMap>(
Expand Down
3 changes: 3 additions & 0 deletions packages/slate-editor/src/modules/events/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export type EditorEventMap = {
'coverage-placeholder-submitted': {
coverage: Pick<CoverageEntry, 'id'>;
};
'coverage-updated': {
coverage: Pick<CoverageEntry, 'id'>;
};
'coverage-removed': {
uuid: string;
};
Expand Down

0 comments on commit a58c9b5

Please sign in to comment.