Skip to content

Commit

Permalink
Add typings
Browse files Browse the repository at this point in the history
  • Loading branch information
robigan committed Jul 13, 2024
1 parent b110e5c commit 4931e52
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
18 changes: 11 additions & 7 deletions frontend/src/components/utils/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ export function getBaseURL() {
return typeof window !== 'undefined' && window.location.origin ? window.location.origin : '';
}

export const groupBy = (keys: any) => (array: any) =>
array.reduce((objectsByKeyValue: any, obj: any) => {
const value = keys.map((key: any) => obj[key]).join('-');
// eslint-disable-next-line no-param-reassign
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
return objectsByKeyValue;
}, {});
export const groupBy = <
K extends PropertyKey = PropertyKey,
T extends Record<K, any> = Record<K, unknown>
>(
keys: K[], array: T[]
) => array.reduce((objectsByKeyValue: Record<string, T[]>, obj) => {
const value = keys.map((key) => obj[key]).join('');
// eslint-disable-next-line no-param-reassign
objectsByKeyValue[value] = (objectsByKeyValue[value] ?? []).concat(obj);
return objectsByKeyValue;
}, {});

export function truncateString(input: string, length: number) {
if (input.length > length + 3) {
Expand Down
17 changes: 10 additions & 7 deletions frontend/src/services/lookups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MatchInterface } from '../interfaces/match';
import { StageWithStageItems } from '../interfaces/stage';
import { TeamInterface } from '../interfaces/team';
import { getTeams } from './adapter';
import { StageItemWithRounds } from '../interfaces/stage_item';

export function getTeamsLookup(tournamentId: number) {
const swrTeamsResponse: SWRResponse = getTeams(tournamentId);
Expand All @@ -18,8 +19,10 @@ export function getTeamsLookup(tournamentId: number) {
return Object.fromEntries(swrTeamsResponse.data.data.teams.map((x: TeamInterface) => [x.id, x]));
}

export function getStageItemLookup(swrStagesResponse: SWRResponse) {
let result: any[] = [];
export function getStageItemLookup(
swrStagesResponse: SWRResponse
) {
let result: [number, StageItemWithRounds][] = [];

swrStagesResponse.data.data.map((stage: StageWithStageItems) =>
stage.stage_items.forEach((stage_item) => {
Expand All @@ -30,7 +33,7 @@ export function getStageItemLookup(swrStagesResponse: SWRResponse) {
}

export function getStageItemList(swrStagesResponse: SWRResponse) {
let result: any[] = [];
let result: [StageItemWithRounds][] = [];

swrStagesResponse.data.data.map((stage: StageWithStageItems) =>
stage.stage_items.forEach((stage_item) => {
Expand All @@ -41,7 +44,7 @@ export function getStageItemList(swrStagesResponse: SWRResponse) {
}

export function getStageItemTeamIdsLookup(swrStagesResponse: SWRResponse) {
let result: any[] = [];
let result: [number, (number | null)[]][] = [];

swrStagesResponse.data.data.map((stage: StageWithStageItems) =>
stage.stage_items.forEach((stageItem) => {
Expand Down Expand Up @@ -79,7 +82,7 @@ export function getStageItemTeamsLookup(
}

export function getMatchLookup(swrStagesResponse: SWRResponse) {
let result: any[] = [];
let result: [number, { match: MatchInterface, stageItem: StageItemWithRounds }][] = [];

swrStagesResponse.data.data.map((stage: StageWithStageItems) =>
stage.stage_items.forEach((stageItem) => {
Expand Down Expand Up @@ -117,12 +120,12 @@ export function stringToColour(input: string) {

export function getMatchLookupByCourt(swrStagesResponse: SWRResponse) {
const matches = Object.values(getMatchLookup(swrStagesResponse)).map((x) => x.match);
return groupBy(['court_id'])(matches);
return groupBy<PropertyKey, MatchInterface>(['court_id'], matches);
}

export function getScheduleData(
swrCourtsResponse: SWRResponse,
matchesByCourtId: any
matchesByCourtId: ReturnType<typeof groupBy<PropertyKey, MatchInterface>>
): { court: Court; matches: MatchInterface[] }[] {
return swrCourtsResponse.data.data.map((court: Court) => ({
matches: (matchesByCourtId[court.id] || [])
Expand Down

0 comments on commit 4931e52

Please sign in to comment.