From 209a150216b8713097adb2bcf451cbda246bda16 Mon Sep 17 00:00:00 2001 From: NikolaBorislavovHristov Date: Mon, 25 Nov 2019 15:57:12 +0200 Subject: [PATCH] auto select group only if user not selected already --- src/routes/MetaDetails/useSelectableGroups.js | 81 +++++++++++-------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/src/routes/MetaDetails/useSelectableGroups.js b/src/routes/MetaDetails/useSelectableGroups.js index 37a22c825..1248beb8c 100644 --- a/src/routes/MetaDetails/useSelectableGroups.js +++ b/src/routes/MetaDetails/useSelectableGroups.js @@ -1,44 +1,50 @@ const React = require('react'); +const isEqual = require('lodash.isequal'); + +const readyGroupForReq = (groups, req) => { + return groups.find((group) => { + return isEqual(group.req, req) && group.content.type === 'Ready'; + }); +}; const reducer = (state, action) => { switch (action.type) { case 'groups-changed': { - if (state.selectedGroup !== null) { - const selectedGroupIncluded = action.groups.some((group) => { - return group.req.base === state.selectedGroup.req.base && - group.content.type === 'Ready'; - }); - if (selectedGroupIncluded) { - return { - ...state, - resourceRef: action.resourceRef, - groups: action.groups - }; - } - } - - const readyGroup = action.groups.find((group) => group.content.type === 'Ready'); - const selectedGroup = readyGroup ? readyGroup : null; - return { - ...state, - resourceRef: action.resourceRef, - groups: action.groups, - selectedGroup - }; - } - case 'group-selected': { - const selectedGroup = state.groups.find((group) => { - return group.req.base === action.base && - group.content.type === 'Ready'; - }); - if (selectedGroup) { + if (state.selected.group === null || + !state.selected.byUser || + !readyGroupForReq(action.groups, state.selected.group.req)) { + const firstReadyGroup = action.groups.find((group) => group.content.type === 'Ready'); + const selectedGroup = firstReadyGroup ? firstReadyGroup : null; return { ...state, - selectedGroup + resourceRef: action.resourceRef, + groups: action.groups, + selected: { + group: selectedGroup, + byUser: false + } }; } - return state; + return { + ...state, + resourceRef: action.resourceRef, + groups: action.groups + }; + } + case 'group-selected': { + const selectedGroup = readyGroupForReq(state.groups, action.req); + if (!selectedGroup) { + return state; + } + + return { + ...state, + selected: { + group: selectedGroup, + byUser: true + } + }; } default: { return state; @@ -50,7 +56,10 @@ const initializer = ([resourceRef, groups]) => { const initialState = { resourceRef: null, groups: [], - selectedGroup: null + selected: { + group: null, + byUser: false + } }; const initAction = { type: 'groups-changed', @@ -67,6 +76,12 @@ const useSelectableGroups = (resourceRef, groups) => { [resourceRef, groups], initializer ); + const selectGroup = React.useCallback((req) => { + dispatch({ + type: 'group-selected', + req + }); + }, []); React.useEffect(() => { dispatch({ type: 'groups-changed', @@ -74,7 +89,7 @@ const useSelectableGroups = (resourceRef, groups) => { groups }); }, [groups]); - return [state.resourceRef, state.groups, state.selectedGroup]; + return [state.resourceRef, state.groups, state.selected.group, selectGroup]; }; module.exports = useSelectableGroups;