DEV: refactoring admin-backups-index (#6933)

This commit is contained in:
Joffrey JAFFEUX 2019-01-23 17:40:24 +01:00 committed by GitHub
parent 60974932c4
commit 0aa049791e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 80 deletions

View File

@ -1,41 +1,36 @@
import { ajax } from "discourse/lib/ajax";
import computed from "ember-addons/ember-computed-decorators";
import { default as computed } from "ember-addons/ember-computed-decorators";
import { setting, i18n } from "discourse/lib/computed";
export default Ember.Controller.extend({
adminBackups: Ember.inject.controller(),
status: Ember.computed.alias("adminBackups.model"),
uploadLabel: i18n("admin.backups.upload.label"),
backupLocation: setting("backup_location"),
localBackupStorage: Ember.computed.equal("backupLocation", "local"),
@computed
localBackupStorage() {
return this.siteSettings.backup_location === "local";
},
uploadLabel: function() {
return I18n.t("admin.backups.upload.label");
}.property(),
restoreTitle: function() {
if (!this.get("status.allowRestore")) {
@computed("status.allowRestore", "status.isOperationRunning")
restoreTitle(allowRestore, isOperationRunning) {
if (!allowRestore) {
return "admin.backups.operations.restore.is_disabled";
} else if (this.get("status.isOperationRunning")) {
} else if (isOperationRunning) {
return "admin.backups.operations.is_running";
} else {
return "admin.backups.operations.restore.title";
}
}.property("status.{allowRestore,isOperationRunning}"),
},
actions: {
toggleReadOnlyMode() {
var self = this;
if (!this.site.get("isReadOnly")) {
bootbox.confirm(
I18n.t("admin.backups.read_only.enable.confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
Discourse.User.currentProp("hideReadOnlyAlert", true);
self._toggleReadOnlyMode(true);
this._toggleReadOnlyMode(true);
}
}
);
@ -45,20 +40,17 @@ export default Ember.Controller.extend({
},
download(backup) {
let link = backup.get("filename");
ajax("/admin/backups/" + link, { type: "PUT" }).then(() => {
bootbox.alert(I18n.t("admin.backups.operations.download.alert"));
});
const link = backup.get("filename");
ajax(`/admin/backups/${link}`, { type: "PUT" }).then(() =>
bootbox.alert(I18n.t("admin.backups.operations.download.alert"))
);
}
},
_toggleReadOnlyMode(enable) {
var site = this.site;
ajax("/admin/backups/readonly", {
type: "PUT",
data: { enable: enable }
}).then(() => {
site.set("isReadOnly", enable);
});
data: { enable }
}).then(() => this.site.set("isReadOnly", enable));
}
});

View File

@ -47,15 +47,15 @@ export default Discourse.Route.extend({
},
model() {
return PreloadStore.getAndRemove("operations_status", function() {
return ajax("/admin/backups/status.json");
}).then(status => {
return BackupStatus.create({
return PreloadStore.getAndRemove("operations_status", () =>
ajax("/admin/backups/status.json")
).then(status =>
BackupStatus.create({
isOperationRunning: status.is_operation_running,
canRollback: status.can_rollback,
allowRestore: status.allow_restore
});
});
})
);
},
deactivate() {
@ -74,33 +74,30 @@ export default Discourse.Route.extend({
},
destroyBackup(backup) {
const self = this;
bootbox.confirm(
I18n.t("admin.backups.operations.destroy.confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
backup.destroy().then(function() {
self
.controllerFor("adminBackupsIndex")
backup.destroy().then(() =>
this.controllerFor("adminBackupsIndex")
.get("model")
.removeObject(backup);
});
.removeObject(backup)
);
}
}
);
},
startRestore(backup) {
const self = this;
bootbox.confirm(
I18n.t("admin.backups.operations.restore.confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
self.transitionTo("admin.backups.logs");
this.transitionTo("admin.backups.logs");
backup.restore();
}
}
@ -108,17 +105,17 @@ export default Discourse.Route.extend({
},
cancelOperation() {
const self = this;
bootbox.confirm(
I18n.t("admin.backups.operations.cancel.confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
Backup.cancel().then(function() {
self
.controllerFor("adminBackups")
.set("model.isOperationRunning", false);
Backup.cancel().then(() => {
this.controllerFor("adminBackups").set(
"model.isOperationRunning",
false
);
});
}
}
@ -130,7 +127,7 @@ export default Discourse.Route.extend({
I18n.t("admin.backups.operations.rollback.confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
function(confirmed) {
confirmed => {
if (confirmed) {
Backup.rollback();
}
@ -139,17 +136,12 @@ export default Discourse.Route.extend({
},
uploadSuccess(filename) {
bootbox.alert(
I18n.t("admin.backups.upload.success", { filename: filename })
);
bootbox.alert(I18n.t("admin.backups.upload.success", { filename }));
},
uploadError(filename, message) {
bootbox.alert(
I18n.t("admin.backups.upload.error", {
filename: filename,
message: message
})
I18n.t("admin.backups.upload.error", { filename, message })
);
},

View File

@ -1,20 +1,38 @@
<div class="backup-options">
{{#if localBackupStorage}}
{{resumable-upload target="/admin/backups/upload" success=(route-action "uploadSuccess") error=(route-action "uploadError") uploadText=uploadLabel title="admin.backups.upload.title" class="btn-default"}}
{{else}}
{{backup-uploader done=(route-action "remoteUploadSuccess")}}
{{/if}}
<div class="backup-options">
{{#if localBackupStorage}}
{{resumable-upload
target="/admin/backups/upload"
success=(route-action "uploadSuccess")
error=(route-action "uploadError")
uploadText=uploadLabel
title="admin.backups.upload.title"
class="btn-default"}}
{{else}}
{{backup-uploader done=(route-action "remoteUploadSuccess")}}
{{/if}}
{{#if site.isReadOnly}}
{{d-button class="btn-default" icon="far-eye" action=(action "toggleReadOnlyMode") disabled=status.isOperationRunning title="admin.backups.read_only.disable.title" label="admin.backups.read_only.disable.label"}}
{{else}}
{{d-button class="btn-default" icon="far-eye" action=(action "toggleReadOnlyMode") disabled=status.isOperationRunning title="admin.backups.read_only.enable.title" label="admin.backups.read_only.enable.label"}}
{{/if}}
</div>
{{#if site.isReadOnly}}
{{d-button
class="btn-default"
icon="far-eye"
action=(action "toggleReadOnlyMode")
disabled=status.isOperationRunning
title="admin.backups.read_only.disable.title"
label="admin.backups.read_only.disable.label"}}
{{else}}
{{d-button
class="btn-default"
icon="far-eye"
action=(action "toggleReadOnlyMode")
disabled=status.isOperationRunning
title="admin.backups.read_only.enable.title"
label="admin.backups.read_only.enable.label"}}
{{/if}}
</div>
<table class="grid">
<thead>
<th width="55%">{{i18n 'admin.backups.columns.filename'}}</th>
<th width="10%">{{i18n 'admin.backups.columns.size'}}</th>
<th width="55%">{{i18n "admin.backups.columns.filename"}}</th>
<th width="10%">{{i18n "admin.backups.columns.size"}}</th>
<th></th>
</thead>
<tbody>
@ -25,24 +43,47 @@
<td class="backup-controls">
<div>
{{d-button class="btn-default download"
action=(action "download")
actionParam=backup
icon="download"
title="admin.backups.operations.download.title"
label="admin.backups.operations.download.label"}}
action=(action "download")
actionParam=backup
icon="download"
title="admin.backups.operations.download.title"
label="admin.backups.operations.download.label"}}
{{#if status.isOperationRunning}}
{{d-button icon="far-trash-alt" action=(route-action "destroyBackup") actionParam=backup class="btn-danger" disabled="true" title="admin.backups.operations.is_running"}}
{{d-button icon="play" action=(route-action "startRestore") actionParam=backup disabled=status.restoreDisabled class="btn-default" title=restoreTitle label="admin.backups.operations.restore.label"}}
{{d-button
icon="far-trash-alt"
action=(route-action "destroyBackup")
actionParam=backup class="btn-danger"
disabled="true"
title="admin.backups.operations.is_running"}}
{{d-button
icon="play"
action=(route-action "startRestore")
actionParam=backup disabled=status.restoreDisabled
class="btn-default"
title=restoreTitle
label="admin.backups.operations.restore.label"}}
{{else}}
{{d-button icon="far-trash-alt" action=(route-action "destroyBackup") actionParam=backup class="btn-danger" title="admin.backups.operations.destroy.title"}}
{{d-button icon="play" action=(route-action "startRestore") actionParam=backup disabled=status.restoreDisabled class="btn-default" title=restoreTitle label="admin.backups.operations.restore.label"}}
{{d-button
icon="far-trash-alt"
action=(route-action "destroyBackup")
actionParam=backup
class="btn-danger"
title="admin.backups.operations.destroy.title"}}
{{d-button
icon="play"
action=(route-action "startRestore")
actionParam=backup
disabled=status.restoreDisabled
class="btn-default"
title=restoreTitle
label="admin.backups.operations.restore.label"}}
{{/if}}
</div>
</td>
</tr>
{{else}}
<tr>
<td>{{i18n 'admin.backups.none'}}</td>
<td>{{i18n "admin.backups.none"}}</td>
<td></td>
<td></td>
</tr>