2023-11-07 12:07:33 -05:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
|
|
|
import { not, or } from "@ember/object/computed";
|
2019-04-18 17:52:59 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { propertyNotEqual } from "discourse/lib/computed";
|
|
|
|
import { bufferedProperty } from "discourse/mixins/buffered-content";
|
2023-11-07 12:07:33 -05:00
|
|
|
import I18n from "I18n";
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-03-06 13:52:19 -05:00
|
|
|
export default Controller.extend(bufferedProperty("model"), {
|
|
|
|
adminPluginsHouseAds: controller("adminPlugins.houseAds"),
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
saving: false,
|
|
|
|
savingStatus: "",
|
|
|
|
|
|
|
|
nameDirty: propertyNotEqual("buffered.name", "model.name"),
|
|
|
|
htmlDirty: propertyNotEqual("buffered.html", "model.html"),
|
2023-04-07 10:56:22 -04:00
|
|
|
visibleToAnonsDirty: propertyNotEqual(
|
|
|
|
"buffered.visible_to_anons",
|
|
|
|
"model.visible_to_anons"
|
|
|
|
),
|
|
|
|
visibleToLoggedInDirty: propertyNotEqual(
|
|
|
|
"buffered.visible_to_logged_in_users",
|
|
|
|
"model.visible_to_logged_in_users"
|
|
|
|
),
|
|
|
|
dirty: or(
|
|
|
|
"nameDirty",
|
|
|
|
"htmlDirty",
|
|
|
|
"visibleToLoggedInDirty",
|
|
|
|
"visibleToAnonsDirty"
|
|
|
|
),
|
2022-03-06 13:52:19 -05:00
|
|
|
disableSave: not("dirty"),
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
actions: {
|
|
|
|
save() {
|
|
|
|
if (!this.get("saving")) {
|
|
|
|
this.setProperties({
|
|
|
|
saving: true,
|
2020-09-04 07:24:14 -04:00
|
|
|
savingStatus: I18n.t("saving"),
|
2019-04-18 17:52:59 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const data = {},
|
|
|
|
buffered = this.get("buffered"),
|
|
|
|
newRecord = !buffered.get("id");
|
|
|
|
|
|
|
|
if (!newRecord) {
|
|
|
|
data.id = buffered.get("id");
|
|
|
|
}
|
|
|
|
data.name = buffered.get("name");
|
|
|
|
data.html = buffered.get("html");
|
2023-04-07 10:56:22 -04:00
|
|
|
data.visible_to_logged_in_users = buffered.get(
|
|
|
|
"visible_to_logged_in_users"
|
|
|
|
);
|
|
|
|
data.visible_to_anons = buffered.get("visible_to_anons");
|
2019-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
ajax(
|
|
|
|
newRecord
|
2019-10-08 17:41:13 -04:00
|
|
|
? `/admin/plugins/pluginad/house_creatives`
|
|
|
|
: `/admin/plugins/pluginad/house_creatives/${buffered.get("id")}`,
|
2019-04-18 17:52:59 -04:00
|
|
|
{
|
|
|
|
type: newRecord ? "POST" : "PUT",
|
2020-09-04 07:24:14 -04:00
|
|
|
data,
|
2019-04-18 17:52:59 -04:00
|
|
|
}
|
|
|
|
)
|
2020-09-04 07:24:14 -04:00
|
|
|
.then((ajaxData) => {
|
2019-04-18 17:52:59 -04:00
|
|
|
this.commitBuffer();
|
|
|
|
this.set("savingStatus", I18n.t("saved"));
|
|
|
|
if (newRecord) {
|
|
|
|
const model = this.get("model");
|
2019-06-14 12:10:11 -04:00
|
|
|
model.set("id", ajaxData.house_ad.id);
|
2019-04-18 17:52:59 -04:00
|
|
|
const houseAds = this.get("adminPluginsHouseAds.model");
|
|
|
|
if (!houseAds.includes(model)) {
|
|
|
|
houseAds.pushObject(model);
|
|
|
|
}
|
|
|
|
this.transitionToRoute(
|
|
|
|
"adminPlugins.houseAds.show",
|
|
|
|
model.get("id")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.setProperties({
|
|
|
|
saving: false,
|
2020-09-04 07:24:14 -04:00
|
|
|
savingStatus: "",
|
2019-04-18 17:52:59 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.rollbackBuffer();
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
const houseAds = this.get("adminPluginsHouseAds.model");
|
|
|
|
const model = this.get("model");
|
|
|
|
|
|
|
|
if (!model.get("id")) {
|
|
|
|
this.transitionToRoute("adminPlugins.houseAds.index");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-08 17:41:13 -04:00
|
|
|
ajax(`/admin/plugins/pluginad/house_creatives/${model.get("id")}`, {
|
2020-09-04 07:24:14 -04:00
|
|
|
type: "DELETE",
|
2019-04-18 17:52:59 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
houseAds.removeObject(model);
|
|
|
|
this.transitionToRoute("adminPlugins.houseAds.index");
|
|
|
|
})
|
2022-10-18 13:58:55 -04:00
|
|
|
.catch(popupAjaxError);
|
2020-09-04 07:24:14 -04:00
|
|
|
},
|
|
|
|
},
|
2019-04-18 17:52:59 -04:00
|
|
|
});
|