Repository URL to install this package:
|
Version:
3.12.20 ▾
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/* eslint-disable no-unused-vars */
import createThunk from '@filerobot/utils/lib/createThunk';
import isFolder from '@filerobot/utils/lib/isFolder';
import isFile from '@filerobot/utils/lib/isFile';
import { PERMISSIONS } from '@filerobot/utils/lib/constants';
import convertItemsToFilesFoldersUuids from '../utils/convertItemsToFilesFoldersUuids';
import convertItemsToFilesFoldersParentUuids from '../utils/convertItemstoFoldersParentUuids';
import getApiPathType from '../utils/getApiPathtype';
import getMoveAndDeleteData from '../utils/getMoveAndDeleteData';
import { foldersAdded, foldersRemoved, foldersUpdated, selectCurrentFolderPath, selectCurrentFolderUuid, selectFolderByUuid } from '../slices/folders.slice';
import { filesAdded, filesRemoved, filesUpdated, selectCurrentFileByUuid } from '../slices/files.slice';
import { selectionsCleared } from '../slices/selections.slice';
import { foldersTreeFolderUpdated, foldersTreeFoldersMoved, foldersTreeFoldersRemoved } from '../slices/foldersTree.slice';
import { selectIsSearchOrFiltersView } from '../slices/views.slice';
import { exactAndExtendedFilesRemoved } from '../slices/search.slice';
// TODO: Check this logic maybe we need to refactor it?
export var moveItemsFromDragging = createThunk(async function (_ref, thunkApi) {
var items = _ref.items,
toFolder = _ref.toFolder;
var state = thunkApi.getState();
var currentFolderPath = selectCurrentFolderPath(state);
var moveToFolder = toFolder || currentFolderPath;
if (moveToFolder.path && !items.some(function (item) {
return item.uuid === moveToFolder.uuid;
})) {
var _await$thunkApi$dispa = await thunkApi.dispatch(moveItems({
items: items,
toFolder: moveToFolder
})),
folders = _await$thunkApi$dispa.folders,
files = _await$thunkApi$dispa.files;
return {
folders: folders,
files: files
};
} else {
return thunkApi.rejectWithValue('INVALID_PATH');
}
});
// Either items or foldersUuids and filesUuids should be provided.
export var moveItems = createThunk(async function (_ref2, thunkApi) {
var items = _ref2.items,
_ref2$foldersUuids = _ref2.foldersUuids,
foldersUuids = _ref2$foldersUuids === void 0 ? [] : _ref2$foldersUuids,
_ref2$filesUuids = _ref2.filesUuids,
filesUuids = _ref2$filesUuids === void 0 ? [] : _ref2$filesUuids,
toFolder = _ref2.toFolder;
var _ref3 = items ? convertItemsToFilesFoldersUuids(items) : {
filesUuids: filesUuids,
foldersUuids: foldersUuids
},
_filesUuids = _ref3.filesUuids,
_foldersUuids = _ref3.foldersUuids;
var apiPathType = getApiPathType(_filesUuids, _foldersUuids);
var _getMoveAndDeleteData = getMoveAndDeleteData(apiPathType, _filesUuids, _foldersUuids),
fileObject = _getMoveAndDeleteData.fileObject,
folderObject = _getMoveAndDeleteData.folderObject;
var currentFolderPath = selectCurrentFolderPath(thunkApi.getState());
var currentFolderUuid = selectCurrentFolderUuid(thunkApi.getState());
var _convertItemsToFilesF = convertItemsToFilesFoldersParentUuids(items),
parentFolderUuids = _convertItemsToFilesF.foldersUuids;
var hasFolder = _foldersUuids.length > 0;
var hasFile = _filesUuids.length > 0;
if (currentFolderPath === toFolder.path && parentFolderUuids.includes(currentFolderUuid)) {
return thunkApi.rejectWithValue('SAME_PATH');
}
var _await$thunkApi$extra = await thunkApi.extra.apiClient.post("".concat(apiPathType, "/move"), {
data: _objectSpread(_objectSpread(_objectSpread({}, fileObject), folderObject), {}, {
new_folder_name: toFolder.path
}),
permissionToCheck: [hasFolder ? PERMISSIONS.FOLDER_MOVE : '', hasFile ? PERMISSIONS.FILE_MOVE : ''],
abortSignal: thunkApi.signal
}),
_await$thunkApi$extra2 = _await$thunkApi$extra.folders,
folders = _await$thunkApi$extra2 === void 0 ? [] : _await$thunkApi$extra2,
_await$thunkApi$extra3 = _await$thunkApi$extra.files,
files = _await$thunkApi$extra3 === void 0 ? [] : _await$thunkApi$extra3;
thunkApi.dispatch(selectionsCleared());
if (currentFolderPath !== toFolder.path) {
if (hasFolder) {
var _foldersUuids2 = folders.map(function (_ref4) {
var uuid = _ref4.uuid;
return uuid;
});
thunkApi.dispatch(foldersRemoved(_foldersUuids2));
thunkApi.dispatch(foldersTreeFoldersMoved({
foldersUuids: _foldersUuids2,
toFolder: toFolder
}));
}
if (hasFile) {
thunkApi.dispatch(filesRemoved(files.map(function (_ref5) {
var uuid = _ref5.uuid;
return uuid;
})));
}
} else {
if (hasFolder) {
thunkApi.dispatch(foldersAdded({
folders: folders
}));
thunkApi.dispatch(foldersTreeFoldersMoved({
foldersUuids: folders.map(function (_ref6) {
var uuid = _ref6.uuid;
return uuid;
}),
toFolder: toFolder
}));
}
if (hasFile) {
thunkApi.dispatch(filesAdded({
files: files
}));
}
}
return {
folders: folders,
files: files
};
});
export var deleteItems = createThunk(async function (_ref7, thunkApi) {
var filesUuids = _ref7.filesUuids,
foldersUuids = _ref7.foldersUuids;
var apiPathType = getApiPathType(filesUuids, foldersUuids);
var _getMoveAndDeleteData2 = getMoveAndDeleteData(apiPathType, filesUuids, foldersUuids),
fileObject = _getMoveAndDeleteData2.fileObject,
folderObject = _getMoveAndDeleteData2.folderObject;
var hasFolder = foldersUuids.length > 0;
var hasFile = filesUuids.length > 0;
var response = await thunkApi.extra.apiClient["delete"](apiPathType, {
data: _objectSpread(_objectSpread({}, fileObject), folderObject),
permissionToCheck: [hasFolder ? PERMISSIONS.FOLDER_DELETE : '', hasFile ? PERMISSIONS.FILE_DELETE : '']
});
thunkApi.dispatch(selectionsCleared());
if (hasFolder) {
thunkApi.dispatch(foldersRemoved(foldersUuids));
thunkApi.dispatch(foldersTreeFoldersRemoved(foldersUuids));
}
if (hasFile) {
// TODO: TO be improved, by getting discovered directly without the condition maybe if possible?
var actionFn = selectIsSearchOrFiltersView(thunkApi.getState()) ? exactAndExtendedFilesRemoved : filesRemoved;
thunkApi.dispatch(actionFn(filesUuids));
}
return {
foldersUuids: response.folders_uuids,
filesUuids: response.files_uuids
};
});
export var changeItemsVisibility = createThunk(async function (_ref8, thunkApi) {
var foldersUuids = _ref8.foldersUuids,
filesUuids = _ref8.filesUuids,
visibility = _ref8.visibility,
singleItem = _ref8.singleItem;
var foldersCount = foldersUuids ? foldersUuids.length : 0;
var filesCount = filesUuids ? filesUuids.length : 0;
// Avoid visibility save request for single item with not changed visibility
if (singleItem) {
var _singleItem$visibilit, _singleItem$visibilit2;
var oldVisibility = ((_singleItem$visibilit = singleItem.visibility) === null || _singleItem$visibilit === void 0 ? void 0 : (_singleItem$visibilit2 = _singleItem$visibilit.in_cdn) === null || _singleItem$visibilit2 === void 0 ? void 0 : _singleItem$visibilit2.set) || singleItem.visibility;
if (visibility === oldVisibility) {
var _isFolder = foldersCount === 1;
return _defineProperty({}, _isFolder ? 'folders' : 'files', singleItem);
}
}
// The request responsible for changing the items' visibility
var _await$thunkApi$extra4 = await thunkApi.extra.apiClient.put('objects/visibility/cdn', {
data: {
folders_uuids: foldersUuids,
files_uuids: filesUuids,
visibility: visibility
},
permissionToCheck: [foldersUuids ? PERMISSIONS.FOLDER_VISIBILITY : '', filesUuids ? PERMISSIONS.FILE_VISIBILITY : ''],
abortSignal: thunkApi.signal
}),
folders = _await$thunkApi$extra4.folders,
files = _await$thunkApi$extra4.files;
var results = {};
if (foldersCount > 0 && folders) {
thunkApi.dispatch(foldersUpdated(folders));
folders.forEach(function (folder) {
return thunkApi.dispatch(foldersTreeFolderUpdated({
uuid: folder.uuid,
visibility: folder.visibility
}));
});
results.folders = folders;
}
if (filesCount > 0 && files) {
thunkApi.dispatch(filesUpdated(files));
results.files = files;
}
return results;
});
export var getItemByUuid = createThunk(function (uuid, thunkApi) {
var state = thunkApi.getState();
var folder = selectFolderByUuid(state, uuid);
var file = selectCurrentFileByUuid(state, uuid);
return {
item: folder || file,
isFolder: !!folder
};
});