-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: Add group and category restriction to house ads #205
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
admin/assets/javascripts/discourse/components/modal/preview.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,18 @@ | ||
import { htmlSafe } from "@ember/template"; | ||
import DModal from "discourse/components/d-modal"; | ||
import i18n from "discourse-common/helpers/i18n"; | ||
|
||
const Preview = <template> | ||
<DModal | ||
@closeModal={{@closeModal}} | ||
@title={{i18n "admin.adplugin.house_ads.preview"}} | ||
> | ||
<:body> | ||
<div class="house-ad-preview"> | ||
{{htmlSafe @model.html}} | ||
</div> | ||
</:body> | ||
</DModal> | ||
</template>; | ||
|
||
export default Preview; |
229 changes: 135 additions & 94 deletions
229
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,156 @@ | ||
import { tracked } from "@glimmer/tracking"; | ||
import Controller, { inject as controller } from "@ember/controller"; | ||
import { not, or } from "@ember/object/computed"; | ||
import { inject as service } from "@ember/service"; | ||
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 { propertyNotEqual } from "discourse/lib/computed"; | ||
import { bufferedProperty } from "discourse/mixins/buffered-content"; | ||
import Category from "discourse/models/category"; | ||
import I18n from "I18n"; | ||
import Preview from "../components/modal/preview"; | ||
|
||
export default Controller.extend(bufferedProperty("model"), { | ||
adminPluginsHouseAds: controller("adminPlugins.houseAds"), | ||
router: service(), | ||
export default class adminPluginsHouseAdsShow extends Controller { | ||
@service router; | ||
@service modal; | ||
|
||
saving: false, | ||
savingStatus: "", | ||
@controller("adminPlugins.houseAds") houseAdsController; | ||
|
||
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"), | ||
@tracked selectedCategories = []; | ||
@tracked selectedGroups = []; | ||
@tracked saving = false; | ||
@tracked savingStatus = ""; | ||
@tracked buffered; | ||
|
||
actions: { | ||
save() { | ||
if (!this.get("saving")) { | ||
this.setProperties({ | ||
saving: true, | ||
savingStatus: I18n.t("saving"), | ||
}); | ||
@observes("model") | ||
modelChanged() { | ||
this.buffered = new TrackedObject({ ...this.model }); | ||
this.selectedCategories = this.model.categories || []; | ||
this.selectedGroups = this.model.group_ids || []; | ||
} | ||
|
||
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"); | ||
data.visible_to_logged_in_users = buffered.get( | ||
"visible_to_logged_in_users" | ||
); | ||
data.visible_to_anons = buffered.get("visible_to_anons"); | ||
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; | ||
} | ||
|
||
ajax( | ||
@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( | ||
newRecord | ||
? `/admin/plugins/pluginad/house_creatives` | ||
: `/admin/plugins/pluginad/house_creatives/${buffered.get("id")}`, | ||
: `/admin/plugins/pluginad/house_creatives/${this.buffered.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") | ||
); | ||
} | ||
}) | ||
.catch(popupAjaxError) | ||
.finally(() => { | ||
this.setProperties({ | ||
saving: false, | ||
savingStatus: "", | ||
}); | ||
}); | ||
); | ||
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 = ""; | ||
} | ||
}, | ||
} | ||
} | ||
|
||
cancel() { | ||
this.rollbackBuffer(); | ||
}, | ||
@action | ||
setCategoryIds(categoryArray) { | ||
this.selectedCategories = categoryArray; | ||
this.buffered.category_ids = categoryArray.map((c) => c.id); | ||
this.setCategoriesForBuffered(); | ||
} | ||
|
||
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) => id); | ||
} | ||
|
||
if (!model.get("id")) { | ||
this.router.transitionTo("adminPlugins.houseAds.index"); | ||
return; | ||
} | ||
@action | ||
cancel() { | ||
this.buffered = new TrackedObject({ ...this.model }); | ||
this.selectedCategories = this.model.categories || []; | ||
this.selectedGroups = this.model.group_ids || []; | ||
this.setCategoriesForBuffered(); | ||
} | ||
|
||
ajax(`/admin/plugins/pluginad/house_creatives/${model.get("id")}`, { | ||
type: "DELETE", | ||
}) | ||
.then(() => { | ||
houseAds.removeObject(model); | ||
this.router.transitionTo("adminPlugins.houseAds.index"); | ||
}) | ||
.catch(popupAjaxError); | ||
}, | ||
}, | ||
}); | ||
@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) | ||
) | ||
: []; | ||
} | ||
} |
12 changes: 7 additions & 5 deletions
12
admin/assets/javascripts/discourse/routes/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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you could change this to a fn
get savingText
that trackssaving
to make it a bit easier to followThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have three different states for
savingStatus
I think we will need to keep it as is, so that we can set the
saving
status during the ajax call. OR we drop thesaving
status and just have two states, "" orsaved
then move it to a getter.