|
| 1 | +import { inject as service } from '@ember/service'; |
| 2 | +import { waitFor } from '@ember/test-waiters'; |
| 3 | +import Component from '@glimmer/component'; |
| 4 | +import { task } from 'ember-concurrency'; |
| 5 | +import IntlService from 'ember-intl/services/intl'; |
| 6 | +import File from 'ember-osf-web/packages/files/file'; |
| 7 | +import captureException, { getApiErrorMessage } from 'ember-osf-web/utils/capture-exception'; |
| 8 | +import config from 'ember-osf-web/config/environment'; |
| 9 | +import { tracked } from '@glimmer/tracking'; |
| 10 | +import { action } from '@ember/object'; |
| 11 | + |
| 12 | +interface Args { |
| 13 | + file: File; |
| 14 | + isOpen: boolean; |
| 15 | + closeModal: () => {}; |
| 16 | +} |
| 17 | + |
| 18 | +export default class SubmitToBoaModal extends Component<Args> { |
| 19 | + @service toast!: Toastr; |
| 20 | + @service intl!: IntlService; |
| 21 | + @tracked selectedDataset?: string; |
| 22 | + |
| 23 | + datasets = [ |
| 24 | + '2022 Jan/Java', |
| 25 | + '2022 Feb/Python', |
| 26 | + '2021 Method Chains', |
| 27 | + '2021 Aug/Python', |
| 28 | + '2021 Aug/Kotlin (small)', |
| 29 | + '2021 Aug/Kotlin', |
| 30 | + '2021 Jan/ML-Verse', |
| 31 | + '2020 August/Python-DS', |
| 32 | + '2019 October/GitHub (small)', |
| 33 | + '2019 October/GitHub (medium)', |
| 34 | + '2019 October/GitHub', |
| 35 | + '2015 September/GitHub', |
| 36 | + '2013 September/SF (small)', |
| 37 | + '2013 September/SF (medium)', |
| 38 | + '2013 September/SF', |
| 39 | + '2013 May/SF', |
| 40 | + '2013 February/SF', |
| 41 | + '2012 July/SF', |
| 42 | + ]; |
| 43 | + |
| 44 | + @action |
| 45 | + onDatasetChange(newDataset: string) { |
| 46 | + this.selectedDataset = newDataset; |
| 47 | + } |
| 48 | + |
| 49 | + @task |
| 50 | + @waitFor |
| 51 | + async confirmSubmitToBoa() { |
| 52 | + try { |
| 53 | + const file = this.args.file; |
| 54 | + const fileModel = file.fileModel; |
| 55 | + const parentFolder = await fileModel.get('parentFolder'); |
| 56 | + const grandparentFolder = await parentFolder.get('parentFolder'); |
| 57 | + const endpoint = config.OSF.url + 'api/v1/project/' + fileModel.target.get('id') + '/boa/submit-job/'; |
| 58 | + const uploadLink = new URL(parentFolder.get('links').upload as string); |
| 59 | + uploadLink.searchParams.set('kind', 'file'); |
| 60 | + const payload = { |
| 61 | + data: { |
| 62 | + nodeId: fileModel.target.get('id'), |
| 63 | + name: file.name, |
| 64 | + materialized: fileModel.materializedPath, |
| 65 | + sizeInt: fileModel.size, |
| 66 | + links: { |
| 67 | + download: file.links.download, |
| 68 | + upload: file.links.upload, |
| 69 | + }, |
| 70 | + }, |
| 71 | + parent: { |
| 72 | + links: { |
| 73 | + upload: uploadLink.toString(), |
| 74 | + }, |
| 75 | + isAddonRoot: !grandparentFolder, |
| 76 | + }, |
| 77 | + dataset: this.selectedDataset, |
| 78 | + }; |
| 79 | + await this.args.file.currentUser.authenticatedAJAX({ |
| 80 | + url: endpoint, |
| 81 | + type: 'POST', |
| 82 | + data: JSON.stringify(payload), |
| 83 | + xhrFields: { withCredentials: true }, |
| 84 | + headers: { |
| 85 | + 'Content-Type': 'application/json', |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + this.args.closeModal(); |
| 90 | + } catch (e) { |
| 91 | + captureException(e); |
| 92 | + const errorMessageKey = this.intl.t('osf-components.file-browser.submit_to_boa_fail', |
| 93 | + { fileName: this.args.file.name, htmlSafe: true }) as string; |
| 94 | + this.toast.error(getApiErrorMessage(e), errorMessageKey); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + this.toast.success( |
| 99 | + this.intl.t('osf-components.file-browser.submit_to_boa_success', { fileName: this.args.file.name }), |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments