A platform for community discussion. Free, open, simple.
FEATURE: redesign house ads admin UI with FormKit (#37799)
Samede2ffbd8646
diff --git a/frontend/discourse/app/components/nav-item.gjs b/frontend/discourse/app/components/nav-item.gjsindex 23c07d3743c..def51ae525d 100644--- a/frontend/discourse/app/components/nav-item.gjs+++ b/frontend/discourse/app/components/nav-item.gjs@@ -11,6 +11,7 @@ import { i18n } from "discourse-i18n";export default class NavItem extends Component {@service router;+ @service session;get contents() {const text = this.args.i18nLabel || i18n(this.args.label);@@ -69,9 +70,24 @@ export default class NavItem extends Component {return this.router.isActive(this.args.route);}+ get refreshHref() {+ if (!this.session.requiresRefresh || !this.args.route) {+ return null;+ }+ try {+ return this.args.routeParam+ ? getURL(this.router.urlFor(this.args.route, this.args.routeParam))+ : getURL(this.router.urlFor(this.args.route));+ } catch {+ return null;+ }+ }+<template><li class={{concatClass (if this.active "active") @class}} ...attributes>- {{#if @routeParam}}+ {{#if this.refreshHref}}+ <a href={{this.refreshHref}}>{{this.contents}}</a>+ {{else if @routeParam}}<LinkTo@route={{@route}}@model={{@routeParam}}diff --git a/frontend/discourse/app/models/session.js b/frontend/discourse/app/models/session.jsindex 6557f411f62..de1f786a5ea 100644--- a/frontend/discourse/app/models/session.js+++ b/frontend/discourse/app/models/session.js@@ -1,3 +1,4 @@+import { tracked } from "@glimmer/tracking";import singleton from "discourse/lib/singleton";import RestModel from "discourse/models/rest";@@ -5,6 +6,7 @@ import RestModel from "discourse/models/rest";// data here you might want later. It is not stored or serialized anywhere.@singletonexport default class Session extends RestModel {+ @tracked requiresRefresh = false;hasFocus = null;init() {diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/adplugin-route-map.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/adplugin-route-map.jsindex 8936b1eb5fd..fb17dc4fb1b 100644--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/adplugin-route-map.js+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/adplugin-route-map.js@@ -1,10 +1,8 @@-/* eslint-disable ember/route-path-style */export default {- resource: "admin.adminPlugins",+ resource: "admin.adminPlugins.show",path: "/plugins",map() {- this.route("houseAds", { path: "/pluginad/house_creatives" }, function () {- this.route("index", { path: "/" });+ this.route("houseAds", { path: "house-ads" }, function () {this.route("show", { path: "/:id" });});},diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjsnew file mode 100644index 00000000000..11ea2a4072a--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ad-form.gjs@@ -0,0 +1,278 @@+import Component from "@glimmer/component";+import { fn } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import Form from "discourse/components/form";+import { ajax } from "discourse/lib/ajax";+import { popupAjaxError } from "discourse/lib/ajax-error";+import { removeValueFromArray } from "discourse/lib/array-tools";+import GroupChooser from "discourse/select-kit/components/group-chooser";+import { i18n } from "discourse-i18n";+import HouseAdsCategorySelector from "./house-ads-category-selector";+import HouseAdsRouteSelector from "./house-ads-route-selector";+import Preview from "./modal/preview";++export default class HouseAdForm extends Component {+ @service router;+ @service modal;+ @service dialog;+ @service toasts;+ @service siteSettings;+ @service site;++ get isNew() {+ return !this.args.model.id;+ }++ get routesEnabled() {+ return this.siteSettings.ad_plugin_routes_enabled;+ }++ get categoryDescription() {+ if (this.routesEnabled) {+ return i18n(+ "admin.adplugin.house_ads.category_chooser_description_with_routes"+ );+ }+ return i18n("admin.adplugin.house_ads.category_chooser_description");+ }++ previewDisabled(data) {+ return !data.html;+ }++ get formData() {+ const model = this.args.model;+ return {+ id: model.id,+ name: model.name,+ html: model.html,+ visible_to_logged_in_users: model.visible_to_logged_in_users,+ visible_to_anons: model.visible_to_anons,+ categories: model.categories || [],+ group_ids: model.groups || [],+ routes: model.routes || [],+ };+ }++ @action+ async save(data) {+ const isNew = !data.id;+ const payload = {+ name: data.name,+ html: data.html,+ visible_to_logged_in_users: data.visible_to_logged_in_users,+ visible_to_anons: data.visible_to_anons,+ category_ids: data.categories ? data.categories.map((c) => c.id) : [],+ group_ids: data.group_ids || [],+ routes: data.routes || [],+ };++ try {+ const result = await ajax(+ isNew+ ? `/admin/plugins/discourse-adplugin/house-ads`+ : `/admin/plugins/discourse-adplugin/house-ads/${data.id}`,+ {+ type: isNew ? "POST" : "PUT",+ data: payload,+ }+ );++ this.toasts.success({+ data: { message: i18n("saved") },+ duration: "short",+ });++ const categories = data.categories ? [...data.categories] : [];+ const groups = data.group_ids ? [...data.group_ids] : [];+ const routes = data.routes ? [...data.routes] : [];++ const houseAds = this.args.houseAds;+ if (isNew) {+ const newAd = {+ id: result.house_ad.id,+ name: data.name,+ html: data.html,+ visible_to_logged_in_users: data.visible_to_logged_in_users,+ visible_to_anons: data.visible_to_anons,+ categories,+ groups,+ routes,+ };+ houseAds.push(newAd);+ this.router.transitionTo(+ "adminPlugins.show.houseAds.show",+ result.house_ad.id+ );+ } else {+ const updatedProps = {+ id: data.id,+ name: data.name,+ html: data.html,+ visible_to_logged_in_users: data.visible_to_logged_in_users,+ visible_to_anons: data.visible_to_anons,+ categories,+ groups,+ routes,+ };+ const existing = houseAds.find((ad) => ad.id === data.id);+ if (existing) {+ Object.assign(existing, updatedProps);+ }+ Object.assign(this.args.model, updatedProps);+ }+ } catch (error) {+ popupAjaxError(error);+ }+ }++ @action+ delete() {+ if (this.isNew) {+ this.router.transitionTo("adminPlugins.show.houseAds.index");+ return;+ }++ return this.dialog.confirm({+ message: i18n("admin.adplugin.house_ads.confirm_delete"),++ didConfirm: async () => {+ try {+ await ajax(+ `/admin/plugins/discourse-adplugin/house-ads/${this.args.model.id}`,+ { type: "DELETE" }+ );+ removeValueFromArray(+ this.args.houseAds,+ this.args.houseAds.find((ad) => ad.id === this.args.model.id)+ );+ this.router.transitionTo("adminPlugins.show.houseAds.index");+ } catch (error) {+ popupAjaxError(error);+ }+ },+ });+ }++ @action+ openPreview(data) {+ this.modal.show(Preview, {+ model: { html: data.html },+ });+ }++ <template>+ <Form+ @onSubmit={{this.save}}+ @data={{this.formData}}+ class="house-ad-form"+ as |form data|+ >+ <form.Field+ @name="name"+ @title={{i18n "admin.adplugin.house_ads.name"}}+ @validation="required"+ @format="large"+ as |field|+ >+ <field.Input />+ </form.Field>++ <form.Field+ @name="html"+ @title={{i18n "admin.adplugin.house_ads.html"}}+ @validation="required"+ @format="full"+ as |field|+ >+ <field.Code @lang="html" @height={{270}} />+ </form.Field>++ <form.Field+ @name="visible_to_logged_in_users"+ @title={{i18n "admin.adplugin.house_ads.show_to_logged_in_users"}}+ as |field|+ >+ <field.Checkbox />+ </form.Field>++ <form.Field+ @name="visible_to_anons"+ @title={{i18n "admin.adplugin.house_ads.show_to_anons"}}+ as |field|+ >+ <field.Checkbox />+ </form.Field>++ <form.Field+ @name="categories"+ @title={{i18n "admin.adplugin.house_ads.categories"}}+ @description={{this.categoryDescription}}+ @format="large"+ as |field|+ >+ <field.Custom>+ <HouseAdsCategorySelector+ @categories={{this.site.categories}}+ @selectedCategories={{field.value}}+ @onChange={{field.set}}+ />+ </field.Custom>+ </form.Field>++ {{#if this.routesEnabled}}+ <form.Field+ @name="routes"+ @title={{i18n "admin.adplugin.house_ads.routes"}}+ @description={{i18n+ "admin.adplugin.house_ads.route_chooser_description"+ }}+ @format="large"+ as |field|+ >+ <field.Custom>+ <HouseAdsRouteSelector+ @value={{field.value}}+ @onChange={{field.set}}+ />+ </field.Custom>+ </form.Field>+ {{/if}}++ <form.Field+ @name="group_ids"+ @title={{i18n "admin.adplugin.house_ads.groups"}}+ @description={{i18n+ "admin.adplugin.house_ads.group_chooser_description"+ }}+ @format="large"+ as |field|+ >+ <field.Custom>+ <GroupChooser+ @content={{this.site.groups}}+ @value={{field.value}}+ @onChange={{field.set}}+ />+ </field.Custom>+ </form.Field>++ <form.Actions>+ <form.Submit @label="admin.adplugin.house_ads.save" />+ <form.Button+ @action={{fn this.openPreview data}}+ @disabled={{this.previewDisabled data}}+ @label="admin.adplugin.house_ads.preview"+ />+ {{#unless this.isNew}}+ <form.Button+ @action={{this.delete}}+ @label="admin.adplugin.house_ads.delete"+ class="btn-danger"+ />+ {{/unless}}+ </form.Actions>+ </Form>+ </template>+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-list.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-list.gjsnew file mode 100644index 00000000000..0c53055437f--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-list.gjs@@ -0,0 +1,33 @@+import { LinkTo } from "@ember/routing";+import { i18n } from "discourse-i18n";++const HouseAdsList = <template>+ <table class="d-admin-table house-ads-table" ...attributes>+ <thead>+ <tr>+ <th>{{i18n "admin.adplugin.house_ads.name"}}</th>+ <th></th>+ </tr>+ </thead>+ <tbody>+ {{#each @houseAds as |ad|}}+ <tr class="d-admin-row__content" data-house-ad-id={{ad.id}}>+ <td class="d-admin-row__overview">+ {{ad.name}}+ </td>+ <td class="d-admin-row__controls">+ <LinkTo+ @route="adminPlugins.show.houseAds.show"+ @model={{ad.id}}+ class="btn btn-small btn-default house-ads-table__edit"+ >+ {{i18n "admin.adplugin.house_ads.edit"}}+ </LinkTo>+ </td>+ </tr>+ {{/each}}+ </tbody>+ </table>+</template>;++export default HouseAdsList;diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-setting.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-setting.gjsindex 0645a8a46b5..d22fa0f66aa 100644--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-setting.gjs+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-setting.gjs@@ -37,7 +37,7 @@ export default class HouseAdsSetting extends Component {try {await ajax(- `/admin/plugins/pluginad/house_settings/${this.get("name")}.json`,+ `/admin/plugins/discourse-adplugin/house-settings/${this.get("name")}`,{type: "PUT",data: { value: this.get("adValue") },diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-settings-panel.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-settings-panel.gjsnew file mode 100644index 00000000000..928e1620eb7--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/admin/components/house-ads-settings-panel.gjs@@ -0,0 +1,48 @@+import DButton from "discourse/components/d-button";+import HouseAdsListSetting from "./house-ads-list-setting";++const HouseAdsSettingsPanel = <template>+ <section class="house-ads-settings" ...attributes>+ <form class="form-horizontal">+ <HouseAdsListSetting+ @name="topic_list_top"+ @value={{@adSettings.topic_list_top}}+ @allAds={{@houseAds}}+ @adSettings={{@adSettings}}+ />+ <HouseAdsListSetting+ @name="topic_above_post_stream"+ @value={{@adSettings.topic_above_post_stream}}+ @allAds={{@houseAds}}+ @adSettings={{@adSettings}}+ />+ <HouseAdsListSetting+ @name="topic_above_suggested"+ @value={{@adSettings.topic_above_suggested}}+ @allAds={{@houseAds}}+ @adSettings={{@adSettings}}+ />+ <HouseAdsListSetting+ @name="post_bottom"+ @value={{@adSettings.post_bottom}}+ @allAds={{@houseAds}}+ @adSettings={{@adSettings}}+ />+ <HouseAdsListSetting+ @name="topic_list_between"+ @value={{@adSettings.topic_list_between}}+ @allAds={{@houseAds}}+ @adSettings={{@adSettings}}+ />++ <DButton+ @label="admin.adplugin.house_ads.more_settings"+ @icon="gear"+ @action={{@onMoreSettings}}+ class="btn-default"+ />+ </form>+ </section>+</template>;++export default HouseAdsSettingsPanel;diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads.jsdeleted file mode 100644index bf16782d537..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads.js+++ /dev/null@@ -1,5 +0,0 @@-import Controller from "@ember/controller";--export default class AdminPluginsHouseAdsController extends Controller {- loadingAds = true;-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/index.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/index.jsdeleted file mode 100644index dac19e99a7e..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/index.js+++ /dev/null@@ -1,9 +0,0 @@-import Controller, { inject as controller } from "@ember/controller";-import { alias } from "@ember/object/computed";--export default class AdminPluginsHouseAdsIndexController extends Controller {- @controller("adminPlugins.houseAds") adminPluginsHouseAds;-- @alias("adminPluginsHouseAds.model") houseAds;- @alias("adminPluginsHouseAds.houseAdsSettings") adSettings;-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/show.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/show.jsdeleted file mode 100644index d0039521d7f..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/controllers/admin-plugins/house-ads/show.js+++ /dev/null@@ -1,165 +0,0 @@-/* eslint-disable ember/no-observers */-import { tracked } from "@glimmer/tracking";-import Controller, { inject as controller } from "@ember/controller";-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";-import { ajax } from "discourse/lib/ajax";-import { popupAjaxError } from "discourse/lib/ajax-error";-import { removeValueFromArray } from "discourse/lib/array-tools";-import { i18n } from "discourse-i18n";-import Preview from "../../../components/modal/preview";--export default class adminPluginsHouseAdsShow extends Controller {- @service router;- @service modal;- @service siteSettings;-- @controller("adminPlugins.houseAds") houseAdsController;-- @tracked selectedCategories = [];- @tracked selectedGroups = [];- @tracked selectedRoutes = [];- @tracked saving = false;- @tracked savingStatus = "";- @tracked buffered;-- @observes("model")- modelChanged() {- this.buffered = new TrackedObject({ ...this.model });- this.selectedCategories = this.model.categories || [];- this.selectedGroups = this.model.groups || [];- this.selectedRoutes = this.model.routes || [];- }-- get disabledSave() {- for (const key in this.buffered) {- if (this.buffered[key] !== this.model[key]) {- return false;- }- }- return true;- }-- get routesEnabled() {- return this.siteSettings.ad_plugin_routes_enabled;- }-- @action- async save() {- if (!this.saving) {- this.saving = true;- this.savingStatus = i18n("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.categories- ? this.buffered.categories.map((c) => c.id)- : [];- data.group_ids = this.buffered.groups- ? this.buffered.groups.map((g) => g.id)- : [];- data.routes = this.buffered.routes || [];- try {- const ajaxData = await ajax(- newRecord- ? `/admin/plugins/pluginad/house_creatives`- : `/admin/plugins/pluginad/house_creatives/${this.buffered.id}`,- {- type: newRecord ? "POST" : "PUT",- data,- }- );- this.savingStatus = i18n("saved");- const houseAds = this.houseAdsController.model;- if (newRecord) {- this.buffered.id = ajaxData.house_ad.id;- if (!houseAds.includes(this.buffered)) {- houseAds.push(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 = "";- }- }- }-- @action- setCategoryIds(categoryArray) {- this.selectedCategories = categoryArray;- this.buffered.categories = this.selectedCategories;- }-- @action- setGroupIds(groupIds) {- this.selectedGroups = groupIds;- this.buffered.groups = groupIds;- }-- @action- setRoutes(routes) {- this.selectedRoutes = routes;- this.buffered.routes = routes;- }-- @action- cancel() {- this.buffered = new TrackedObject({ ...this.model });- this.selectedCategories = this.model.categories || [];- this.selectedGroups = this.model.groups || [];- }-- @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",- }- );- removeValueFromArray(- this.houseAdsController.model,- this.houseAdsController.model.find(- (item) => item.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,- },- });- }-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads.jsdeleted file mode 100644index 2ad1b206f98..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads.js+++ /dev/null@@ -1,25 +0,0 @@-import EmberObject from "@ember/object";-import { TrackedArray } from "@ember-compat/tracked-built-ins";-import { ajax } from "discourse/lib/ajax";-import DiscourseRoute from "discourse/routes/discourse";--export default class AdminPluginsHouseAds extends DiscourseRoute {- settings = null;-- model() {- return ajax("/admin/plugins/pluginad/house_creatives.json").then((data) => {- this.set("settings", EmberObject.create(data.settings));- return new TrackedArray(- data.house_ads.map((ad) => EmberObject.create(ad))- );- });- }-- setupController(controller, model) {- controller.setProperties({- model,- houseAdsSettings: this.get("settings"),- loadingAds: false,- });- }-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/index.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/index.jsdeleted file mode 100644index 8a9bdd36146..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/index.js+++ /dev/null@@ -1,12 +0,0 @@-import { action } from "@ember/object";-import { service } from "@ember/service";-import DiscourseRoute from "discourse/routes/discourse";--export default class AdminPluginsHouseAdsIndex extends DiscourseRoute {- @service router;-- @action- moreSettings() {- this.router.transitionTo("adminSiteSettingsCategory", "ad_plugin");- }-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/show.js b/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/show.jsdeleted file mode 100644index 6b9316b66f7..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/routes/admin-plugins/house-ads/show.js+++ /dev/null@@ -1,28 +0,0 @@-import { TrackedObject } from "@ember-compat/tracked-built-ins";-import DiscourseRoute from "discourse/routes/discourse";-import { i18n } from "discourse-i18n";--export default class AdminPluginsHouseAdsShow extends DiscourseRoute {- model(params) {- if (params.id === "new") {- return new TrackedObject({- name: i18n("admin.adplugin.house_ads.new_name"),- html: "",- visible_to_logged_in_users: true,- visible_to_anons: true,- });- } else {- const houseAd = this.modelFor("adminPlugins.houseAds").find(- (item) => item.id === parseInt(params.id, 10)- );-- if (houseAd.groups && Array.isArray(houseAd.groups)) {- houseAd.groups = houseAd.groups.map((g) => {- return g.id;- });- }-- return new TrackedObject(houseAd);- }- }-}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads.gjsdeleted file mode 100644index 77a87930997..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads.gjs+++ /dev/null@@ -1,37 +0,0 @@-import { LinkTo } from "@ember/routing";-import icon from "discourse/helpers/d-icon";-import { i18n } from "discourse-i18n";--export default <template>- <div class="adplugin-mgmt">- <h1>{{i18n "admin.adplugin.house_ads.title"}}</h1>- {{#if @controller.model.length}}- <div class="content-list">- <div class="house-ads-actions">- <LinkTo- @route="adminPlugins.houseAds.show"- @model="new"- class="btn btn-primary"- >- {{icon "plus"}}- <span>{{i18n "admin.adplugin.house_ads.new"}}</span>- </LinkTo>- <LinkTo @route="adminPlugins.houseAds.index" class="btn btn-default">- {{icon "gear"}}- <span>{{i18n "admin.adplugin.house_ads.settings"}}</span>- </LinkTo>- </div>- <ul class="house-ads-list">- {{#each @controller.model as |ad|}}- <li class="house-ads-list-item">- {{#LinkTo route="adminPlugins.houseAds.show" model=ad.id}}- {{ad.name}}- {{/LinkTo}}- </li>- {{/each}}- </ul>- </div>- {{/if}}- {{outlet}}- </div>-</template>diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/index.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/index.gjsdeleted file mode 100644index 7247f8398c7..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/index.gjs+++ /dev/null@@ -1,59 +0,0 @@-import { LinkTo } from "@ember/routing";-import DButton from "discourse/components/d-button";-import routeAction from "discourse/helpers/route-action";-import { i18n } from "discourse-i18n";-import HouseAdsListSetting from "../../../components/house-ads-list-setting";--export default <template>- <section class="house-ads-settings content-body">- <div>{{i18n "admin.adplugin.house_ads.description"}}</div>-- {{#if @controller.houseAds.length}}- <form class="form-horizontal">- <HouseAdsListSetting- @name="topic_list_top"- @value={{@controller.adSettings.topic_list_top}}- @allAds={{@controller.houseAds}}- @adSettings={{@controller.adSettings}}- />- <HouseAdsListSetting- @name="topic_above_post_stream"- @value={{@controller.adSettings.topic_above_post_stream}}- @allAds={{@controller.houseAds}}- @adSettings={{@controller.adSettings}}- />- <HouseAdsListSetting- @name="topic_above_suggested"- @value={{@controller.adSettings.topic_above_suggested}}- @allAds={{@controller.houseAds}}- @adSettings={{@controller.adSettings}}- />- <HouseAdsListSetting- @name="post_bottom"- @value={{@controller.adSettings.post_bottom}}- @allAds={{@controller.houseAds}}- @adSettings={{@controller.adSettings}}- />- <HouseAdsListSetting- @name="topic_list_between"- @value={{@controller.adSettings.topic_list_between}}- @allAds={{@controller.houseAds}}- @adSettings={{@controller.adSettings}}- />-- <DButton- @label="admin.adplugin.house_ads.more_settings"- @icon="gear"- @action={{routeAction "moreSettings"}}- class="btn-default"- />- </form>- {{else}}- <p>- {{#LinkTo route="adminPlugins.houseAds.show" model="new"}}- {{i18n "admin.adplugin.house_ads.get_started"}}- {{/LinkTo}}- </p>- {{/if}}- </section>-</template>diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/show.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/show.gjsdeleted file mode 100644index ba0469707ea..00000000000--- a/plugins/discourse-adplugin/admin/assets/javascripts/admin/templates/admin-plugins/house-ads/show.gjs+++ /dev/null@@ -1,111 +0,0 @@-import { Input } from "@ember/component";-import { fn, hash } from "@ember/helper";-import AceEditor from "discourse/components/ace-editor";-import DButton from "discourse/components/d-button";-import TextField from "discourse/components/text-field";-import GroupChooser from "discourse/select-kit/components/group-chooser";-import { i18n } from "discourse-i18n";-import HouseAdsCategorySelector from "../../../components/house-ads-category-selector";-import HouseAdsRouteSelector from "../../../components/house-ads-route-selector";--export default <template>- <section class="edit-house-ad content-body">- <h1><TextField- @value={{@controller.buffered.name}}- class="house-ad-name"- /></h1>- <div class="controls">- <AceEditor- @content={{@controller.buffered.html}}- @onChange={{fn (mut @controller.buffered.html)}}- @mode="html"- />- </div>- <div class="controls">- <div class="visibility-settings">- <div>- <Input- @type="checkbox"- @checked={{@controller.buffered.visible_to_logged_in_users}}- class="visible-to-logged-in-checkbox"- />- <span>{{i18n- "admin.adplugin.house_ads.show_to_logged_in_users"- }}</span>- </div>-- <div>- <Input- class="visible-to-anonymous-checkbox"- @type="checkbox"- @checked={{@controller.buffered.visible_to_anons}}- />- <span>{{i18n "admin.adplugin.house_ads.show_to_anons"}}</span>- </div>-- <HouseAdsCategorySelector- @categories={{@controller.site.categories}}- @selectedCategories={{@controller.selectedCategories}}- @onChange={{@controller.setCategoryIds}}- @options={{hash allowAny=true}}- class="house-ads-categories"- />- <div class="description">- {{#if @controller.routesEnabled}}- {{i18n- "admin.adplugin.house_ads.category_chooser_description_with_routes"- }}- {{else}}- {{i18n "admin.adplugin.house_ads.category_chooser_description"}}- {{/if}}- </div>-- {{#if @controller.routesEnabled}}- <HouseAdsRouteSelector- @value={{@controller.selectedRoutes}}- @onChange={{@controller.setRoutes}}- />- <div class="description">- {{i18n "admin.adplugin.house_ads.route_chooser_description"}}- </div>- {{/if}}-- <GroupChooser- @content={{@controller.site.groups}}- @value={{@controller.selectedGroups}}- @onChange={{@controller.setGroupIds}}- class="banner-groups"- />- <div class="description">- {{i18n "admin.adplugin.house_ads.group_chooser_description"}}- </div>- </div>-- <DButton- @action={{@controller.save}}- @disabled={{@controller.disabledSave}}- @label="admin.adplugin.house_ads.save"- class="btn-primary save-button"- />-- {{#if @controller.saving}}- {{@controller.savingStatus}}- {{else}}- {{#unless @controller.disabledSave}}- <DButton @action={{@controller.cancel}} @label="cancel" />- {{/unless}}- {{/if}}-- <DButton- @action={{@controller.openPreview}}- @label="admin.adplugin.house_ads.preview"- />-- <DButton- @action={{@controller.destroy}}- @label="admin.adplugin.house_ads.delete"- class="btn-danger delete-button"- />- </div>- </section>-</template>diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/index.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/index.jsnew file mode 100644index 00000000000..87e6905943b--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/index.js@@ -0,0 +1,29 @@+import { tracked } from "@glimmer/tracking";+import Controller from "@ember/controller";+import { action } from "@ember/object";+import { service } from "@ember/service";++export default class AdminPluginsShowHouseAdsIndexController extends Controller {+ @service router;++ @tracked currentTab = "ads";++ get houseAds() {+ return this.model.houseAds;+ }++ get adSettings() {+ return this.model.houseAdsSettings;+ }++ @action+ onTabChange(tab, event) {+ event.preventDefault();+ this.currentTab = tab;+ }++ @action+ moreSettings() {+ this.router.transitionTo("adminSiteSettingsCategory", "ad_plugin");+ }+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/show.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/show.jsnew file mode 100644index 00000000000..7a05236a9e6--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/controllers/admin-plugins/show/house-ads/show.js@@ -0,0 +1,6 @@+import { tracked } from "@glimmer/tracking";+import Controller from "@ember/controller";++export default class AdminPluginsShowHouseAdsShowController extends Controller {+ @tracked houseAds;+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads.jsnew file mode 100644index 00000000000..1ece41cdc31--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads.js@@ -0,0 +1,17 @@+import EmberObject from "@ember/object";+import { TrackedArray } from "@ember-compat/tracked-built-ins";+import { ajax } from "discourse/lib/ajax";+import DiscourseRoute from "discourse/routes/discourse";++export default class AdminPluginsShowHouseAds extends DiscourseRoute {+ async model() {+ const data = await ajax("/admin/plugins/discourse-adplugin/house-ads");++ return {+ houseAds: new TrackedArray(+ data.house_ads.map((ad) => EmberObject.create(ad))+ ),+ houseAdsSettings: EmberObject.create(data.settings),+ };+ }+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/index.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/index.jsnew file mode 100644index 00000000000..81fc54c761d--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/index.js@@ -0,0 +1,7 @@+import DiscourseRoute from "discourse/routes/discourse";++export default class AdminPluginsShowHouseAdsIndex extends DiscourseRoute {+ model() {+ return this.modelFor("adminPlugins.show.houseAds");+ }+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.jsnew file mode 100644index 00000000000..b707ac0b348--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/routes/admin-plugins/show/house-ads/show.js@@ -0,0 +1,39 @@+import { TrackedObject } from "@ember-compat/tracked-built-ins";+import DiscourseRoute from "discourse/routes/discourse";++export default class AdminPluginsShowHouseAdsShow extends DiscourseRoute {+ model(params) {+ const parentModel = this.modelFor("adminPlugins.show.houseAds");++ if (params.id === "new") {+ return new TrackedObject({+ name: null,+ html: null,+ visible_to_logged_in_users: true,+ visible_to_anons: true,+ });+ } else {+ const houseAd = parentModel.houseAds.find(+ (item) => item.id === parseInt(params.id, 10)+ );++ if (!houseAd) {+ return this.replaceWith("adminPlugins.show.houseAds.index");+ }++ if (houseAd.groups && Array.isArray(houseAd.groups)) {+ houseAd.groups = houseAd.groups.map((g) =>+ typeof g === "object" && g !== null ? g.id : g+ );+ }++ return new TrackedObject(houseAd);+ }+ }++ setupController(controller, model) {+ super.setupController(controller, model);+ const parentModel = this.modelFor("adminPlugins.show.houseAds");+ controller.houseAds = parentModel.houseAds;+ }+}diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads.gjsnew file mode 100644index 00000000000..06068302726--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads.gjs@@ -0,0 +1,5 @@+export default <template>+ <div class="adplugin-mgmt">+ {{outlet}}+ </div>+</template>diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/index.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/index.gjsnew file mode 100644index 00000000000..ac990e4d468--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/index.gjs@@ -0,0 +1,65 @@+import { fn } from "@ember/helper";+import { on } from "@ember/modifier";+import AdminConfigAreaEmptyList from "discourse/admin/components/admin-config-area-empty-list";+import DPageSubheader from "discourse/components/d-page-subheader";+import HorizontalOverflowNav from "discourse/components/horizontal-overflow-nav";+import { eq } from "discourse/truth-helpers";+import { i18n } from "discourse-i18n";+import HouseAdsList from "../../../../../admin/components/house-ads-list";+import HouseAdsSettingsPanel from "../../../../../admin/components/house-ads-settings-panel";++const HouseAdsIndex = <template>+ <div class="discourse-adplugin__house-ads admin-detail">+ <DPageSubheader+ @titleLabel={{i18n "admin.adplugin.house_ads.title"}}+ @descriptionLabel={{i18n "admin.adplugin.house_ads.description"}}+ >+ <:actions as |actions|>+ <actions.Primary+ @label="admin.adplugin.house_ads.new"+ @route="adminPlugins.show.houseAds.show"+ @routeModels="new"+ @icon="plus"+ />+ </:actions>+ </DPageSubheader>++ {{#if @controller.houseAds.length}}+ <HorizontalOverflowNav>+ <li>+ <a+ href="#"+ class={{if (eq @controller.currentTab "ads") "active"}}+ {{on "click" (fn @controller.onTabChange "ads")}}+ >{{i18n "admin.adplugin.house_ads.tabs.ads"}}</a>+ </li>+ <li>+ <a+ href="#"+ class={{if (eq @controller.currentTab "settings") "active"}}+ {{on "click" (fn @controller.onTabChange "settings")}}+ >{{i18n "admin.adplugin.house_ads.tabs.settings"}}</a>+ </li>+ </HorizontalOverflowNav>++ {{#if (eq @controller.currentTab "ads")}}+ <HouseAdsList @houseAds={{@controller.houseAds}} />+ {{else}}+ <HouseAdsSettingsPanel+ @adSettings={{@controller.adSettings}}+ @houseAds={{@controller.houseAds}}+ @onMoreSettings={{@controller.moreSettings}}+ />+ {{/if}}+ {{else}}+ <AdminConfigAreaEmptyList+ @emptyLabel="admin.adplugin.house_ads.get_started"+ @ctaLabel="admin.adplugin.house_ads.new"+ @ctaRoute="adminPlugins.show.houseAds.show"+ @ctaRouteModels="new"+ />+ {{/if}}+ </div>+</template>;++export default HouseAdsIndex;diff --git a/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/show.gjs b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/show.gjsnew file mode 100644index 00000000000..364d67b8f33--- /dev/null+++ b/plugins/discourse-adplugin/admin/assets/javascripts/discourse/templates/admin-plugins/show/house-ads/show.gjs@@ -0,0 +1,13 @@+import BackButton from "discourse/components/back-button";+import HouseAdForm from "../../../../../admin/components/house-ad-form";++export default <template>+ <BackButton @route="adminPlugins.show.houseAds.index" />++ <div class="house-ad-form-container admin-detail">+ <HouseAdForm+ @model={{@controller.model}}+ @houseAds={{@controller.houseAds}}+ />+ </div>+</template>diff --git a/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb b/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rbindex 04182b8e872..6de876962ab 100644--- a/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb+++ b/plugins/discourse-adplugin/app/controllers/ad_plugin/house_ads_controller.rb@@ -18,18 +18,16 @@ module AdPlugindef showhouse_ad = HouseAd.find_by(id: params[:id])- if house_ad- render_json_dump(- house_ad:- serialize_data(- house_ad,- HouseAdSerializer,- { include_categories: true, include_groups: true },- ),- )- else- render_json_error(I18n.t("not_found"), status: 404)- end+ raise Discourse::NotFound if house_ad.nil?++ render_json_dump(+ house_ad:+ serialize_data(+ house_ad,+ HouseAdSerializer,+ { include_categories: true, include_groups: true },+ ),+ )enddef create@@ -45,46 +43,25 @@ module AdPluginenddef update- ad = HouseAd.find_by(id: house_ad_params[:id])+ ad = HouseAd.find_by(id: params[:id])+ raise Discourse::NotFound if ad.nil?- if ad.nil?- ad = HouseAd.new(house_ad_params.except(:id, :routes))- if ad.save- sync_routes(ad, house_ad_params[:routes])- render_json_dump(- serialize_data(- ad,- HouseAdSerializer,- { include_categories: true, include_groups: true },- ),- )- else- render_json_error(ad)- end+ if ad.update(house_ad_params.except(:routes))+ sync_routes(ad, house_ad_params[:routes])+ render_json_dump(+ serialize_data(ad, HouseAdSerializer, { include_categories: true, include_groups: true }),+ )else- if ad.update(house_ad_params.except(:id, :routes))- sync_routes(ad, house_ad_params[:routes])- render_json_dump(- serialize_data(- ad,- HouseAdSerializer,- { include_categories: true, include_groups: true },- ),- )- else- render_json_error(ad)- end+ render_json_error(ad)endenddef destroy- ad = HouseAd.find_by(id: house_ad_params[:id])- if ad- ad.destroy- render json: success_json- else- render_json_error(I18n.t("not_found"), status: 404)- end+ ad = HouseAd.find_by(id: params[:id])+ raise Discourse::NotFound if ad.nil?++ ad.destroy+ render json: success_jsonendprivate@@ -94,7 +71,6 @@ module AdPluginbeginpermitted =params.permit(- :id,:name,:html,:visible_to_anons,diff --git a/plugins/discourse-adplugin/assets/javascripts/discourse/initializers/adplugin-admin-plugin-configuration-nav.js b/plugins/discourse-adplugin/assets/javascripts/discourse/initializers/adplugin-admin-plugin-configuration-nav.jsindex 47a393e1157..c4a441eb77d 100644--- a/plugins/discourse-adplugin/assets/javascripts/discourse/initializers/adplugin-admin-plugin-configuration-nav.js+++ b/plugins/discourse-adplugin/assets/javascripts/discourse/initializers/adplugin-admin-plugin-configuration-nav.js@@ -13,6 +13,12 @@ export default {withPluginApi((api) => {api.setAdminPluginIcon(PLUGIN_ID, "rectangle-ad");+ api.addAdminPluginConfigurationNav(PLUGIN_ID, [+ {+ label: "admin.adplugin.house_ads.title",+ route: "adminPlugins.show.houseAds",+ },+ ]);});},};diff --git a/plugins/discourse-adplugin/assets/stylesheets/adplugin.scss b/plugins/discourse-adplugin/assets/stylesheets/adplugin.scssindex c0031fd3425..a7ae5f6e921 100644--- a/plugins/discourse-adplugin/assets/stylesheets/adplugin.scss+++ b/plugins/discourse-adplugin/assets/stylesheets/adplugin.scss@@ -217,17 +217,9 @@}.adplugin-mgmt {- .house-ad-name {- width: 100%;- }-- .house-ads-actions {- .btn {- margin-right: 8px;- }- }+ margin-top: 1em;- .house-ads-list {+ .house-ads-table {margin-top: 1em;}@@ -258,47 +250,6 @@.house-ads-list-setting {margin-bottom: 1.5em;}-- .content-body {- padding-left: 2%;-- .visibility-settings {- margin-bottom: 1em;-- .description {- color: var(--primary-medium);- font-size: $font-down-1;- }-- .category-selector,- .group-chooser,- .route-selector {- margin-top: 1em;- margin-bottom: 0.2em;- }- }-- .controls {- margin-bottom: 1em;- }-- .delete-button {- float: right;- }-- .ace-wrapper {- position: relative;- height: 270px;-- .ace_editor {- position: absolute;- left: 0;- right: 0;- top: 0;- bottom: 0;- }- }- }}.house-ad-preview {diff --git a/plugins/discourse-adplugin/config/locales/client.en.yml b/plugins/discourse-adplugin/config/locales/client.en.ymlindex 21431589d68..be99ae6611f 100755--- a/plugins/discourse-adplugin/config/locales/client.en.yml+++ b/plugins/discourse-adplugin/config/locales/client.en.yml@@ -32,8 +32,13 @@ en:new_name: "New House Ad"save: "Save"delete: "Delete"- description: "Define your own ads and where they should be displayed."- get_started: "Start by creating a new ad."+ confirm_delete: "Are you sure you want to delete this ad?"+ html: "HTML"+ categories: "Categories"+ routes: "Routes"+ groups: "Groups"+ description: "Create and manage your own ads to display across your site in various positions."+ get_started: "No house ads have been created yet."filter_placeholder: "Select ads..."more_settings: "More Settings"show_to_anons: "Show to anonymous users"@@ -43,6 +48,12 @@ en:route_chooser_description: "Choose the pages/routes where this ad should be displayed. Leave this and category selections empty to show the ad everywhere."group_chooser_description: "Choose the groups that can view this ad or leave empty to show the ad to all signed in users."preview: "Preview"+ back: "House Ads"+ name: "Name"+ edit: "Edit"+ tabs:+ ads: "Ads"+ settings: "Settings"topic_list_top:title: "Topic list top ads"diff --git a/plugins/discourse-adplugin/plugin.rb b/plugins/discourse-adplugin/plugin.rbindex ffd3d94c785..ef1c1f1b09c 100755--- a/plugins/discourse-adplugin/plugin.rb+++ b/plugins/discourse-adplugin/plugin.rb@@ -10,7 +10,7 @@register_asset "stylesheets/adplugin.scss"register_svg_icon "rectangle-ad"-add_admin_route "admin.adplugin.house_ads.title", "houseAds"+add_admin_route "admin.adplugin.house_ads.title", "discourse-adplugin", use_new_show_route: trueenabled_site_setting :discourse_adplugin_enabled@@ -47,6 +47,16 @@ after_initialize dopost "/ad_plugin/ad_impressions" => "ad_plugin/ad_impressions#create"mount AdPlugin::Engine, at: "/admin/plugins/pluginad", constraints: AdminConstraint.new++ # HTML routes for admin SPA (full page refresh support)+ scope "/admin/plugins/discourse-adplugin", constraints: AdminConstraint.new do+ get "/house-ads" => "ad_plugin/house_ads#index", :format => false+ get "/house-ads/:id" => "ad_plugin/house_ads#show", :format => false+ put "/house-ads/:id" => "ad_plugin/house_ads#update", :format => false+ delete "/house-ads/:id" => "ad_plugin/house_ads#destroy", :format => false+ post "/house-ads" => "ad_plugin/house_ads#create", :format => false+ put "/house-settings/:id" => "ad_plugin/house_ad_settings#update", :format => false+ endendadd_to_serializer :site, :house_creatives dodiff --git a/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb b/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rbindex 82cb4ec1886..145802bca3d 100644--- a/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb+++ b/plugins/discourse-adplugin/spec/requests/house_ad_controller_spec.rb@@ -1,9 +1,9 @@# frozen_string_literal: truedescribe AdPlugin::HouseAdsController do- let(:admin) { Fabricate(:admin) }- let(:category) { Fabricate(:category) }- let(:group) { Fabricate(:group) }+ fab!(:admin)+ fab!(:category)+ fab!(:group)let!(:ad) doAdPlugin::HouseAd.create(@@ -38,6 +38,24 @@ describe AdPlugin::HouseAdsController doexpect(created_ad.html).to include("<div>Ad</div>")end+ it "ignores a user-supplied id in the body" do+ chosen_id = 99_999++ post "/admin/plugins/pluginad/house_creatives.json",+ params: {+ id: chosen_id,+ name: "ID Inject Ad",+ html: "<p>injected</p>",+ visible_to_anons: "true",+ visible_to_logged_in_users: "true",+ }+ expect(response.status).to eq(200)++ created_ad = AdPlugin::HouseAd.find_by(name: "ID Inject Ad")+ expect(created_ad).to be_present+ expect(created_ad.id).not_to eq(chosen_id)+ end+it "strips event handler attributes from html on create" dopost "/admin/plugins/pluginad/house_creatives.json",params: {@@ -130,6 +148,37 @@ describe AdPlugin::HouseAdsController doexpect(ad_copy.route_names).to contain_exactly("discovery.latest", "topic.show")end+ it "returns 404 when the ad does not exist" do+ nonexistent_id = ad.id + 999++ expect {+ put "/admin/plugins/pluginad/house_creatives/#{nonexistent_id}.json",+ params: {+ name: "Phantom Ad",+ html: "<p>Should not be created</p>",+ visible_to_anons: "true",+ visible_to_logged_in_users: "true",+ }+ }.not_to change { AdPlugin::HouseAd.count }++ expect(response.status).to eq(404)+ end++ it "does not allow id injection via body params" do+ put "/admin/plugins/pluginad/house_creatives/#{ad.id}.json",+ params: {+ id: ad.id + 999,+ name: "Updated Name",+ html: "<p>Updated</p>",+ visible_to_anons: "true",+ visible_to_logged_in_users: "true",+ }++ expect(response.status).to eq(200)+ ad.reload+ expect(ad.name).to eq("Updated Name")+ end+it "replaces routes on update" doad.routes.create!(route_name: "discovery.latest")diff --git a/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb b/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rbindex e3f9b422eaf..94e101f50af 100644--- a/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb+++ b/plugins/discourse-adplugin/spec/system/admin_house_ad_spec.rb@@ -2,39 +2,95 @@describe "Admin House Ad", type: :system dofab!(:admin)- let(:house_ad) do- AdPlugin::HouseAd.create(- name: "some-name",- html: "<div>somecode</div>",- visible_to_anons: true,- visible_to_logged_in_users: false,- )- end- before do- enable_current_plugin- sign_in(admin)- end+ let(:house_ads_page) { PageObjects::Pages::AdminHouseAds.new }+ let(:form) { PageObjects::Components::FormKit.new(".house-ad-form") }+ let(:dialog) { PageObjects::Components::Dialog.new }+ let(:toasts) { PageObjects::Components::Toasts.new }++ context "when plugin is enabled" do+ before do+ enable_current_plugin+ sign_in(admin)+ end++ it "navigates from plugins page to house ads without routing errors" do+ page.visit "/admin/plugins"+ expect(page).to have_no_css(".alert-error")++ find("tr[data-plugin-name='discourse-adplugin'] a.admin-plugins-list__name").click+ expect(page).to have_current_path(%r{/admin/plugins/discourse-adplugin})+ expect(page).to have_no_css(".alert-error")+ end++ it "supports the full house ad lifecycle" do+ house_ads_page.visit_page+ expect(house_ads_page).to have_empty_state++ # Create a new ad+ house_ads_page.click_new_ad+ expect(form.field("visible_to_logged_in_users")).to be_checked+ expect(form.field("visible_to_anons")).to be_checked++ form.field("name").fill_in("My Test Ad")+ form.field("html").fill_in("<h1>hello world</h1>")+ form.field("visible_to_anons").toggle+ form.submit- describe "when visiting the page for creating new ads" do- it "has the visibility checkboxes on by default" do- visit("/admin/plugins/pluginad/house_creatives/new")+ expect(toasts).to have_success(I18n.t("js.saved"))+ ad = AdPlugin::HouseAd.last+ expect(ad).to have_attributes(+ name: "My Test Ad",+ visible_to_logged_in_users: true,+ visible_to_anons: false,+ )- expect(find("input.visible-to-anonymous-checkbox").checked?).to eq(true)- expect(find("input.visible-to-logged-in-checkbox").checked?).to eq(true)+ # Navigate back to list, verify ad appears+ house_ads_page.click_back+ expect(house_ads_page).to have_ad_listed("My Test Ad")++ # Click into the ad, verify fields populated+ house_ads_page.click_ad("My Test Ad")+ expect(form.field("name")).to have_value("My Test Ad")+ expect(form.field("visible_to_logged_in_users")).to be_checked+ expect(form.field("visible_to_anons")).to be_unchecked++ # Edit and save+ form.field("name").fill_in("Updated Ad")+ form.submit++ expect(toasts).to have_success(I18n.t("js.saved"))+ expect(ad.reload.name).to eq("Updated Ad")++ # Navigate back, verify updated name+ house_ads_page.click_back+ expect(house_ads_page).to have_ad_listed("Updated Ad")++ # Navigate back in and delete+ house_ads_page.click_ad("Updated Ad")+ house_ads_page.click_delete++ expect(dialog).to be_open+ dialog.click_yes++ expect(house_ads_page).to have_empty_state+ expect(AdPlugin::HouseAd.exists?(ad.id)).to eq(false)endend- describe "when visiting the page of an existing ad" do- it "the controls reflect the correct state of the ad" do- visit("/admin/plugins/pluginad/house_creatives/#{house_ad.id}")+ context "when plugin is toggled on from the plugins list" do+ before { sign_in(admin) }++ it "shows the House Ads nav tab after enabling and clicking into the plugin" do+ page.visit "/admin/plugins"++ find(+ "tr[data-plugin-name='discourse-adplugin'] .admin-plugins-list__enabled .d-toggle-switch__checkbox-slider",+ ).click++ find(".admin-plugin-tab-nav-item[data-plugin-nav-tab-id='discourse-adplugin'] a").click- expect(find("input.house-ad-name").value).to eq(house_ad.name)- expect(find("input.visible-to-anonymous-checkbox").checked?).to eq(true)- expect(find("input.visible-to-logged-in-checkbox").checked?).to eq(false)- # would be nice to assert for the HTML content in ace-editor, but there- # doesn't seem to be a way to check the content in ace-editor from the- # DOM+ expect(page).to have_css(".admin-plugin-config-page__top-nav-item", text: "House Ads")endendenddiff --git a/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb b/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rbnew file mode 100644index 00000000000..51aa48a4bf1--- /dev/null+++ b/plugins/discourse-adplugin/spec/system/page_objects/pages/admin_house_ads.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++module PageObjects+ module Pages+ class AdminHouseAds < PageObjects::Pages::Base+ def visit_page+ page.visit "/admin/plugins/discourse-adplugin/house-ads"+ self+ end++ def visit_new+ page.visit "/admin/plugins/discourse-adplugin/house-ads/new"+ self+ end++ def click_new_ad+ find(".d-page-subheader__actions .btn-primary").click+ self+ end++ def click_back+ find("a.back-button").click+ self+ end++ def click_ad(name)+ find(".house-ads-table tr.d-admin-row__content", text: name).find(+ ".house-ads-table__edit",+ ).click+ self+ end++ def has_ad_listed?(name)+ has_css?(".house-ads-table tr.d-admin-row__content", text: name)+ end++ def has_no_ad_listed?(name)+ has_no_css?(".house-ads-table tr.d-admin-row__content", text: name)+ end++ def click_delete+ find(".house-ad-form .btn-danger").click+ self+ end++ def has_empty_state?+ has_css?(".admin-config-area-empty-list")+ end+ end+ end+end