Skip to content

Commit

Permalink
@uppy/transloadit: close assembly if upload is cancelled (#3591)
Browse files Browse the repository at this point in the history
Fixes: #3156
  • Loading branch information
aduh95 authored Mar 24, 2022
1 parent 0033b9f commit aba7b29
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
27 changes: 27 additions & 0 deletions e2e/cypress/integration/dashboard-transloadit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,31 @@ describe('Dashboard with Transloadit', () => {

cy.get('.uppy-StatusBar-statusPrimary').should('contain', 'Complete')
})

it('should close assembly polling when cancelled', () => {
cy.get('@file-input').attachFile(['images/cat.jpg', 'images/traffic.jpg'])
cy.get('.uppy-StatusBar-actionBtn--upload').click()

cy.intercept({
method: 'GET',
url: '/assemblies/*',
}).as('assemblyPolling')
cy.intercept(
{ method: 'PATCH', pathname: '/files/*', times: 1 },
{ statusCode: 204, body: {} },
)
cy.intercept(
{ method: 'DELETE', pathname: '/resumable/files/*', times: 1 },
{ statusCode: 204, body: {} },
)
cy.wait('@assemblyPolling')
cy.window().then(({ uppy }) => {
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).every((a: any) => a.pollInterval)).to.equal(true)
})
cy.get('button[data-cy=cancel]').click()

cy.window().then(({ uppy }) => {
expect(Object.values(uppy.getPlugin('Transloadit').activeAssemblies).some((a: any) => a.pollInterval)).to.equal(false)
})
})
})
4 changes: 2 additions & 2 deletions e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"target": "es2020",
"lib": ["es2020", "dom"],
"types": ["cypress", "cypress-file-upload"]
},
"include": ["cypress/**/*.ts"]
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/status-bar/src/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function CancelBtn (props) {
title={i18n('cancel')}
aria-label={i18n('cancel')}
onClick={() => uppy.cancelAll()}
data-cy="cancel"
data-uppy-super-focusable
>
<svg
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/transloadit/src/Assembly.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class TransloaditAssembly extends Emitter {
this.socket = null
}
clearInterval(this.pollInterval)
this.pollInterval = null
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/@uppy/transloadit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ module.exports = class Transloadit extends BasePlugin {
...updatedFiles,
},
})
const fileRemovedHandler = (fileRemoved, reason) => {
if (reason === 'cancel-all') {
assembly.close()
this.uppy.off(fileRemovedHandler)
} else if (fileRemoved.id in updatedFiles) {
delete updatedFiles[fileRemoved.id]
if (Object.keys(updatedFiles).length === 0) {
assembly.close()
this.uppy.off(fileRemovedHandler)
}
}
}
this.uppy.on('file-removed', fileRemovedHandler)

this.uppy.emit('transloadit:assembly-created', status, fileIDs)

Expand Down

0 comments on commit aba7b29

Please sign in to comment.