2020-11-26 09:22:30 -05:00
|
|
|
import { findInTreeOrThrow } from './find-in-tree'
|
|
|
|
|
|
|
|
export function renameInTree(tree, id, { newName }) {
|
|
|
|
return mutateInTree(tree, id, (parent, entity, index) => {
|
|
|
|
const newParent = Object.assign([], parent)
|
|
|
|
const newEntity = {
|
|
|
|
...entity,
|
2021-04-27 03:52:58 -04:00
|
|
|
name: newName,
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
newParent[index] = newEntity
|
|
|
|
return newParent
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteInTree(tree, id) {
|
|
|
|
return mutateInTree(tree, id, (parent, entity, index) => {
|
|
|
|
return [...parent.slice(0, index), ...parent.slice(index + 1)]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function moveInTree(tree, entityId, toFolderId) {
|
|
|
|
const found = findInTreeOrThrow(tree, entityId)
|
|
|
|
if (found.parentFolderId === toFolderId) {
|
|
|
|
// nothing to do (the entity was probably already moved)
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
const newFileTreeData = deleteInTree(tree, entityId)
|
|
|
|
return createEntityInTree(newFileTreeData, toFolderId, {
|
|
|
|
...found.entity,
|
2021-04-27 03:52:58 -04:00
|
|
|
type: found.type,
|
2020-11-26 09:22:30 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createEntityInTree(tree, parentFolderId, newEntityData) {
|
|
|
|
const { type, ...newEntity } = newEntityData
|
|
|
|
if (!type) throw new Error('Entity has no type')
|
|
|
|
const entityType = `${type}s`
|
|
|
|
|
|
|
|
return mutateInTree(tree, parentFolderId, (parent, folder, index) => {
|
|
|
|
parent[index] = {
|
|
|
|
...folder,
|
2021-04-27 03:52:58 -04:00
|
|
|
[entityType]: [...folder[entityType], newEntity],
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
return parent
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function mutateInTree(tree, id, mutationFunction) {
|
2023-10-26 06:01:08 -04:00
|
|
|
if (!id || tree._id === id) {
|
2020-11-26 09:22:30 -05:00
|
|
|
// covers the root folder case: it has no parent so in order to use
|
|
|
|
// mutationFunction we pass an empty array as the parent and return the
|
|
|
|
// mutated tree directly
|
|
|
|
const [newTree] = mutationFunction([], tree, 0)
|
|
|
|
return newTree
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const entityType of ['docs', 'fileRefs', 'folders']) {
|
|
|
|
for (let index = 0; index < tree[entityType].length; index++) {
|
|
|
|
const entity = tree[entityType][index]
|
|
|
|
if (entity._id === id) {
|
|
|
|
return {
|
|
|
|
...tree,
|
2021-04-27 03:52:58 -04:00
|
|
|
[entityType]: mutationFunction(tree[entityType], entity, index),
|
2020-11-26 09:22:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const newFolders = tree.folders.map(folder =>
|
|
|
|
mutateInTree(folder, id, mutationFunction)
|
|
|
|
)
|
|
|
|
|
|
|
|
return { ...tree, folders: newFolders }
|
|
|
|
}
|