mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
auto select group only if user not selected already
This commit is contained in:
parent
26d9d38f89
commit
209a150216
1 changed files with 48 additions and 33 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue