discourse/app/assets/javascripts/admin/components/admin-web-hook-event.js.es6

109 lines
3.0 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import computed from "ember-addons/ember-computed-decorators";
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { ensureJSON, plainJSON, prettyJSON } from "discourse/lib/formatter";
2016-06-16 01:49:57 +08:00
export default Ember.Component.extend({
2018-06-15 23:03:24 +08:00
tagName: "li",
2016-06-16 01:49:57 +08:00
expandDetails: null,
expandDetailsRequestKey: "request",
expandDetailsResponseKey: "response",
2016-06-16 01:49:57 +08:00
2018-06-15 23:03:24 +08:00
@computed("model.status")
2016-06-16 01:49:57 +08:00
statusColorClasses(status) {
2018-06-15 23:03:24 +08:00
if (!status) return "";
2016-06-16 01:49:57 +08:00
if (status >= 200 && status <= 299) {
2018-06-15 23:03:24 +08:00
return "text-successful";
2016-06-16 01:49:57 +08:00
} else {
2018-06-15 23:03:24 +08:00
return "text-danger";
2016-06-16 01:49:57 +08:00
}
},
2018-06-15 23:03:24 +08:00
@computed("model.created_at")
2016-06-16 01:49:57 +08:00
createdAt(createdAt) {
2018-06-15 23:03:24 +08:00
return moment(createdAt).format("YYYY-MM-DD HH:mm:ss");
2016-06-16 01:49:57 +08:00
},
2018-06-15 23:03:24 +08:00
@computed("model.duration")
2016-06-16 01:49:57 +08:00
completion(duration) {
const seconds = Math.floor(duration / 10.0) / 100.0;
2018-06-15 23:03:24 +08:00
return I18n.t("admin.web_hooks.events.completed_in", { count: seconds });
2016-06-16 01:49:57 +08:00
},
@computed("expandDetails")
expandRequestIcon(expandDetails) {
return expandDetails === this.get("expandDetailsRequestKey")
? "ellipsis-h"
: "ellipsis-v";
},
@computed("expandDetails")
expandResponseIcon(expandDetails) {
return expandDetails === this.get("expandDetailsResponseKey")
? "ellipsis-h"
: "ellipsis-v";
},
2016-06-16 01:49:57 +08:00
actions: {
redeliver() {
2018-06-15 23:03:24 +08:00
return bootbox.confirm(
I18n.t("admin.web_hooks.events.redeliver_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
ajax(
`/admin/api/web_hooks/${this.get(
"model.web_hook_id"
)}/events/${this.get("model.id")}/redeliver`,
{ type: "POST" }
)
.then(json => {
this.set("model", json.web_hook_event);
})
.catch(popupAjaxError);
}
2016-06-16 01:49:57 +08:00
}
2018-06-15 23:03:24 +08:00
);
2016-06-16 01:49:57 +08:00
},
toggleRequest() {
const expandDetailsKey = this.get("expandDetailsRequestKey");
2016-06-16 01:49:57 +08:00
2018-06-15 23:03:24 +08:00
if (this.get("expandDetails") !== expandDetailsKey) {
let headers = _.extend(
{
"Request URL": this.get("model.request_url"),
"Request method": "POST"
},
ensureJSON(this.get("model.headers"))
);
2016-06-16 01:49:57 +08:00
this.setProperties({
headers: plainJSON(headers),
2018-06-15 23:03:24 +08:00
body: prettyJSON(this.get("model.payload")),
2016-06-16 01:49:57 +08:00
expandDetails: expandDetailsKey,
2018-06-15 23:03:24 +08:00
bodyLabel: I18n.t("admin.web_hooks.events.payload")
2016-06-16 01:49:57 +08:00
});
} else {
2018-06-15 23:03:24 +08:00
this.set("expandDetails", null);
2016-06-16 01:49:57 +08:00
}
},
toggleResponse() {
const expandDetailsKey = this.get("expandDetailsResponseKey");
2016-06-16 01:49:57 +08:00
2018-06-15 23:03:24 +08:00
if (this.get("expandDetails") !== expandDetailsKey) {
2016-06-16 01:49:57 +08:00
this.setProperties({
2018-06-15 23:03:24 +08:00
headers: plainJSON(this.get("model.response_headers")),
body: this.get("model.response_body"),
2016-06-16 01:49:57 +08:00
expandDetails: expandDetailsKey,
2018-06-15 23:03:24 +08:00
bodyLabel: I18n.t("admin.web_hooks.events.body")
2016-06-16 01:49:57 +08:00
});
} else {
2018-06-15 23:03:24 +08:00
this.set("expandDetails", null);
2016-06-16 01:49:57 +08:00
}
}
}
});