This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/exception.js.es6
Jarek Radosz fe588cc7f8
DEV: Fix function prototype deprecations (#8681)
* DEV: Fix the function prototype observers deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.observes('foo') to observer('foo', function() {}). [deprecation id: function-prototype-extensions.observes] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-observes for more details.

* DEV: Fix the function prototype event listeners deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.on('foo') to on('foo', function() {}). [deprecation id: function-prototype-extensions.on] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-on for more details.

* DEV: Simplify `default as` imports

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2020-01-16 18:56:53 +01:00

122 lines
3.1 KiB
JavaScript

import { equal, gte, none, alias } from "@ember/object/computed";
import { schedule } from "@ember/runloop";
import Controller from "@ember/controller";
import discourseComputed, { on } from "discourse-common/utils/decorators";
const ButtonBackBright = {
classes: "btn-primary",
action: "back",
key: "errors.buttons.back"
},
ButtonBackDim = {
classes: "",
action: "back",
key: "errors.buttons.back"
},
ButtonTryAgain = {
classes: "btn-primary",
action: "tryLoading",
key: "errors.buttons.again",
icon: "sync"
},
ButtonLoadPage = {
classes: "btn-primary",
action: "tryLoading",
key: "errors.buttons.fixed"
};
// The controller for the nice error page
export default Controller.extend({
thrown: null,
lastTransition: null,
@discourseComputed
isNetwork() {
// never made it on the wire
if (this.get("thrown.readyState") === 0) return true;
// timed out
if (this.get("thrown.jqTextStatus") === "timeout") return true;
return false;
},
isNotFound: equal("thrown.status", 404),
isForbidden: equal("thrown.status", 403),
isServer: gte("thrown.status", 500),
isUnknown: none("isNetwork", "isServer"),
// TODO
// make ajax requests to /srv/status with exponential backoff
// if one succeeds, set networkFixed to true, which puts a "Fixed!" message on the page
networkFixed: false,
loading: false,
@on("init")
_init() {
this.set("loading", false);
},
@discourseComputed("isNetwork", "isServer", "isUnknown")
reason() {
if (this.isNetwork) {
return I18n.t("errors.reasons.network");
} else if (this.isServer) {
return I18n.t("errors.reasons.server");
} else if (this.isNotFound) {
return I18n.t("errors.reasons.not_found");
} else if (this.isForbidden) {
return I18n.t("errors.reasons.forbidden");
} else {
// TODO
return I18n.t("errors.reasons.unknown");
}
},
requestUrl: alias("thrown.requestedUrl"),
@discourseComputed("networkFixed", "isNetwork", "isServer", "isUnknown")
desc() {
if (this.networkFixed) {
return I18n.t("errors.desc.network_fixed");
} else if (this.isNetwork) {
return I18n.t("errors.desc.network");
} else if (this.isNotFound) {
return I18n.t("errors.desc.not_found");
} else if (this.isServer) {
return I18n.t("errors.desc.server", {
status: this.get("thrown.status") + " " + this.get("thrown.statusText")
});
} else {
// TODO
return I18n.t("errors.desc.unknown");
}
},
@discourseComputed("networkFixed", "isNetwork", "isServer", "isUnknown")
enabledButtons() {
if (this.networkFixed) {
return [ButtonLoadPage];
} else if (this.isNetwork) {
return [ButtonBackDim, ButtonTryAgain];
} else {
return [ButtonBackBright, ButtonTryAgain];
}
},
actions: {
back() {
window.history.back();
},
tryLoading() {
this.set("loading", true);
schedule("afterRender", () => {
this.lastTransition.retry();
this.set("loading", false);
});
}
}
});