Skip to content

Commit

Permalink
Fix psych sheet footer (thewca#10550)
Browse files Browse the repository at this point in the history
* Remove redundant dataWithUser from footer props

* Pass registrationsWithPsychSheet to footer

instead of registrations

* Move footer fun below main component
  • Loading branch information
kr-matthews authored Jan 8, 2025
1 parent 54a2ddf commit bdb5209
Showing 1 changed file with 56 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,6 @@ import { EventSelector } from '../../wca/EventSelector';

const sortReducer = createSortReducer(['name', 'country', 'total']);

function FooterContent({
dataWithUser, registrations, competitionInfo, psychSheetEvent,
}) {
if (!dataWithUser || !registrations) return null;

const newcomerCount = dataWithUser.filter(
(reg) => !reg.user.wca_id,
).length;

const countryCount = new Set(
dataWithUser.map((reg) => reg.user.country.iso2),
).size;

const eventCounts = Object.fromEntries(
competitionInfo.event_ids.map((evt) => {
const competingCount = registrations.filter(
(reg) => reg.competing.event_ids.includes(evt),
).length;

return [evt, competingCount];
}),
);

const totalEvents = Object.values(eventCounts).reduce((a, b) => a + b, 0);

return (
<Table.Row>
<Table.Cell>
{`${newcomerCount} ${I18n.t('registrations.registration_info_people.newcomer', { count: newcomerCount })} + ${
dataWithUser.length - newcomerCount
} ${I18n.t('registrations.registration_info_people.returner', { count: dataWithUser.length - newcomerCount })} =
${dataWithUser.length} ${I18n.t('registrations.registration_info_people.person', { count: dataWithUser.length })}`}
</Table.Cell>
<Table.Cell>{`${I18n.t('registrations.list.country_plural', { count: countryCount })}`}</Table.Cell>
{psychSheetEvent === undefined ? (
<>
{competitionInfo.event_ids.map((evt) => (
<Table.Cell key={`footer-count-${evt}`}>
{eventCounts[evt]}
</Table.Cell>
))}
<Table.Cell>{totalEvents}</Table.Cell>
</>
) : (
<>
<Table.Cell />
<Table.Cell />
<Table.Cell />
<Table.Cell />
</>
)}
</Table.Row>
);
}

export default function RegistrationList({ competitionInfo }) {
const { isLoading: registrationsLoading, data: registrations, isError } = useQuery({
queryKey: ['registrations', competitionInfo.id],
Expand Down Expand Up @@ -319,9 +264,8 @@ export default function RegistrationList({ competitionInfo }) {
</Table.Body>
<Table.Footer>
<FooterContent
registrations={registrations}
registrations={registrationsWithPsychSheet}
psychSheetEvent={psychSheetEvent}
dataWithUser={registrations}
competitionInfo={competitionInfo}
/>
</Table.Footer>
Expand All @@ -330,6 +274,61 @@ export default function RegistrationList({ competitionInfo }) {
);
}

function FooterContent({
registrations, psychSheetEvent, competitionInfo,
}) {
if (!registrations) return null;

const newcomerCount = registrations.filter(
(reg) => !reg.user.wca_id,
).length;

const countryCount = new Set(
registrations.map((reg) => reg.user.country.iso2),
).size;

const eventCounts = Object.fromEntries(
competitionInfo.event_ids.map((evt) => {
const competingCount = registrations.filter(
(reg) => reg.competing.event_ids.includes(evt),
).length;

return [evt, competingCount];
}),
);

const totalEvents = Object.values(eventCounts).reduce((a, b) => a + b, 0);

return (
<Table.Row>
<Table.Cell>
{`${newcomerCount} ${I18n.t('registrations.registration_info_people.newcomer', { count: newcomerCount })} + ${
registrations.length - newcomerCount
} ${I18n.t('registrations.registration_info_people.returner', { count: registrations.length - newcomerCount })} =
${registrations.length} ${I18n.t('registrations.registration_info_people.person', { count: registrations.length })}`}
</Table.Cell>
<Table.Cell>{`${I18n.t('registrations.list.country_plural', { count: countryCount })}`}</Table.Cell>
{psychSheetEvent === undefined ? (
<>
{competitionInfo.event_ids.map((evt) => (
<Table.Cell key={`footer-count-${evt}`}>
{eventCounts[evt]}
</Table.Cell>
))}
<Table.Cell>{totalEvents}</Table.Cell>
</>
) : (
<>
<Table.Cell />
<Table.Cell />
<Table.Cell />
<Table.Cell />
</>
)}
</Table.Row>
);
}

function PsychSheetEventSelector({
handleEventSelection,
eventList,
Expand Down

0 comments on commit bdb5209

Please sign in to comment.