fix: depth in collection items

This commit is contained in:
Anoop M D 2022-03-18 22:48:05 +05:30
parent ce02906070
commit c1e93915d4
2 changed files with 23 additions and 3 deletions

View File

@ -1,9 +1,10 @@
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import each from 'lodash/each';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { createSlice } from '@reduxjs/toolkit' import { createSlice } from '@reduxjs/toolkit'
import { getCollectionsFromIdb, saveCollectionToIdb } from 'utils/idb'; import { getCollectionsFromIdb, saveCollectionToIdb } from 'utils/idb';
import { sendNetworkRequest } from 'utils/network'; import { sendNetworkRequest } from 'utils/network';
import { findCollectionByUid, findItemInCollection, cloneItem, transformCollectionToSaveToIdb } from 'utils/collections'; import { findCollectionByUid, findItemInCollection, cloneItem, transformCollectionToSaveToIdb, addDepth } from 'utils/collections';
// todo: errors should be tracked in each slice and displayed as toasts // todo: errors should be tracked in each slice and displayed as toasts
@ -16,7 +17,8 @@ export const collectionsSlice = createSlice({
initialState, initialState,
reducers: { reducers: {
_loadCollections: (state, action) => { _loadCollections: (state, action) => {
state.collections = action.payload; each(action.payload.collections, (c) => addDepth(c.items));
state.collections = action.payload.collections;
}, },
_createCollection: (state, action) => { _createCollection: (state, action) => {
state.collections.push(action.payload); state.collections.push(action.payload);
@ -65,6 +67,7 @@ export const collectionsSlice = createSlice({
type: 'folder', type: 'folder',
items: [] items: []
}); });
addDepth(collection.items);
} }
}, },
_newRequest: (state, action) => { _newRequest: (state, action) => {
@ -72,6 +75,7 @@ export const collectionsSlice = createSlice({
if(collection) { if(collection) {
collection.items.push(action.payload.item); collection.items.push(action.payload.item);
addDepth(collection.items);
} }
}, },
collectionClicked: (state, action) => { collectionClicked: (state, action) => {
@ -112,7 +116,9 @@ export const {
export const loadCollectionsFromIdb = () => (dispatch) => { export const loadCollectionsFromIdb = () => (dispatch) => {
getCollectionsFromIdb(window.__idb) getCollectionsFromIdb(window.__idb)
.then((collections) => dispatch(_loadCollections(collections))) .then((collections) => dispatch(_loadCollections({
collections: collections
})))
.catch((err) => console.log(err)); .catch((err) => console.log(err));
}; };

View File

@ -2,6 +2,20 @@ import each from 'lodash/each';
import find from 'lodash/find'; import find from 'lodash/find';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
export const addDepth = (items = []) => {
const depth = (itms, initialDepth) => {
each(itms, (i) => {
i.depth = initialDepth;
if(i.items && i.items.length) {
depth(i.items, initialDepth + 1);
}
})
}
depth(items, 1);
};
export const flattenItems = (items = []) => { export const flattenItems = (items = []) => {
const flattenedItems = []; const flattenedItems = [];