FEATURE: support count parameter on the events list component (#513)
* FEATURE: support count parameter on the events list component * DEV: fix linting
This commit is contained in:
parent
ef547b0448
commit
9fe3eb2583
|
|
@ -119,7 +119,7 @@ module DiscoursePostEvent
|
||||||
private
|
private
|
||||||
|
|
||||||
def filtered_events_params
|
def filtered_events_params
|
||||||
params.permit(:post_id, :category_id, :include_subcategories, :include_expired)
|
params.permit(:post_id, :category_id, :include_subcategories, :include_expired, :limit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import { isNotFullDayEvent } from "../lib/guess-best-date-format";
|
||||||
export const DEFAULT_MONTH_FORMAT = "MMMM YYYY";
|
export const DEFAULT_MONTH_FORMAT = "MMMM YYYY";
|
||||||
export const DEFAULT_DATE_FORMAT = "dddd, MMM D";
|
export const DEFAULT_DATE_FORMAT = "dddd, MMM D";
|
||||||
export const DEFAULT_TIME_FORMAT = "LT";
|
export const DEFAULT_TIME_FORMAT = "LT";
|
||||||
|
const DEFAULT_COUNT = 8;
|
||||||
|
|
||||||
export default class UpcomingEventsList extends Component {
|
export default class UpcomingEventsList extends Component {
|
||||||
@service appEvents;
|
@service appEvents;
|
||||||
|
|
@ -25,6 +26,7 @@ export default class UpcomingEventsList extends Component {
|
||||||
monthFormat = this.args.params?.monthFormat ?? DEFAULT_MONTH_FORMAT;
|
monthFormat = this.args.params?.monthFormat ?? DEFAULT_MONTH_FORMAT;
|
||||||
dateFormat = this.args.params?.dateFormat ?? DEFAULT_DATE_FORMAT;
|
dateFormat = this.args.params?.dateFormat ?? DEFAULT_DATE_FORMAT;
|
||||||
timeFormat = this.args.params?.timeFormat ?? DEFAULT_TIME_FORMAT;
|
timeFormat = this.args.params?.timeFormat ?? DEFAULT_TIME_FORMAT;
|
||||||
|
count = this.args.params?.count ?? DEFAULT_COUNT;
|
||||||
|
|
||||||
title = I18n.t(
|
title = I18n.t(
|
||||||
"discourse_calendar.discourse_post_event.upcoming_events_list.title"
|
"discourse_calendar.discourse_post_event.upcoming_events_list.title"
|
||||||
|
|
@ -75,7 +77,7 @@ export default class UpcomingEventsList extends Component {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { events } = await ajax("/discourse-post-event/events", {
|
const { events } = await ajax("/discourse-post-event/events", {
|
||||||
data: { category_id: this.categoryId },
|
data: { category_id: this.categoryId, limit: this.count },
|
||||||
});
|
});
|
||||||
|
|
||||||
this.eventsByMonth = this.groupByMonthAndDay(events);
|
this.eventsByMonth = this.groupByMonthAndDay(events);
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,8 @@ module DiscoursePostEvent
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
events = events.limit(params[:limit].to_i) if params[:limit].present?
|
||||||
|
|
||||||
events
|
events
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -172,5 +172,15 @@ describe DiscoursePostEvent::EventFinder do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "with a limit parameter provided" do
|
||||||
|
let!(:event1) { Fabricate(:event) }
|
||||||
|
let!(:event2) { Fabricate(:event) }
|
||||||
|
let!(:event3) { Fabricate(:event) }
|
||||||
|
|
||||||
|
it "returns the correct number of events" do
|
||||||
|
expect(finder.search(current_user, { limit: 2 })).to match_array([event1, event2])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -290,6 +290,15 @@ module DiscoursePostEvent
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "limits the number of events returned when limit param provided" do
|
||||||
|
get "/discourse-post-event/events.json?category_id=#{category.id}&include_subcategories=true&limit=1"
|
||||||
|
|
||||||
|
expect(response.status).to eq(200)
|
||||||
|
events = response.parsed_body["events"]
|
||||||
|
expect(events.length).to eq(1)
|
||||||
|
expect(events[0]["id"]).to eq(event_1.id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,7 @@ module("Integration | Component | upcoming-events-list", function (hooks) {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("with events, overriden formats", async function (assert) {
|
test("with events, overridden formats", async function (assert) {
|
||||||
pretender.get("/discourse-post-event/events", twoEventsResponseHandler);
|
pretender.get("/discourse-post-event/events", twoEventsResponseHandler);
|
||||||
|
|
||||||
await render(<template>
|
await render(<template>
|
||||||
|
|
@ -225,11 +225,44 @@ module("Integration | Component | upcoming-events-list", function (hooks) {
|
||||||
"it displays the try again button"
|
"it displays the try again button"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("with events, overridden count parameter", async function (assert) {
|
||||||
|
pretender.get("/discourse-post-event/events", twoEventsResponseHandler);
|
||||||
|
|
||||||
|
await render(<template>
|
||||||
|
<UpcomingEventsList @params={{hash count=1}} />
|
||||||
|
</template>);
|
||||||
|
|
||||||
|
this.appEvents.trigger("page:changed", { url: "/" });
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
query(".upcoming-events-list__heading").innerText,
|
||||||
|
I18n.t(
|
||||||
|
"discourse_calendar.discourse_post_event.upcoming_events_list.title"
|
||||||
|
),
|
||||||
|
"it displays the title"
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(".loading-container .spinner", { count: 0 });
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
queryAll(".upcoming-events-list__event").length,
|
||||||
|
1,
|
||||||
|
"it limits the resulting items to the count parameter"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
[...queryAll(".upcoming-events-list__event-name")].map(
|
||||||
|
(el) => el.innerText
|
||||||
|
),
|
||||||
|
["Awesome Event"],
|
||||||
|
"it displays the event name"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function twoEventsResponseHandler() {
|
function twoEventsResponseHandler({ queryParams }) {
|
||||||
return response({
|
const events = [
|
||||||
events: [
|
|
||||||
{
|
{
|
||||||
id: 67501,
|
id: 67501,
|
||||||
starts_at: tomorrowAllDay,
|
starts_at: tomorrowAllDay,
|
||||||
|
|
@ -264,6 +297,9 @@ function twoEventsResponseHandler() {
|
||||||
name: "Another Awesome Event",
|
name: "Another Awesome Event",
|
||||||
category_id: 2,
|
category_id: 2,
|
||||||
},
|
},
|
||||||
],
|
];
|
||||||
|
|
||||||
|
return response({
|
||||||
|
events: queryParams.limit ? events.slice(0, queryParams.limit) : events,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue