-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Add group and category conditions to an ad
- Loading branch information
1 parent
c4227de
commit 1c8db02
Showing
6 changed files
with
195 additions
and
114 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
admin/assets/javascripts/discourse/components/house-ads-category-selector.gjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
233 changes: 133 additions & 100 deletions
233
admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,148 @@ | ||
import Controller, { inject as controller } from "@ember/controller"; | ||
import { not, or } from "@ember/object/computed"; | ||
Check failure on line 2 in admin/assets/javascripts/discourse/controllers/admin-plugins-house-ads-show.js
|
||
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"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import adminPluginsHouseAds from "../routes/admin-plugins-house-ads"; | ||
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; | ||
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)); | ||
} | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.