From 3bf3d812acae1ab500bcfb0dd8eed37d2f1c21ea Mon Sep 17 00:00:00 2001 From: Jannik Stehle Date: Tue, 16 Jan 2024 11:02:04 +0100 Subject: [PATCH] refactor: remove pasteSelectedFiles from vuex files store --- packages/web-app-files/src/store/actions.ts | 62 +------------------ .../components/AppBar/CreateAndUpload.spec.ts | 20 ++++-- .../actions/files/useFileActionsPaste.ts | 61 ++++++++++++++---- .../resource/conflictHandling/transfer.ts | 5 +- .../src/mocks/store/filesModuleMockOptions.ts | 1 - 5 files changed, 68 insertions(+), 81 deletions(-) diff --git a/packages/web-app-files/src/store/actions.ts b/packages/web-app-files/src/store/actions.ts index 437377ba2b8..0cd7229e1bb 100644 --- a/packages/web-app-files/src/store/actions.ts +++ b/packages/web-app-files/src/store/actions.ts @@ -1,13 +1,6 @@ import PQueue from 'p-queue' -import { - MessageStore, - CapabilityStore, - getParentPaths, - ConfigStore, - ResourceTransfer, - TransferType -} from '@ownclouders/web-pkg' +import { MessageStore, CapabilityStore, ConfigStore, getParentPaths } from '@ownclouders/web-pkg' import { buildShare, buildCollaboratorShare, @@ -16,7 +9,6 @@ import { import { avatarUrl } from '../helpers/user' import { has } from 'lodash-es' import get from 'lodash-es/get' -import { ClipboardActions } from '@ownclouders/web-pkg' import { buildResource, isProjectSpaceResource, @@ -51,58 +43,6 @@ export default { context.commit('ADD_FILE_SELECTION', file) } }, - pasteSelectedFiles( - context, - { - targetSpace, - clientService, - loadingService, - $gettext, - $ngettext, - sourceSpace, - resources, - clipboardStore - } - ) { - const copyMove = new ResourceTransfer( - sourceSpace, - resources, - targetSpace, - context.state.currentFolder, - clientService, - loadingService, - $gettext, - $ngettext - ) - let movedResourcesPromise - if (clipboardStore.action === ClipboardActions.Cut) { - movedResourcesPromise = copyMove.perform(TransferType.MOVE) - } - if (clipboardStore.action === ClipboardActions.Copy) { - movedResourcesPromise = copyMove.perform(TransferType.COPY) - } - return movedResourcesPromise.then((movedResources) => { - const loadingResources = [] - const fetchedResources = [] - for (const resource of movedResources) { - loadingResources.push( - (async () => { - const movedResource = await (clientService.webdav as WebDAV).getFileInfo( - targetSpace, - resource - ) - fetchedResources.push(movedResource) - })() - ) - } - - return Promise.all(loadingResources).then(() => { - const currentFolder = context.getters.currentFolder - context.commit('UPSERT_RESOURCES', fetchedResources) - context.commit('LOAD_INDICATORS', currentFolder.path) - }) - }) - }, resetFileSelection(context) { context.commit('RESET_SELECTION') }, diff --git a/packages/web-app-files/tests/unit/components/AppBar/CreateAndUpload.spec.ts b/packages/web-app-files/tests/unit/components/AppBar/CreateAndUpload.spec.ts index 17c30e9f295..6f502bf8993 100644 --- a/packages/web-app-files/tests/unit/components/AppBar/CreateAndUpload.spec.ts +++ b/packages/web-app-files/tests/unit/components/AppBar/CreateAndUpload.spec.ts @@ -8,7 +8,8 @@ import { useRequest, useSpacesStore, CapabilityStore, - useClipboardStore + useClipboardStore, + useFileActionsPaste } from '@ownclouders/web-pkg' import { eventBus, UppyResource } from '@ownclouders/web-pkg' import { @@ -28,7 +29,8 @@ jest.mock('@ownclouders/web-pkg', () => ({ useExtensionRegistry: jest.fn(), useRequest: jest.fn(), useFileActionsCreateNewFile: jest.fn(), - useFileActions: jest.fn() + useFileActions: jest.fn(), + useFileActionsPaste: jest.fn() })) const elSelector = { @@ -106,7 +108,7 @@ describe('CreateAndUpload component', () => { expect(wrapper.findAll(`${elSelector.clipboardBtns} .oc-button`).length).toBe(2) }) it('call the "paste files"-action', async () => { - const { wrapper, storeOptions } = getWrapper({ + const { wrapper, mocks } = getWrapper({ clipboardResources: [ mock({ shareRoot: undefined @@ -114,7 +116,7 @@ describe('CreateAndUpload component', () => { ] }) await wrapper.find(elSelector.pasteFilesBtn).trigger('click') - expect(storeOptions.modules.Files.actions.pasteSelectedFiles).toHaveBeenCalled() + expect(mocks.pasteActionHandler).toHaveBeenCalled() }) it('call "clear clipboard"-action', async () => { const { wrapper } = getWrapper({ clipboardResources: [mock()] }) @@ -196,13 +198,21 @@ function getWrapper({ }) ) + const pasteActionHandler = jest.fn() + jest.mocked(useFileActionsPaste).mockReturnValue( + mock>({ + actions: ref([mock({ handler: pasteActionHandler })]) + }) + ) + const storeOptions = { ...defaultStoreMockOptions } storeOptions.modules.Files.state.areFileExtensionsShown = areFileExtensionsShown storeOptions.modules.Files.getters.currentFolder.mockImplementation(() => currentFolder) storeOptions.modules.Files.getters.files.mockImplementation(() => files) const store = createStore(storeOptions) const mocks = { - ...defaultComponentMocks({ currentRoute: mock({ name: currentRouteName }) }) + ...defaultComponentMocks({ currentRoute: mock({ name: currentRouteName }) }), + pasteActionHandler } const capabilities = { spaces: { enabled: true }, diff --git a/packages/web-pkg/src/composables/actions/files/useFileActionsPaste.ts b/packages/web-pkg/src/composables/actions/files/useFileActionsPaste.ts index 48821183827..a2bdf451b9e 100644 --- a/packages/web-pkg/src/composables/actions/files/useFileActionsPaste.ts +++ b/packages/web-pkg/src/composables/actions/files/useFileActionsPaste.ts @@ -14,6 +14,7 @@ import { useStore } from '../../store' import { FileAction, FileActionOptions } from '../types' import { Resource, SpaceResource } from '@ownclouders/web-client' import { useClipboardStore } from '../../piniaStores' +import { ClipboardActions, ResourceTransfer, TransferType } from '../../../helpers' export const useFileActionsPaste = ({ store }: { store?: Store } = {}) => { store = store || useStore() @@ -35,6 +36,53 @@ export const useFileActionsPaste = ({ store }: { store?: Store } = {}) => { return $gettext('Ctrl + V') }) + const currentFolder = computed(() => store.getters['Files/currentFolder']) + + const pasteSelectedFiles = ({ + targetSpace, + sourceSpace, + resources + }: { + targetSpace: SpaceResource + sourceSpace: SpaceResource + resources: Resource[] + }) => { + const copyMove = new ResourceTransfer( + sourceSpace, + resources, + targetSpace, + unref(currentFolder), + clientService, + loadingService, + $gettext, + $ngettext + ) + let movedResourcesPromise: Promise + if (clipboardStore.action === ClipboardActions.Cut) { + movedResourcesPromise = copyMove.perform(TransferType.MOVE) + } + if (clipboardStore.action === ClipboardActions.Copy) { + movedResourcesPromise = copyMove.perform(TransferType.COPY) + } + return movedResourcesPromise.then((movedResources) => { + const loadingResources = [] + const fetchedResources: Resource[] = [] + for (const resource of movedResources) { + loadingResources.push( + (async () => { + const movedResource = await clientService.webdav.getFileInfo(targetSpace, resource) + fetchedResources.push(movedResource) + })() + ) + } + + return Promise.all(loadingResources).then(() => { + store.commit('Files/UPSERT_RESOURCES', fetchedResources) + store.commit('Files/LOAD_INDICATORS', unref(currentFolder).path) + }) + }) + } + const handler = async ({ space: targetSpace }: FileActionOptions) => { const resourceSpaceMapping: Record = clipboardStore.resources.reduce((acc, resource) => { @@ -52,19 +100,10 @@ export const useFileActionsPaste = ({ store }: { store?: Store } = {}) => { acc[matchingSpace.id].resources.push(resource) return acc }, {}) + const promises = Object.values(resourceSpaceMapping).map( ({ space: sourceSpace, resources: resourcesToCopy }) => { - // TODO: move away from store, no need to sit there - return store.dispatch('Files/pasteSelectedFiles', { - targetSpace, - sourceSpace: sourceSpace, - resources: resourcesToCopy, - clientService, - loadingService, - $gettext, - $ngettext, - clipboardStore - }) + return pasteSelectedFiles({ targetSpace, sourceSpace, resources: resourcesToCopy }) } ) await Promise.all(promises) diff --git a/packages/web-pkg/src/helpers/resource/conflictHandling/transfer.ts b/packages/web-pkg/src/helpers/resource/conflictHandling/transfer.ts index dd93b11fda8..8fb091c503a 100644 --- a/packages/web-pkg/src/helpers/resource/conflictHandling/transfer.ts +++ b/packages/web-pkg/src/helpers/resource/conflictHandling/transfer.ts @@ -160,9 +160,8 @@ export class ResourceTransfer extends ConflictDialog { let targetName = resource.name let overwriteTarget = false if (hasConflict) { - const resolveStrategy = resolvedConflicts.find( - (e) => e.resource.id === resource.id - )?.strategy + const resolveStrategy = resolvedConflicts.find((e) => e.resource.id === resource.id) + ?.strategy if (resolveStrategy === ResolveStrategy.SKIP) { continue } diff --git a/packages/web-test-helpers/src/mocks/store/filesModuleMockOptions.ts b/packages/web-test-helpers/src/mocks/store/filesModuleMockOptions.ts index 5117f2891f6..4eace4cdecc 100644 --- a/packages/web-test-helpers/src/mocks/store/filesModuleMockOptions.ts +++ b/packages/web-test-helpers/src/mocks/store/filesModuleMockOptions.ts @@ -38,7 +38,6 @@ export const filesModuleMockOptions = { }, actions: { deleteFiles: jest.fn(), - pasteSelectedFiles: jest.fn(), loadIndicators: jest.fn(), loadVersions: jest.fn(), loadShares: jest.fn(),