|
| 1 | +import Controller from '@ember/controller'; |
| 2 | +import { run } from '@ember/runloop'; |
| 3 | + |
| 4 | +export default Controller.extend({ |
| 5 | + importStatus : '', |
| 6 | + importError : '', |
| 7 | + btnImportEvent : false, |
| 8 | + file : false, |
| 9 | + fileName : '', |
| 10 | + importTask(taskUrl) { |
| 11 | + run.later(() => { |
| 12 | + this.get('loader') |
| 13 | + .load(taskUrl) |
| 14 | + .then(data => { |
| 15 | + if (data.state !== 'SUCCESS') { |
| 16 | + this.set('importStatus', `Status: ${ data.state}`); |
| 17 | + this.importTask(taskUrl); |
| 18 | + } else { |
| 19 | + this.set('importStatus', `Status: ${ data.state}`); |
| 20 | + document.location = `/events/${ data.result.id}`; |
| 21 | + } |
| 22 | + }) |
| 23 | + .catch(e => { |
| 24 | + this.setProperties({ |
| 25 | + 'importError' : e.message, |
| 26 | + 'btnImportEvent' : false, |
| 27 | + 'file' : false |
| 28 | + }); |
| 29 | + }); |
| 30 | + }, 3000); |
| 31 | + }, |
| 32 | + actions: { |
| 33 | + uploadFile(files) { |
| 34 | + let [file] = files; |
| 35 | + var data = new FormData(); |
| 36 | + var endpoint = 'import/json'; |
| 37 | + var ext = file.name.split('.'); |
| 38 | + ext = ext[ext.length - 1].toLowerCase(); |
| 39 | + if (ext === 'xml') { |
| 40 | + endpoint = 'import/pentabarf'; |
| 41 | + } else if (ext === 'ical') { |
| 42 | + endpoint = 'import/ical'; |
| 43 | + } |
| 44 | + data.append('file', file); |
| 45 | + |
| 46 | + this.setProperties({ |
| 47 | + 'importStatus' : 'Uploading file.. Please don\'t close this window', |
| 48 | + 'importError' : '', |
| 49 | + 'btnImportEvent' : true, |
| 50 | + 'file' : true |
| 51 | + }); |
| 52 | + |
| 53 | + this.get('loader').post( |
| 54 | + `/events/${endpoint}`, |
| 55 | + data, |
| 56 | + { isFile: true } |
| 57 | + ).then(data => { |
| 58 | + this.importTask(`tasks/${data.task_url.split('/')[3]}`); |
| 59 | + }).catch(e => { |
| 60 | + this.set('importError', e.message); |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | +}); |
0 commit comments