2024-04-09 13:54:11 -04:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2023-11-07 12:07:33 -05:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2024-04-09 13:54:11 -04:00
|
|
|
import EmberObject, { action } from "@ember/object";
|
|
|
|
import { service } from "@ember/service";
|
|
|
|
import { TrackedObject } from "@ember-compat/tracked-built-ins";
|
|
|
|
import { observes } from "@ember-decorators/object";
|
2019-04-18 17:52:59 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2024-04-09 13:54:11 -04:00
|
|
|
import Category from "discourse/models/category";
|
2023-11-07 12:07:33 -05:00
|
|
|
import I18n from "I18n";
|
2024-04-09 13:54:11 -04:00
|
|
|
import Preview from "../components/modal/preview";
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
export default class adminPluginsHouseAdsShow extends Controller {
|
|
|
|
@service router;
|
|
|
|
@service modal;
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@controller("adminPlugins.houseAds") houseAdsController;
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@tracked selectedCategories = [];
|
|
|
|
@tracked selectedGroups = [];
|
|
|
|
@tracked saving = false;
|
|
|
|
@tracked savingStatus = "";
|
|
|
|
@tracked buffered;
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@observes("model")
|
|
|
|
modelChanged() {
|
|
|
|
this.buffered = new TrackedObject({ ...this.model });
|
|
|
|
this.selectedCategories = this.model.categories || [];
|
|
|
|
this.selectedGroups = this.model.group_ids || [];
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
get disabledSave() {
|
|
|
|
for (const key in this.buffered) {
|
|
|
|
// we don't want to compare the categories array
|
|
|
|
if (key !== "categories" && this.buffered[key] !== this.model[key]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@action
|
|
|
|
async save() {
|
|
|
|
if (!this.saving) {
|
|
|
|
this.saving = true;
|
|
|
|
this.savingStatus = I18n.t("saving");
|
|
|
|
const data = {};
|
|
|
|
const newRecord = !this.buffered.id;
|
|
|
|
if (!newRecord) {
|
|
|
|
data.id = this.buffered.id;
|
|
|
|
}
|
|
|
|
data.name = this.buffered.name;
|
|
|
|
data.html = this.buffered.html;
|
|
|
|
data.visible_to_logged_in_users =
|
|
|
|
this.buffered.visible_to_logged_in_users;
|
|
|
|
data.visible_to_anons = this.buffered.visible_to_anons;
|
|
|
|
data.category_ids = this.buffered.category_ids;
|
|
|
|
data.group_ids = this.buffered.group_ids;
|
|
|
|
try {
|
|
|
|
const ajaxData = await ajax(
|
2019-04-18 17:52:59 -04:00
|
|
|
newRecord
|
2019-10-08 17:41:13 -04:00
|
|
|
? `/admin/plugins/pluginad/house_creatives`
|
2024-04-09 13:54:11 -04:00
|
|
|
: `/admin/plugins/pluginad/house_creatives/${this.buffered.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
|
|
|
}
|
2024-04-09 13:54:11 -04:00
|
|
|
);
|
|
|
|
this.savingStatus = I18n.t("saved");
|
|
|
|
const houseAds = this.houseAdsController.model;
|
|
|
|
if (newRecord) {
|
|
|
|
this.buffered.id = ajaxData.house_ad.id;
|
|
|
|
if (!houseAds.includes(this.buffered)) {
|
|
|
|
houseAds.pushObject(EmberObject.create(this.buffered));
|
|
|
|
}
|
|
|
|
this.router.transitionTo(
|
|
|
|
"adminPlugins.houseAds.show",
|
|
|
|
this.buffered.id
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
houseAds
|
|
|
|
.find((ad) => ad.id === this.buffered.id)
|
|
|
|
.setProperties(this.buffered);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
popupAjaxError(error);
|
|
|
|
} finally {
|
|
|
|
this.set("model", this.buffered);
|
|
|
|
this.saving = false;
|
|
|
|
this.savingStatus = "";
|
2019-04-18 17:52:59 -04:00
|
|
|
}
|
2024-04-09 13:54:11 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@action
|
|
|
|
setCategoryIds(categoryArray) {
|
|
|
|
this.selectedCategories = categoryArray;
|
|
|
|
this.buffered.category_ids = categoryArray.map((c) => c.id);
|
|
|
|
this.setCategoriesForBuffered();
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@action
|
|
|
|
setGroupIds(groupIds) {
|
|
|
|
this.selectedGroups = groupIds;
|
|
|
|
this.buffered.group_ids = groupIds.map((id) => id);
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@action
|
|
|
|
cancel() {
|
|
|
|
this.buffered = new TrackedObject({ ...this.model });
|
|
|
|
this.selectedCategories = this.model.categories || [];
|
|
|
|
this.selectedGroups = this.model.group_ids || [];
|
|
|
|
this.setCategoriesForBuffered();
|
|
|
|
}
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2024-04-09 13:54:11 -04:00
|
|
|
@action
|
|
|
|
async destroy() {
|
|
|
|
if (!this.buffered.id) {
|
|
|
|
this.router.transitionTo("adminPlugins.houseAds.index");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await ajax(
|
|
|
|
`/admin/plugins/pluginad/house_creatives/${this.buffered.id}`,
|
|
|
|
{
|
|
|
|
type: "DELETE",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
this.houseAdsController.model.removeObject(
|
|
|
|
this.houseAdsController.model.findBy("id", this.buffered.id)
|
|
|
|
);
|
|
|
|
this.router.transitionTo("adminPlugins.houseAds.index");
|
|
|
|
} catch (error) {
|
|
|
|
popupAjaxError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
openPreview() {
|
|
|
|
this.modal.show(Preview, {
|
|
|
|
model: {
|
|
|
|
html: this.buffered.html,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setCategoriesForBuffered() {
|
|
|
|
// we need to fetch the categories because the serializer is not being used
|
|
|
|
// to attach the category object to the house ads
|
|
|
|
this.buffered.categories = this.buffered.category_ids
|
|
|
|
? this.buffered.category_ids.map((categoryId) =>
|
|
|
|
Category.findById(categoryId)
|
|
|
|
)
|
|
|
|
: [];
|
|
|
|
}
|
|
|
|
}
|