Skip to content

Commit

Permalink
Filter events held at competition through backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg committed Oct 24, 2024
1 parent 192edd7 commit ebbed07
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,19 @@ def self.search(query, params: {}, managed_by_user: nil)
.where('competition_delegates.delegate_id = ?', delegate_user.id)
end

if params[:event_ids].present?
event_ids = params[:event_ids].presence
unless event_ids.is_a?(Array)
raise WcaExceptions::BadApiParameter.new("Invalid event IDs: '#{params[:event_ids]}'")
end
event_ids.each do |event_id|
# This looks completely crazy (why not just pass the array as a whole, to build a `WHERE event_id IN (...)`??)
# but is actually necessary to make sure that the competition holds ALL of the required events
# and not just one or more (ie any) of the requested events.
competitions = competitions.has_event(event_id)
end
end

if params[:start].present?
start_date = Date.safe_parse(params[:start])
if !start_date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './filterUtils';
import { calculateQueryKey, createSearchParams } from './queryUtils';
import useDebounce from '../../lib/hooks/useDebounce';
import { isCancelled, isInProgress, isProbablyOver } from '../../lib/utils/competition-table';
import { isInProgress, isProbablyOver } from '../../lib/utils/competition-table';

const DEBOUNCE_MS = 600;

Expand Down Expand Up @@ -76,11 +76,7 @@ function CompetitionsView({ canViewAdminDetails = false }) {
},
});

const baseCompetitions = rawCompetitionData?.pages.flatMap((page) => page.data)
.filter((comp) => (
(debouncedFilterState.selectedEvents.every((event) => comp.event_ids.includes(event)))
));

const baseCompetitions = rawCompetitionData?.pages.flatMap((page) => page.data);
const compIds = baseCompetitions?.map((comp) => comp.id) || [];

const {
Expand Down
5 changes: 5 additions & 0 deletions app/webpacker/components/CompetitionsOverview/queryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function calculateQueryKey(filterState, canViewAdminDetails = false) {
return {
timeOrder: filterState?.timeOrder,
region: filterState?.region,
selectedEvents: filterState?.selectedEvents,
delegate: filterState?.delegate,
search: filterState?.search,
time: timeKey,
Expand All @@ -28,6 +29,7 @@ export function calculateQueryKey(filterState, canViewAdminDetails = false) {
export function createSearchParams(filterState, pageParam, canViewAdminDetails = false) {
const {
region,
selectedEvents,
delegate,
search,
timeOrder,
Expand All @@ -45,6 +47,9 @@ export function createSearchParams(filterState, pageParam, canViewAdminDetails =
const regionParam = isContinent(region) ? 'continent' : 'country_iso2';
searchParams.append(regionParam, region);
}
if (selectedEvents && selectedEvents.length > 0) {
selectedEvents.forEach((eventId) => searchParams.append('event_ids[]', eventId));
}
if (delegate) {
searchParams.append('delegate', delegate);
}
Expand Down

0 comments on commit ebbed07

Please sign in to comment.