Ghost-Admin/app/controllers/settings/labs.js

90 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-02-13 05:22:32 +01:00
import Ember from 'ember';
2016-01-19 14:03:27 +01:00
const {
$,
Controller,
inject: {service},
2016-01-19 14:03:27 +01:00
isArray
} = Ember;
export default Controller.extend({
uploadButtonText: 'Import',
importErrors: '',
submitting: false,
showDeleteAllModal: false,
2016-01-19 14:03:27 +01:00
ghostPaths: service(),
notifications: service(),
session: service(),
ajax: service(),
actions: {
onUpload(file) {
let formData = new FormData();
let notifications = this.get('notifications');
let currentUserId = this.get('session.user.id');
2016-01-18 16:37:14 +01:00
let dbUrl = this.get('ghostPaths.url').api('db');
this.set('uploadButtonText', 'Importing');
this.set('importErrors', '');
formData.append('importfile', file);
2016-01-18 16:37:14 +01:00
this.get('ajax').post(dbUrl, {
data: formData,
dataType: 'json',
cache: false,
contentType: false,
processData: false
}).then(() => {
// Clear the store, so that all the new data gets fetched correctly.
this.store.unloadAll();
2015-07-03 13:27:36 +02:00
// Reload currentUser and set session
this.set('session.user', this.store.findRecord('user', currentUserId));
// TODO: keep as notification, add link to view content
notifications.showNotification('Import successful.', {key: 'import.upload.success'});
}).catch((response) => {
2016-01-18 16:37:14 +01:00
if (response && response.errors && isArray(response.errors)) {
this.set('importErrors', response.errors);
}
notifications.showAlert('Import Failed', {type: 'error', key: 'import.upload.failed'});
}).finally(() => {
this.set('uploadButtonText', 'Import');
});
},
exportData() {
let dbUrl = this.get('ghostPaths.url').api('db');
let accessToken = this.get('session.data.authenticated.access_token');
let downloadURL = `${dbUrl}?access_token=${accessToken}`;
let iframe = $('#iframeDownload');
if (iframe.length === 0) {
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
}
iframe.attr('src', downloadURL);
},
sendTestEmail() {
let notifications = this.get('notifications');
2016-01-18 16:37:14 +01:00
let emailUrl = this.get('ghostPaths.url').api('mail', 'test');
this.toggleProperty('submitting');
2016-01-18 16:37:14 +01:00
this.get('ajax').post(emailUrl).then(() => {
notifications.showAlert('Check your email for the test message.', {type: 'info', key: 'test-email.send.success'});
this.toggleProperty('submitting');
}).catch((error) => {
2016-01-18 16:37:14 +01:00
notifications.showAPIError(error, {key: 'test-email:send'});
this.toggleProperty('submitting');
});
},
toggleDeleteAllModal() {
this.toggleProperty('showDeleteAllModal');
}
}
});