Skip to content

Commit

Permalink
Refactor, works in UI, tests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
brrusselburg committed Nov 27, 2024
1 parent c30f5ec commit dfa0860
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
37 changes: 21 additions & 16 deletions assets/javascripts/discourse/components/upcoming-events-list.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ function addToResult(date, item, result) {
result[monthKey][day].push(item);
}

function isCustomTitle() {

}

export default class UpcomingEventsList extends Component {
@service appEvents;
@service siteSettings;
Expand All @@ -45,7 +41,6 @@ export default class UpcomingEventsList extends Component {
count = this.args.params?.count ?? DEFAULT_COUNT;
upcomingDays = this.args.params?.upcomingDays ?? DEFAULT_UPCOMING_DAYS;

title = this.title;
emptyMessage = I18n.t("discourse_post_event.upcoming_events_list.empty");
allDayLabel = I18n.t("discourse_post_event.upcoming_events_list.all_day");
errorMessage = I18n.t("discourse_post_event.upcoming_events_list.error");
Expand Down Expand Up @@ -73,22 +68,32 @@ export default class UpcomingEventsList extends Component {
}

get title() {
const titleMap = JSON.parse(this.siteSettings.map_events_title);
const categorySlug = this.router.currentRoute.attributes?.category?.slug;
const customTitleFind = titleMap.find(
(o) => o.category_slug === categorySlug
);
const titleSetting = this.siteSettings?.map_events_title;

const title = (typeof customTitleFind === "undefined") ? null : customTitleFind.custom_title;
if (titleSetting == "") {
return I18n.t("discourse_post_event.upcoming_events_list.title");
}

const categories = JSON.parse(titleSetting).map(
({ category_slug }) => category_slug
);

if (!title) {
return I18n.t("discourse_post_event.upcoming_events_list.title", {
upcoming_events_title: "Upcoming Events",
if (categories.includes(categorySlug)) {
const titleMap = JSON.parse(titleSetting);
const customTitleLookup = titleMap.find(
(o) => o.category_slug === categorySlug
);
const custom_title =
typeof customTitleLookup === "undefined"
? null
: customTitleLookup.custom_title;

return I18n.t("discourse_post_event.upcoming_events_list.custom_title", {
upcoming_events_title: custom_title,
});
} else {
return I18n.t("discourse_post_event.upcoming_events_list.title", {
upcoming_events_title: title,
});
return I18n.t("discourse_post_event.upcoming_events_list.title");
}
}

Expand Down
3 changes: 2 additions & 1 deletion config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ en:
status: "Status"
starts_at: "Starts at"
upcoming_events_list:
title: "%{upcoming_events_title}"
title: "Upcoming events"
custom_title: "%{upcoming_events_title}"
empty: "No upcoming events"
all_day: "All-day"
error: "Failed to retrieve events"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,31 @@ module("Integration | Component | upcoming-events-list", function (hooks) {
);
});

// test("with events, overridden titles", async function (assert) {
// await render(
// <template>
// <UpcomingEventsList
// />
// </template>
// );

// this.appEvents.trigger("page:changed", { url: "/" });

// this.args.params.categorySlug = "c/announcements";
// assert.equal(
// component.title(),
// "Upcoming Announcements",
// "it sets 'Upcoming Announcements' as the title in 'c/announcements'"
// );

// this.args.params.categorySlug = "c/events";
// assert.equal(
// component.title(),
// "Upcoming Cool Events",
// "it sets 'Upcoming Cool Events' as the title in 'c/events'"
// );

// this.args.params.categorySlug = "c/unknown";
// assert.equal(
// component.title(),
// "Upcoming Events",
// "it returns the default value for title when otherwise not specified"
// );
// });
test("with events, overridden titles", async function (assert) {
// this.siteSettings.map_events_title =
// '[{"category_slug": "announcements", "custom_title": "Upcoming Announcements"}]';
// await render(<template><UpcomingEventsList /></template>);
// this.appEvents.trigger("page:changed", { url: "/c/1" });
// debugger;
// // this.args.params.categorySlug = "announcements";
// assert.strictEqual(
// query(".upcoming-events-list__heading").innerText,
// "Upcoming Announcements",
// "it sets 'Upcoming Announcements' as the title in 'c/announcements'"
// );
// this.args.params.categorySlug = "events";
// assert.equal(
// component.title(),
// "Upcoming Cool Events",
// "it sets 'Upcoming Cool Events' as the title in 'c/events'"
// );
// this.args.params.categorySlug = "unknown";
// assert.equal(
// component.title(),
// "Upcoming Events",
// "it returns the default value for title when otherwise not specified"
// );
});

test("with events, view-all navigation", async function (assert) {
pretender.get("/discourse-post-event/events", twoEventsResponseHandler);
Expand Down

0 comments on commit dfa0860

Please sign in to comment.