Skip to content

Commit

Permalink
Merge pull request ManageIQ#6319 from martinpovolny/toolbar_2nd_try
Browse files Browse the repository at this point in the history
Fix toolbar initialization.
  • Loading branch information
mzazrivec authored Oct 22, 2019
2 parents 83c4af9 + 28f7661 commit 612cb45
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/javascript/components/miq-toolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ const filterViews = toolbarItems => toolbarItems
.flat()
.filter(i => i && i.id && i.id.indexOf('view_') === 0);

const sanitizeToolbars = arr => arr ? arr.filter(Boolean) : [];

const toolbarReducer = (state, action) => {
switch (action.type) {
case 'INCREMENT':
Expand All @@ -200,7 +202,7 @@ const toolbarReducer = (state, action) => {
case 'TOOLBARS':
return {
...state,
toolbars: action.toolbars,
toolbars: sanitizeToolbars(action.toolbars),
};
default:
return state;
Expand All @@ -213,8 +215,8 @@ const initState = {
};

/* Wrapper class for generic toolbars and special toolbars. */
const MiqToolbar = ({ toolbars }) => {
const [state, dispatch] = useReducer(toolbarReducer, initState);
const MiqToolbar = ({ toolbars: initToolbars }) => {
const [state, dispatch] = useReducer(toolbarReducer, {...initState, toolbars: sanitizeToolbars(initToolbars)});

useEffect(() => {
// Initiall toolbars are given in props.
Expand All @@ -225,13 +227,15 @@ const MiqToolbar = ({ toolbars }) => {
return () => subscription.unsubscribe();
}, []);

const {count, toolbars} = state;

const renderGenericToolbar = () => {
const groups = separateItems(state.toolbars.filter(item => !!item));
const groups = separateItems(toolbars.filter(item => !!item));
const views = filterViews(groups);

return (
<Toolbar
count={state.count}
count={count}
groups={groups}
views={views}
onClick={onClick}
Expand All @@ -240,7 +244,7 @@ const MiqToolbar = ({ toolbars }) => {
);
};

if (!toolbars || (toolbars.length === 0)) {
if (toolbars.length === 0) {
return null;
}

Expand Down

0 comments on commit 612cb45

Please sign in to comment.