Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/core: remove reason #5159

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions docs/uppy-core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,7 @@ Retry an upload (after an error, for example).

Retry all uploads (after an error, for example).

#### `cancelAll({ reason: 'user' })`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically |
#### `cancelAll()`

Cancel all uploads, reset progress and remove all files.

Expand Down Expand Up @@ -874,11 +870,7 @@ uppy.getPlugin('Dashboard').setOptions({
});
```

#### `close({ reason: 'user' })`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `reason` | `string` | The reason for canceling. Plugins can use this to provide different cleanup behavior (Transloadit plugin cancels an Assembly if user clicked on the “cancel” button). Possible values are: `user` (default) - The user has pressed “cancel”; `unmount` - The Uppy instance has been closed programmatically |
#### `destroy()`

Uninstall all plugins and close down this Uppy instance. Also runs
`uppy.cancelAll()` before uninstalling.
Expand Down Expand Up @@ -1024,25 +1016,19 @@ Fired each time a file is removed.
**Parameters**

- `file` - The [Uppy file](#working-with-uppy-files) that was removed.
- `reason` - A string explaining why the file was removed. See
[#2301](https://github.com/transloadit/uppy/issues/2301#issue-628931176) for
details. Current reasons are: `removed-by-user` and `cancel-all`.

**Example**

```js
uppy.on('file-removed', (file, reason) => {
uppy.on('file-removed', (file) => {
console.log('Removed file', file);
});
```

```js
uppy.on('file-removed', (file, reason) => {
uppy.on('file-removed', (file) => {
removeFileFromUploadingCounterUI(file);

if (reason === 'removed-by-user') {
sendDeleteRequestForFile(file);
}
sendDeleteRequestForFile(file);
});
```

Expand Down Expand Up @@ -1322,10 +1308,6 @@ Fired when “info” message should be hidden in the UI. See

#### `cancel-all`

| Argument | Type | Description |
| -------- | -------- | ----------------------------------- |
| `reason` | `string` | See [uppy.cancelAll](####cancelAll) |

Fired when `cancelAll()` is called, all uploads are canceled, files removed and
progress is reset.

Expand Down
31 changes: 13 additions & 18 deletions packages/@uppy/core/src/Uppy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
uploadID: string,
) => Promise<unknown> | void

type FileRemoveReason = 'user' | 'cancel-all' | 'unmount'

type LogLevel = 'info' | 'warning' | 'error' | 'success'

export type UnknownPlugin<
Expand Down Expand Up @@ -228,15 +226,15 @@

export interface _UppyEventMap<M extends Meta, B extends Body> {
'back-online': () => void
'cancel-all': (reason: { reason?: FileRemoveReason }) => void
'cancel-all': () => void
complete: (result: UploadResult<M, B>) => void
error: (
error: { name: string; message: string; details?: string },
file?: UppyFile<M, B>,
response?: UppyFile<M, B>['response'],
) => void
'file-added': (file: UppyFile<M, B>) => void
'file-removed': (file: UppyFile<M, B>, reason?: FileRemoveReason) => void
'file-removed': (file: UppyFile<M, B>) => void
'files-added': (files: UppyFile<M, B>[]) => void
'info-hidden': () => void
'info-visible': () => void
Expand Down Expand Up @@ -1115,7 +1113,7 @@
}
}

removeFiles(fileIDs: string[], reason?: FileRemoveReason): void {

Check failure on line 1116 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Cannot find name 'FileRemoveReason'.

Check failure on line 1116 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Cannot find name 'FileRemoveReason'.
const { files, currentUploads } = this.getState()
const updatedFiles = { ...files }
const updatedUploads = { ...currentUploads }
Expand Down Expand Up @@ -1175,7 +1173,7 @@

const removedFileIDs = Object.keys(removedFiles)
removedFileIDs.forEach((fileID) => {
this.emit('file-removed', removedFiles[fileID], reason)

Check failure on line 1176 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Expected 2 arguments, but got 3.

Check failure on line 1176 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Expected 2 arguments, but got 3.
})

if (removedFileIDs.length > 5) {
Expand All @@ -1185,7 +1183,7 @@
}
}

removeFile(fileID: string, reason?: FileRemoveReason): void {

Check failure on line 1186 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Cannot find name 'FileRemoveReason'.

Check failure on line 1186 in packages/@uppy/core/src/Uppy.ts

View workflow job for this annotation

GitHub Actions / Type tests

Cannot find name 'FileRemoveReason'.
this.removeFiles([fileID], reason)
}

Expand Down Expand Up @@ -1283,21 +1281,18 @@
return this.#runUpload(uploadID)
}

cancelAll({ reason = 'user' }: { reason?: FileRemoveReason } = {}): void {
this.emit('cancel-all', { reason })

// Only remove existing uploads if user is canceling
if (reason === 'user') {
const { files } = this.getState()
cancelAll(): void {
this.emit('cancel-all')

const fileIDs = Object.keys(files)
if (fileIDs.length) {
this.removeFiles(fileIDs, 'cancel-all')
}
const { files } = this.getState()

this.setState(defaultUploadState)
// todo should we call this.emit('reset-progress') like we do for resetProgress?
const fileIDs = Object.keys(files)
if (fileIDs.length) {
this.removeFiles(fileIDs, 'cancel-all')
}

this.setState(defaultUploadState)
// todo should we call this.emit('reset-progress') like we do for resetProgress?
}

retryUpload(fileID: string): Promise<UploadResult<M, B> | undefined> {
Expand Down Expand Up @@ -1796,12 +1791,12 @@
/**
* Uninstall all plugins and close down this Uppy instance.
*/
destroy({ reason }: { reason?: FileRemoveReason } | undefined = {}): void {
destroy(): void {
this.log(
`Closing Uppy instance ${this.opts.id}: removing all files and uninstalling plugins`,
)

this.cancelAll({ reason })
this.cancelAll()

this.#storeUnsubscribe()

Expand Down
Loading