Skip to content

Commit 4b711e6

Browse files
YogeshSharma0201abhinavk96
authored andcommitted
Fix import Event (#1945)
1 parent 9d696a4 commit 4b711e6

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

app/components/events/event-import-section.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ export default Component.extend(FormMixin, {
2727
};
2828
},
2929
actions: {
30+
onFileSelected(files) {
31+
this.set('files', files);
32+
},
3033
submit() {
3134
this.onValid(() => {
35+
this.uploadFile(this.files);
3236
});
3337
}
3438
}

app/controllers/events/import.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Controller from '@ember/controller';
2+
import { run } from '@ember/runloop';
3+
4+
export default Controller.extend({
5+
importStatus : '',
6+
importError : '',
7+
isImporting : 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+
this.transitionToRoute('events.view', data.result.id);
21+
}
22+
})
23+
.catch(e => {
24+
this.setProperties({
25+
'importError' : e.message,
26+
'isImporting' : 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 === 'ics' || ext === 'ical') {
42+
endpoint = 'import/ical';
43+
} else if (ext === 'xcal') {
44+
endpoint = 'import/xcal';
45+
}
46+
data.append('file', file);
47+
48+
this.setProperties({
49+
'importStatus' : 'Uploading file.. Please don\'t close this window',
50+
'importError' : '',
51+
'isImporting' : true,
52+
'file' : true
53+
});
54+
55+
this.get('loader').post(
56+
`/events/${endpoint}`,
57+
data,
58+
{ isFile: true }
59+
).then(data => {
60+
this.importTask(`tasks/${data.task_url.split('/')[3]}`);
61+
}).catch(e => {
62+
this.set('importError', e.message);
63+
});
64+
}
65+
}
66+
});

app/services/loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default Service.extend({
1717
adapter : 'adapter:application',
1818
isExternal : false,
1919
isFormData : false,
20+
isFile : false,
2021
queryParams : {},
2122
withoutPrefix : false,
2223
replaceHeaders : false,
@@ -48,6 +49,9 @@ export default Service.extend({
4849
} else {
4950
if (config.isFormData) {
5051
fetchOptions.body = objectToFormData(data);
52+
} else if (config.isFile) {
53+
delete fetchOptions.headers['Content-Type'];
54+
fetchOptions.body = data;
5155
} else {
5256
fetchOptions.headers['Content-Type'] = 'application/json';
5357
fetchOptions.body = JSON.stringify(data);

app/templates/components/events/event-import-section.hbs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
<form class="ui form" {{action 'submit' on='submit'}}>
33
<div class="field {{if device.isMobile 'sixteen' 'five'}} wide column">
44
<label class="required" for="file"> {{t 'Select event source file to import'}}</label>
5-
{{input type='file' id='file'}}
5+
<input accept='.zip,.xml,.ical,.ics,.xcal' type="file" disabled={{disabled}} onchange={{action 'onFileSelected' value='target.files'}}>
66
</div>
77
<div class="field {{if device.isMobile 'sixteen' 'five'}} wide column">
88
<span class="text muted">
99
<b>{{t 'Supported formats:'}}</b>
1010
{{t 'Open Event compatible json package (.zip), PentabarfXML (.xml), iCalendar (.ical, .ics), XML Representation of iCalendar (.xcal)'}}
1111
</span>
1212
</div>
13-
<button class="ui small primary button" type="submit">{{t 'Import Event'}}</button>
13+
<button class="ui small primary button" type="submit" disabled={{isImporting}}>{{t 'Import Event'}}</button>
14+
<div style="margin-top: 10px;">
15+
<span>{{importStatus}}</span><br>
16+
<span class="red">{{importError}}</span>
17+
</div>
1418
</form>
1519
</div>

app/templates/events/import.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
{{events/event-import-section}}
1+
{{events/event-import-section uploadFile=(action 'uploadFile') importStatus=importStatus importError=importError isImporting=isImporting file=file}}
22
<br><br>
33
{{events/imports-history-section}}

0 commit comments

Comments
 (0)