Skip to content

Commit

Permalink
FEATURE: Add group and category conditions to an ad
Browse files Browse the repository at this point in the history
  • Loading branch information
janzenisaac committed Apr 5, 2024
1 parent c4227de commit 1c8db02
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CategorySelector from "select-kit/components/category-selector";

export default class HouseAdsCategorySelector extends CategorySelector {
get value() {
return this.selectedCategories.map((c) => c.id);
}
}
Original file line number Diff line number Diff line change
@@ -1,115 +1,148 @@
import Controller, { inject as controller } from "@ember/controller";

Check failure on line 1 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

Run autofix to sort these imports!
import { not, or } from "@ember/object/computed";

Check failure on line 2 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

'not' is defined but never used

Check failure on line 2 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

'or' is defined but never used
import { inject as service } from "@ember/service";
import { service } from "@ember/service";
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";
import I18n from "I18n";
import { TrackedArray, TrackedObject } from "@ember-compat/tracked-built-ins";

Check failure on line 7 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

'TrackedArray' is defined but never used
import { tracked } from "@glimmer/tracking";
import adminPluginsHouseAds from "../routes/admin-plugins-house-ads";

Check failure on line 9 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

'adminPluginsHouseAds' is defined but never used
import { action } from "@ember/object";

export default Controller.extend(bufferedProperty("model"), {
adminPluginsHouseAds: controller("adminPlugins.houseAds"),
router: service(),

saving: false,
savingStatus: "",

nameDirty: propertyNotEqual("buffered.name", "model.name"),
htmlDirty: propertyNotEqual("buffered.html", "model.html"),
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"
),
disableSave: not("dirty"),

actions: {
save() {
if (!this.get("saving")) {
this.setProperties({
saving: true,
savingStatus: I18n.t("saving"),
});
export default class adminPluginsHouseAdsShow extends Controller {
@service router;

@controller("adminPlugins.houseAds") foo;

@tracked selectedCategories = this.model?.categories || [];
@tracked selectedGroups = this.model?.group_ids || [];
@tracked buffered = new TrackedObject(this.model);
@tracked saving = false;
@tracked savingStatus = "";
get nameDirty() {
return this.buffered.name !== this.model?.name;
}
get htmlDirty() {
return this.buffered.html !== this.model?.html;
}
get visibleToAnonsDirty() {
return this.buffered.visible_to_anons !== this.model?.visible_to_anons;
}
get visibleToLoggedInDirty() {
return (
this.buffered.visible_to_logged_in_users !==
this.model?.visible_to_logged_in_users
);
}
get categoryIdsDirty() {
return this.buffered.category_ids !== this.model?.category_ids;
}
get groupIdsDirty() {
return this.buffered.group_ids !== this.model?.group_ids;
}
get dirty() {
return (
this.nameDirty ||
this.htmlDirty ||
this.visibleToLoggedInDirty ||
this.visibleToAnonsDirty ||
this.categoryIdsDirty ||
this.groupIdsDirty
);
}
const data = {},
buffered = this.get("buffered"),
newRecord = !buffered.get("id");
get disableSave() {
return !this.dirty;
}
if (!newRecord) {
data.id = buffered.get("id");
@action
save() {
debugger;

Check failure on line 67 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected 'debugger' statement
if (!this.saving) {
this.saving = true;
this.savingStatus = I18n.t("saving");
const data = {},
buffered = this.buffered,
newRecord = !buffered.id;
if (!newRecord) {
data.id = buffered.id;
}
data.name = buffered.name;
data.html = buffered.html;
data.visible_to_logged_in_users = buffered.visible_to_logged_in_users;
data.visible_to_anons = buffered.visible_to_anons;
data.category_ids = buffered.category_ids;
data.group_ids = buffered.group_ids;
ajax(
newRecord
? `/admin/plugins/pluginad/house_creatives`
: `/admin/plugins/pluginad/house_creatives/${buffered.id}`,
{
type: newRecord ? "POST" : "PUT",
data,
}
data.name = buffered.get("name");
data.html = buffered.get("html");
data.visible_to_logged_in_users = buffered.get(
"visible_to_logged_in_users"
);
data.visible_to_anons = buffered.get("visible_to_anons");

ajax(
newRecord
? `/admin/plugins/pluginad/house_creatives`
: `/admin/plugins/pluginad/house_creatives/${buffered.get("id")}`,
{
type: newRecord ? "POST" : "PUT",
data,
}
)
.then((ajaxData) => {
this.commitBuffer();
this.set("savingStatus", I18n.t("saved"));
if (newRecord) {
const model = this.get("model");
model.set("id", ajaxData.house_ad.id);
const houseAds = this.get("adminPluginsHouseAds.model");
if (!houseAds.includes(model)) {
houseAds.pushObject(model);
}
this.router.transitionTo(
"adminPlugins.houseAds.show",
model.get("id")
);
)
.then((ajaxData) => {
this.savingStatus = I18n.t("saved");
if (newRecord) {
const model = this.model;
model.set("id", ajaxData.house_ad.id);
const houseAds = this.foo.get("model");
if (!houseAds.includes(model)) {
houseAds.pushObject(model);
}
})
.catch(popupAjaxError)
.finally(() => {
this.setProperties({
saving: false,
savingStatus: "",
});
});
}
},
this.router.transitionTo("adminPlugins.houseAds.show", model.id);
}
})
.catch(popupAjaxError)
.finally(() => {
this.set("model", this.buffered);
this.saving = false;
this.savingStatus = "";
});
}
}
cancel() {
this.rollbackBuffer();
},
@action
setCategoryIds(categoryArray) {
this.selectedCategories = categoryArray;
this.buffered.category_ids = categoryArray.map((c) => c.id);
}
destroy() {
const houseAds = this.get("adminPluginsHouseAds.model");
const model = this.get("model");
@action
setGroupIds(groupIds) {
this.selectedGroups = groupIds;
this.buffered.group_ids = groupIds.map((id) => parseInt(id));

Check failure on line 122 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js

View workflow job for this annotation

GitHub Actions / ci / linting

Missing radix parameter
}
if (!model.get("id")) {
this.router.transitionTo("adminPlugins.houseAds.index");
return;
}
@action
cancel(e) {
e.preventDefault();
this.buffered = new TrackedObject(this.model);
}
ajax(`/admin/plugins/pluginad/house_creatives/${model.get("id")}`, {
type: "DELETE",
@action
destroy() {
const houseAds = this.foo.get("model");
const model = this.model;
if (!model.id) {
this.router.transitionTo("adminPlugins.houseAds.index");
return;
}
ajax(`/admin/plugins/pluginad/house_creatives/${model.id}`, {
type: "DELETE",
})
.then(() => {
houseAds.removeObject(model);
this.router.transitionTo("adminPlugins.houseAds.index");
})
.then(() => {
houseAds.removeObject(model);
this.router.transitionTo("adminPlugins.houseAds.index");
})
.catch(popupAjaxError);
},
},
});
.catch(popupAjaxError);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default DiscourseRoute.extend({
},

setupController(controller, model) {
console.log(model);

Check failure on line 16 in admin/assets/javascripts/discourse/routes/admin-plugins-house-ads.js

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected console statement
controller.setProperties({
model,
houseAdsSettings: this.get("settings"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,34 @@
/>
<span>{{i18n "admin.adplugin.house_ads.show_to_anons"}}</span>
</div>

{{log this.model}}

Check failure on line 26 in admin/assets/javascripts/discourse/templates/admin/plugins-house-ads-show.hbs

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected {{log}} usage.
{{log this.buffered}}

Check failure on line 27 in admin/assets/javascripts/discourse/templates/admin/plugins-house-ads-show.hbs

View workflow job for this annotation

GitHub Actions / ci / linting

Unexpected {{log}} usage.
<HouseAdsCategorySelector
@categories={{this.site.categories}}
@selectedCategories={{this.selectedCategories}}
@onChange={{this.setCategoryIds}}
@options={{hash allowAny=true}}
class="house-ads-categories"
/>
<div class="banner-ad-description">
{{i18n "discourse_group_category_banner_ads.category_ids.description"}}
</div>

<GroupChooser
@content={{this.site.groups}}
@onChange={{this.setGroupIds}}
@value={{this.selectedGroups}}
class="banner-groups"
/>
<div class="banner-ad-description">
{{i18n "discourse_group_category_banner_ads.group_ids.description"}}
</div>
</div>

<DButton
@action={{action "save"}}
@disabled={{disableSave}}
@action={{this.save}}
@disabled={{this.disableSave}}
@label="admin.adplugin.house_ads.save"
class="btn-primary save-button"
/>
Expand All @@ -35,12 +58,12 @@
{{savingStatus}}
{{else}}
{{#if dirty}}
<a href {{action "cancel"}}>{{i18n "cancel"}}</a>
<a href {{on "click" this.cancel}}>{{i18n "cancel"}}</a>
{{/if}}
{{/if}}

<DButton
@action={{action "destroy"}}
@action={{this.destroy}}
@label="admin.adplugin.house_ads.delete"
class="btn-danger delete-button"
/>
Expand Down
18 changes: 15 additions & 3 deletions app/controllers/house_ads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@ class HouseAdsController < ::ApplicationController
requires_plugin AdPlugin.plugin_name

def index
render_json_dump(house_ads: HouseAd.all.map(&:to_hash), settings: HouseAdSetting.all)
render_json_dump(
house_ads:
HouseAd.all.map do |ad|
ad.to_hash.merge!(
categories: Category.secured(@guardian).where(id: ad.category_ids),
)
end,
settings: HouseAdSetting.all,
)
end

def show
render_json_dump(house_ad: HouseAd.find(params[:id])&.to_hash)
house_ad_hash = HouseAd.find(params[:id])&.to_hash
if house_ad_hash
house_ad_hash.merge!(categories: Category.secured(@guardian).where(id: house_ad_hash[:category_ids]))
end
render_json_dump(house_ad: house_ad_hash)
end

def create
Expand Down Expand Up @@ -41,7 +53,7 @@ def house_ad_params
@permitted ||=
begin
permitted =
params.permit(:id, :name, :html, :visible_to_anons, :visible_to_logged_in_users)
params.permit(:id, :name, :html, :visible_to_anons, :visible_to_logged_in_users, group_ids: [], category_ids: [])
permitted[:visible_to_logged_in_users] = ActiveModel::Type::Boolean.new.cast(
permitted[:visible_to_logged_in_users],
)
Expand Down
Loading

0 comments on commit 1c8db02

Please sign in to comment.