mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-16 10:03:51 -07:00
Use createItemMap like Sonarr
(cherry picked from commit 43d04cd54efcf5569cb316a5856878d4291f96be)
This commit is contained in:
parent
014eb27a26
commit
89cbf0c022
1 changed files with 12 additions and 6 deletions
|
@ -16,6 +16,13 @@ const blacklistedProperties = [
|
||||||
'id'
|
'id'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function createItemMap(data) {
|
||||||
|
return data.reduce((acc, d, index) => {
|
||||||
|
acc[d.id] = index;
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
|
||||||
export default function createHandleActions(handlers, defaultState, section) {
|
export default function createHandleActions(handlers, defaultState, section) {
|
||||||
return handleActions({
|
return handleActions({
|
||||||
|
|
||||||
|
@ -42,7 +49,7 @@ export default function createHandleActions(handlers, defaultState, section) {
|
||||||
|
|
||||||
if (_.isArray(payload.data)) {
|
if (_.isArray(payload.data)) {
|
||||||
newState.items = payload.data;
|
newState.items = payload.data;
|
||||||
newState.itemMap = _.zipObject(_.map(payload.data, 'id'), _.range(payload.data.length));
|
newState.itemMap = createItemMap(payload.data);
|
||||||
} else {
|
} else {
|
||||||
newState.item = payload.data;
|
newState.item = payload.data;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +74,7 @@ export default function createHandleActions(handlers, defaultState, section) {
|
||||||
const items = newState.items;
|
const items = newState.items;
|
||||||
|
|
||||||
if (!newState.itemMap) {
|
if (!newState.itemMap) {
|
||||||
newState.itemMap = _.zipObject(_.map(items, 'id'), _.range(items.length));
|
newState.itemMap = createItemMap(payload.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
const index = payload.id in newState.itemMap ? newState.itemMap[payload.id] : -1;
|
const index = payload.id in newState.itemMap ? newState.itemMap[payload.id] : -1;
|
||||||
|
@ -87,9 +94,8 @@ export default function createHandleActions(handlers, defaultState, section) {
|
||||||
|
|
||||||
newState.items.splice(index, 1, newItem);
|
newState.items.splice(index, 1, newItem);
|
||||||
} else if (!updateOnly) {
|
} else if (!updateOnly) {
|
||||||
const newIndex = newState.items.push({ ...otherProps }) - 1;
|
newState.items.push({ ...otherProps });
|
||||||
|
newState.itemMap = createItemMap(newState.items);
|
||||||
newState.itemMap[payload.id] = newIndex;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return updateSectionState(state, payloadSection, newState);
|
return updateSectionState(state, payloadSection, newState);
|
||||||
|
@ -126,7 +132,7 @@ export default function createHandleActions(handlers, defaultState, section) {
|
||||||
newState.items = [...newState.items];
|
newState.items = [...newState.items];
|
||||||
_.remove(newState.items, { id: payload.id });
|
_.remove(newState.items, { id: payload.id });
|
||||||
|
|
||||||
newState.itemMap = _.zipObject(_.map(newState.items, 'id'), _.range(newState.items.length));
|
newState.itemMap = createItemMap(newState.items);
|
||||||
|
|
||||||
return updateSectionState(state, payloadSection, newState);
|
return updateSectionState(state, payloadSection, newState);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue