Skip to content

Commit

Permalink
refactor: remove pasteSelectedFiles from vuex files store
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Jan 16, 2024
1 parent c6dea16 commit 3bf3d81
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 81 deletions.
62 changes: 1 addition & 61 deletions packages/web-app-files/src/store/actions.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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')
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
useRequest,
useSpacesStore,
CapabilityStore,
useClipboardStore
useClipboardStore,
useFileActionsPaste
} from '@ownclouders/web-pkg'
import { eventBus, UppyResource } from '@ownclouders/web-pkg'
import {
Expand All @@ -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 = {
Expand Down Expand Up @@ -106,15 +108,15 @@ 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<Resource>({
shareRoot: undefined
})
]
})
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<Resource>()] })
Expand Down Expand Up @@ -196,13 +198,21 @@ function getWrapper({
})
)

const pasteActionHandler = jest.fn()
jest.mocked(useFileActionsPaste).mockReturnValue(
mock<ReturnType<typeof useFileActionsPaste>>({
actions: ref([mock<FileAction>({ 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<RouteLocation>({ name: currentRouteName }) })
...defaultComponentMocks({ currentRoute: mock<RouteLocation>({ name: currentRouteName }) }),
pasteActionHandler
}
const capabilities = {
spaces: { enabled: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> } = {}) => {
store = store || useStore()
Expand All @@ -35,6 +36,53 @@ export const useFileActionsPaste = ({ store }: { store?: Store<any> } = {}) => {
return $gettext('Ctrl + V')
})

const currentFolder = computed<Resource>(() => 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<Resource[]>
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<string, { space: SpaceResource; resources: Resource[] }> =
clipboardStore.resources.reduce((acc, resource) => {
Expand All @@ -52,19 +100,10 @@ export const useFileActionsPaste = ({ store }: { store?: Store<any> } = {}) => {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export const filesModuleMockOptions = {
},
actions: {
deleteFiles: jest.fn(),
pasteSelectedFiles: jest.fn(),
loadIndicators: jest.fn(),
loadVersions: jest.fn(),
loadShares: jest.fn(),
Expand Down

0 comments on commit 3bf3d81

Please sign in to comment.