Skip to content
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

New comp overview: Filter for events and cancelled status in the backend #10080

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions app/models/competition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,11 @@ def self.search(query, params: {}, managed_by_user: nil)
competitions = Competition.visible
end

if params[:include_cancelled].present?
include_cancelled = ActiveRecord::Type::Boolean.new.cast(params[:include_cancelled])
competitions = competitions.not_cancelled unless include_cancelled
end

if params[:continent].present?
continent = Continent.find(params[:continent])
if !continent
Expand All @@ -1750,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,12 +76,7 @@ function CompetitionsView({ canViewAdminDetails = false }) {
},
});

const baseCompetitions = rawCompetitionData?.pages.flatMap((page) => page.data)
.filter((comp) => (
(!isCancelled(comp) || debouncedFilterState.shouldIncludeCancelled)
&& (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
17 changes: 16 additions & 1 deletion app/webpacker/components/CompetitionsOverview/queryUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@ export function calculateQueryKey(filterState, canViewAdminDetails = false) {
return {
timeOrder: filterState?.timeOrder,
region: filterState?.region,
selectedEvents: filterState?.selectedEvents,
delegate: filterState?.delegate,
search: filterState?.search,
time: timeKey,
shouldIncludeCancelled: filterState?.shouldIncludeCancelled,
adminStatus,
};
}

export function createSearchParams(filterState, pageParam, canViewAdminDetails = false) {
const {
region, delegate, search, timeOrder, selectedYear, customStartDate, customEndDate, adminStatus,
region,
selectedEvents,
delegate,
search,
timeOrder,
selectedYear,
customStartDate,
customEndDate,
adminStatus,
shouldIncludeCancelled,
} = filterState;

const dateNow = DateTime.now();
Expand All @@ -36,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 All @@ -45,6 +59,7 @@ export function createSearchParams(filterState, pageParam, canViewAdminDetails =
if (canViewAdminDetails && adminStatus && adminStatus !== 'all') {
searchParams.append('admin_status', adminStatus);
}
searchParams.append('include_cancelled', shouldIncludeCancelled);

if (timeOrder === 'present') {
searchParams.append('sort', 'start_date,end_date,name');
Expand Down