Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions src/DirectUploadProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ export type DelegatedProps = {|
xhr: XMLHttpRequest,
}) => mixed,
render: RenderProps => React.Node,
fullAttributes?: boolean,
|}

type Props = {
...DelegatedProps,
origin: Origin,
onSuccess: (string[]) => mixed,
onSuccess: (any[]) => mixed,
headers?: CustomHeaders,
}

Expand Down Expand Up @@ -87,11 +88,11 @@ class DirectUploadProvider extends React.Component<Props, State> {

this.setState({ uploading: true })

const signedIds = await Promise.all(
const resultArr = await Promise.all(
this.uploads.map(upload => upload.start())
)

this.props.onSuccess(signedIds)
this.props.onSuccess(resultArr)
this.uploads = []
this.setState({ fileUploads: {}, uploading: false })
}
Expand All @@ -110,6 +111,7 @@ class DirectUploadProvider extends React.Component<Props, State> {
onBeforeBlobRequest,
onBeforeStorageRequest,
origin,
fullAttributes,
} = this.props

return new Upload(file, {
Expand All @@ -119,6 +121,7 @@ class DirectUploadProvider extends React.Component<Props, State> {
onBeforeStorageRequest,
onChangeFile: this.handleChangeFileUpload,
origin,
fullAttributes,
})
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/Upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Options = {|
}) => mixed,
onChangeFile: ({ [string]: ActiveStorageFileUpload }) => mixed,
headers?: CustomHeaders,
fullAttributes?: boolean,
|}

class Upload {
Expand Down Expand Up @@ -85,13 +86,13 @@ class Upload {
})
}

handleSuccess = (signedId: string) => {
handleSuccess = (result: string | Object) => {
this.handleChangeFile({
state: 'finished',
id: this.id,
file: this.directUpload.file,
})
return signedId
return result
}

handleError = (error: string) => {
Expand All @@ -104,11 +105,14 @@ class Upload {
throw error
}

start(): Promise<string> {
start(): Promise<string> | Promise<Object> {
const promise = new Promise((resolve, reject) => {
this.directUpload.create((error, attributes) => {
if (error) reject(error)
else resolve(attributes.signed_id)
else {
let result = this.options.fullAttributes ? attributes : attributes.signed_id
resolve(result)
}
})
})

Expand Down
7 changes: 6 additions & 1 deletion src/Upload.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jest.mock('activestorage', () => {
id: 'id',
file,
create(cb) {
cb(null, { signed_id: 'signedId' })
cb(null, { signed_id: 'signedId', key: 'key' })
},
})),
}
Expand Down Expand Up @@ -79,6 +79,11 @@ describe('Upload', () => {
expect(await upload.start()).toEqual('signedId')
})

it('resolves with fullAttributes from the direct upload when option "fullAttributes" is true', async () => {
const upload = new Upload(file, { ...options, fullAttributes: true })
expect(await upload.start()).toEqual({ signed_id: 'signedId', key: 'key' })
})

it('reports that the upload is finished if it does so', async () => {
const upload = new Upload(file, options)
await upload.start()
Expand Down