Skip to content

Commit 6ab92b8

Browse files
committed
fix: extracted download submission handler
1 parent 1f72e1d commit 6ab92b8

File tree

1 file changed

+33
-30
lines changed
  • src/components/ChallengeEditor/Submissions

1 file changed

+33
-30
lines changed

src/components/ChallengeEditor/Submissions/index.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class SubmissionsComponent extends React.Component {
6161
this.checkIsReviewPhaseComplete = this.checkIsReviewPhaseComplete.bind(
6262
this
6363
)
64+
this.downloadSubmission = this.downloadSubmission.bind(this)
6465
}
6566

6667
componentDidMount () {
@@ -228,6 +229,37 @@ class SubmissionsComponent extends React.Component {
228229
})
229230
}
230231

232+
async downloadSubmission (submission) {
233+
// download submission
234+
const reactLib = getTopcoderReactLib()
235+
const { getService } = reactLib.services.submissions
236+
const submissionsService = getService(this.props.token)
237+
submissionsService.downloadSubmission(submission.id)
238+
.then((blob) => {
239+
isValidDownloadFile(blob).then((isValidFile) => {
240+
if (isValidFile.success) {
241+
// eslint-disable-next-line no-undef
242+
const url = window.URL.createObjectURL(new Blob([blob]))
243+
const link = document.createElement('a')
244+
link.href = url
245+
let fileName = submission.legacySubmissionId
246+
if (!fileName) {
247+
fileName = submission.id
248+
}
249+
fileName = fileName + '.zip'
250+
link.setAttribute('download', `${fileName}`)
251+
document.body.appendChild(link)
252+
link.click()
253+
link.parentNode.removeChild(link)
254+
} else {
255+
this.setState({
256+
alertMessage: isValidFile.message || 'Can not download this submission.'
257+
})
258+
}
259+
})
260+
})
261+
}
262+
231263
render () {
232264
const { challenge, token, loggedInUserResource } = this.props
233265
const { checkpoints, track, type, tags } = challenge
@@ -555,36 +587,7 @@ class SubmissionsComponent extends React.Component {
555587
<div className={styles['button-wrapper']}>
556588
<button
557589
className={styles['download-submission-button']}
558-
onClick={() => {
559-
// download submission
560-
const reactLib = getTopcoderReactLib()
561-
const { getService } = reactLib.services.submissions
562-
const submissionsService = getService(token)
563-
submissionsService.downloadSubmission(s.id)
564-
.then((blob) => {
565-
isValidDownloadFile(blob).then((isValidFile) => {
566-
if (isValidFile.success) {
567-
// eslint-disable-next-line no-undef
568-
const url = window.URL.createObjectURL(new Blob([blob]))
569-
const link = document.createElement('a')
570-
link.href = url
571-
let fileName = s.legacySubmissionId
572-
if (!fileName) {
573-
fileName = s.id
574-
}
575-
fileName = fileName + '.zip'
576-
link.setAttribute('download', `${fileName}`)
577-
document.body.appendChild(link)
578-
link.click()
579-
link.parentNode.removeChild(link)
580-
} else {
581-
this.setState({
582-
alertMessage: isValidFile.message || 'Can not download this submission.'
583-
})
584-
}
585-
})
586-
})
587-
}}
590+
onClick={() => this.downloadSubmission(s)}
588591
>
589592
<ReactSVG path={assets(`${Download}`)} />
590593
</button>

0 commit comments

Comments
 (0)