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

110 lines
3.0 KiB
Plaintext
Raw Normal View History

import discourseComputed from "discourse-common/utils/decorators";
import Component from "@ember/component";
2018-06-15 11:03:24 -04:00
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { ensureJSON, plainJSON, prettyJSON } from "discourse/lib/formatter";
2016-06-15 13:49:57 -04:00
export default Component.extend({
2018-06-15 11:03:24 -04:00
tagName: "li",
2016-06-15 13:49:57 -04:00
expandDetails: null,
expandDetailsRequestKey: "request",
expandDetailsResponseKey: "response",
2016-06-15 13:49:57 -04:00
@discourseComputed("model.status")
2016-06-15 13:49:57 -04:00
statusColorClasses(status) {
2018-06-15 11:03:24 -04:00
if (!status) return "";
2016-06-15 13:49:57 -04:00
if (status >= 200 && status <= 299) {
2018-06-15 11:03:24 -04:00
return "text-successful";
2016-06-15 13:49:57 -04:00
} else {
2018-06-15 11:03:24 -04:00
return "text-danger";
2016-06-15 13:49:57 -04:00
}
},
@discourseComputed("model.created_at")
2016-06-15 13:49:57 -04:00
createdAt(createdAt) {
2018-06-15 11:03:24 -04:00
return moment(createdAt).format("YYYY-MM-DD HH:mm:ss");
2016-06-15 13:49:57 -04:00
},
@discourseComputed("model.duration")
2016-06-15 13:49:57 -04:00
completion(duration) {
const seconds = Math.floor(duration / 10.0) / 100.0;
2018-06-15 11:03:24 -04:00
return I18n.t("admin.web_hooks.events.completed_in", { count: seconds });
2016-06-15 13:49:57 -04:00
},
@discourseComputed("expandDetails")
expandRequestIcon(expandDetails) {
return expandDetails === this.expandDetailsRequestKey
? "ellipsis-h"
: "ellipsis-v";
},
@discourseComputed("expandDetails")
expandResponseIcon(expandDetails) {
return expandDetails === this.expandDetailsResponseKey
? "ellipsis-h"
: "ellipsis-v";
},
2016-06-15 13:49:57 -04:00
actions: {
redeliver() {
2018-06-15 11:03:24 -04: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-15 13:49:57 -04:00
}
2018-06-15 11:03:24 -04:00
);
2016-06-15 13:49:57 -04:00
},
toggleRequest() {
const expandDetailsKey = this.expandDetailsRequestKey;
2016-06-15 13:49:57 -04:00
if (this.expandDetails !== expandDetailsKey) {
2018-06-15 11:03:24 -04:00
let headers = _.extend(
{
"Request URL": this.get("model.request_url"),
"Request method": "POST"
},
ensureJSON(this.get("model.headers"))
);
2016-06-15 13:49:57 -04:00
this.setProperties({
headers: plainJSON(headers),
2018-06-15 11:03:24 -04:00
body: prettyJSON(this.get("model.payload")),
2016-06-15 13:49:57 -04:00
expandDetails: expandDetailsKey,
2018-06-15 11:03:24 -04:00
bodyLabel: I18n.t("admin.web_hooks.events.payload")
2016-06-15 13:49:57 -04:00
});
} else {
2018-06-15 11:03:24 -04:00
this.set("expandDetails", null);
2016-06-15 13:49:57 -04:00
}
},
toggleResponse() {
const expandDetailsKey = this.expandDetailsResponseKey;
2016-06-15 13:49:57 -04:00
if (this.expandDetails !== expandDetailsKey) {
2016-06-15 13:49:57 -04:00
this.setProperties({
2018-06-15 11:03:24 -04:00
headers: plainJSON(this.get("model.response_headers")),
body: this.get("model.response_body"),
2016-06-15 13:49:57 -04:00
expandDetails: expandDetailsKey,
2018-06-15 11:03:24 -04:00
bodyLabel: I18n.t("admin.web_hooks.events.body")
2016-06-15 13:49:57 -04:00
});
} else {
2018-06-15 11:03:24 -04:00
this.set("expandDetails", null);
2016-06-15 13:49:57 -04:00
}
}
}
});