Skip to content

Commit

Permalink
refactor: controller statusSelectors (#5462)
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Makowski <[email protected]>
  • Loading branch information
petermakowski authored Jun 12, 2024
1 parent 12a73ad commit 914c7f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ export const ControllerActionFormWrapper = ({
const processingControllers = useSelector(
actionStatus ? statusSelectors[actionStatus] : () => []
);
const controllerSystemIds = controllers.map(({ system_id }) => system_id);
// The form expects one error, so we only show the latest error with the
// assumption that all selected controllers fail in the same way.
const errors = useSelector((state: RootState) =>
controllerSelectors.eventErrorsForControllers(
state,
controllers.map(({ system_id }) => system_id),
controllerSystemIds,
kebabToCamelCase(action)
)
)[0]?.error;
Expand Down
27 changes: 14 additions & 13 deletions src/app/store/controller/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ const processing = (state: RootState): Controller[ControllerMeta.PK][] =>
)
);

export const statusSelectors: {
[x: string]: Selector<RootState, Controller[]>;
} = {};

// Create a selector for each controller status.
ACTIONS.forEach(({ status }) => {
statusSelectors[status] = createSelector(
[defaultSelectors.all, statuses],
(controllers: Controller[], statuses: ControllerStatuses) =>
controllers.filter(
({ system_id }) => statuses[system_id][status as keyof ControllerStatus]
)
);
});
export const statusSelectors = ACTIONS.reduce(
(selectors, { status }) => {
selectors[status] = createSelector(
[defaultSelectors.all, statuses],
(controllers: Controller[], statuses: ControllerStatuses) =>
controllers.filter(
({ system_id }) =>
statuses[system_id][status as keyof ControllerStatus]
)
);
return selectors;
},
{} as { [x: string]: Selector<RootState, Controller[]> }
);

/**
* Get the statuses for a controller.
Expand Down

0 comments on commit 914c7f0

Please sign in to comment.