2021-01-26 09:05:10 +01:00
|
|
|
import Component from "@ember/component";
|
2021-02-16 01:43:00 +01:00
|
|
|
import { equal } from "@ember/object/computed";
|
2021-02-01 14:58:37 +01:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { getToken } from "wizard/lib/ajax";
|
2021-01-26 09:05:10 +01:00
|
|
|
|
|
|
|
export default Component.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
classNames: ["validator"],
|
2021-02-15 09:19:14 +01:00
|
|
|
classNameBindings: ["isValid", "isInvalid"],
|
|
|
|
validMessageKey: null,
|
|
|
|
invalidMessageKey: null,
|
|
|
|
isValid: null,
|
2021-02-16 01:43:00 +01:00
|
|
|
isInvalid: equal("isValid", false),
|
2021-02-15 09:19:14 +01:00
|
|
|
layoutName: "components/validator", // useful for sharing the template with extending components
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
if (this.get("validation.backend")) {
|
|
|
|
// set a function that can be called as often as it need to
|
|
|
|
// from the derived component
|
|
|
|
this.backendValidate = (params) => {
|
|
|
|
return ajax("/realtime-validations", {
|
|
|
|
data: {
|
2021-02-16 01:43:00 +01:00
|
|
|
type: this.get("type"),
|
2021-02-15 09:19:14 +01:00
|
|
|
authenticity_token: getToken(),
|
|
|
|
...params,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
didInsertElement() {
|
|
|
|
this.appEvents.on("custom-wizard:validate", this, this.checkIsValid);
|
|
|
|
},
|
2021-01-30 18:46:04 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
willDestroyElement() {
|
|
|
|
this.appEvents.off("custom-wizard:validate", this, this.checkIsValid);
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:19:14 +01:00
|
|
|
checkIsValid() {
|
|
|
|
this.set("isValid", this.validate());
|
|
|
|
},
|
|
|
|
});
|