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/components/edit-topic-timer-form.js.es6
Joffrey JAFFEUX 0431942f3d
DEV: select-kit 2 (#7998)
This new iteration of select-kit focuses on following best principales and disallowing mutations inside select-kit components. A best effort has been made to avoid breaking changes, however if you content was a flat array, eg: ["foo", "bar"] You will need to set valueProperty=null and nameProperty=null on the component.

Also almost every component should have an `onChange` handler now to decide what to do with the updated data. **select-kit will not mutate your data by itself anymore**
2020-02-03 14:22:14 +01:00

90 lines
2.4 KiB
JavaScript

import { isEmpty } from "@ember/utils";
import { equal, or, readOnly } from "@ember/object/computed";
import { schedule } from "@ember/runloop";
import Component from "@ember/component";
import discourseComputed, {
observes,
on
} from "discourse-common/utils/decorators";
import {
PUBLISH_TO_CATEGORY_STATUS_TYPE,
OPEN_STATUS_TYPE,
DELETE_STATUS_TYPE,
REMINDER_TYPE,
CLOSE_STATUS_TYPE,
BUMP_TYPE
} from "discourse/controllers/edit-topic-timer";
export default Component.extend({
selection: readOnly("topicTimer.status_type"),
autoOpen: equal("selection", OPEN_STATUS_TYPE),
autoClose: equal("selection", CLOSE_STATUS_TYPE),
autoDelete: equal("selection", DELETE_STATUS_TYPE),
autoBump: equal("selection", BUMP_TYPE),
publishToCategory: equal("selection", PUBLISH_TO_CATEGORY_STATUS_TYPE),
reminder: equal("selection", REMINDER_TYPE),
showTimeOnly: or("autoOpen", "autoDelete", "reminder", "autoBump"),
@discourseComputed(
"topicTimer.updateTime",
"publishToCategory",
"topicTimer.category_id"
)
saveDisabled(updateTime, publishToCategory, topicTimerCategoryId) {
return isEmpty(updateTime) || (publishToCategory && !topicTimerCategoryId);
},
@discourseComputed("topic.visible")
excludeCategoryId(visible) {
if (visible) return this.get("topic.category_id");
},
@on("init")
@observes("topicTimer", "topicTimer.execute_at", "topicTimer.duration")
_setUpdateTime() {
let time = null;
const executeAt = this.get("topicTimer.execute_at");
if (executeAt && this.get("topicTimer.based_on_last_post")) {
time = this.get("topicTimer.duration");
} else if (executeAt) {
const closeTime = moment(executeAt);
if (closeTime > moment()) {
time = closeTime.format("YYYY-MM-DD HH:mm");
}
}
this.set("topicTimer.updateTime", time);
},
@observes("selection")
_updateBasedOnLastPost() {
if (!this.autoClose) {
schedule("afterRender", () => {
this.set("topicTimer.based_on_last_post", false);
});
}
},
didReceiveAttrs() {
this._super(...arguments);
// TODO: get rid of this hack
schedule("afterRender", () => {
if (!this.get("topicTimer.status_type")) {
this.set(
"topicTimer.status_type",
this.get("timerTypes.firstObject.id")
);
}
});
},
actions: {
onChangeTimerType(value) {
this.set("topicTimer.status_type", value);
}
}
});