diff --git a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js index a56b467acf..7290bb6ac9 100644 --- a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js +++ b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js @@ -8,11 +8,26 @@ import { withLoadingIndicator } from '../../../HOC'; const styles = {}; export const StagesPlain = ({ stages, events, classes, ...passOnProps }: PlainProps) => { - const eventsByStage = useMemo(() => stages.reduce( - (acc, stage) => { - acc[stage.id] = events.filter(event => event.programStage === stage.id); - return acc; - }, {}), [stages, events], + const eventsByStage = useMemo( + () => stages.reduce( + (acc, stage) => { + acc[stage.id] = acc[stage.id] || []; + return acc; + }, + events.reduce( + (acc, event) => { + const stageId = event.programStage; + if (acc[stageId]) { + acc[stageId].push(event); + } else { + acc[stageId] = [event]; + } + return acc; + }, + {}, + ), + ), + [stages, events], ); return (<>