Skip to content

Commit 3c017de

Browse files
committed
Continued migration to typescript
1 parent 3fa3720 commit 3c017de

29 files changed

Lines changed: 1030 additions & 727 deletions

src/lib/api/localStorage.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib/api/localStorage.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { WCA_OAUTH_CLIENT_ID } from './wca-env';
2+
3+
export const localStorageKey = (key: string): string =>
4+
`delegate-dashboard.${WCA_OAUTH_CLIENT_ID}.${key}`;
5+
export const getLocalStorage = (key: string): string | null =>
6+
localStorage.getItem(localStorageKey(key));
7+
export const setLocalStorage = (key: string, value: string | null): void => {
8+
if (value === null) return;
9+
localStorage.setItem(localStorageKey(key), value);
10+
};

src/lib/api/wcaAPI.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

src/lib/api/wcaAPI.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { pick } from '../utils';
2+
import { getLocalStorage } from './localStorage';
3+
import { WCA_ORIGIN } from './wca-env';
4+
import { Competition } from '@wca/helpers';
5+
6+
interface WcaUser {
7+
id: number;
8+
name: string;
9+
wca_id?: string;
10+
avatar: {
11+
url: string;
12+
thumb_url: string;
13+
};
14+
country_iso2: string;
15+
email?: string;
16+
}
17+
18+
interface WcaPerson {
19+
id: number;
20+
name: string;
21+
wcaUserId?: number;
22+
wcaId?: string;
23+
countryIso2: string;
24+
}
25+
26+
interface CompetitionSearchResult {
27+
id: string;
28+
name: string;
29+
start_date: string;
30+
end_date: string;
31+
city: string;
32+
country_iso2: string;
33+
}
34+
35+
const wcaAccessToken = (): string | null => getLocalStorage('accessToken');
36+
37+
export const getMe = (): Promise<{ me: WcaUser }> => {
38+
console.log('Access Token:', wcaAccessToken());
39+
return wcaApiFetch(`/me`);
40+
};
41+
42+
/**
43+
* @deprecated
44+
*/
45+
export const getManageableCompetitions = (): Promise<CompetitionSearchResult[]> => {
46+
const params = new URLSearchParams({
47+
managed_by_me: 'true',
48+
});
49+
return wcaApiFetch(`/competitions?${params.toString()}`);
50+
};
51+
52+
export const getUpcomingManageableCompetitions = (): Promise<CompetitionSearchResult[]> => {
53+
const oneWeekAgo = new Date(Date.now() - 2 * 7 * 24 * 60 * 60 * 1000);
54+
const params = new URLSearchParams({
55+
managed_by_me: 'true',
56+
start: oneWeekAgo.toISOString(),
57+
sort: 'start_date',
58+
});
59+
return wcaApiFetch(`/competitions?${params.toString()}`);
60+
};
61+
62+
export const getPastManageableCompetitions = (): Promise<CompetitionSearchResult[]> => {
63+
const oneWeekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
64+
const params = new URLSearchParams({
65+
managed_by_me: 'true',
66+
end: oneWeekAgo.toISOString(),
67+
});
68+
return wcaApiFetch(`/competitions?${params.toString()}`);
69+
};
70+
71+
export const getWcif = (competitionId: string): Promise<Competition> =>
72+
wcaApiFetch(`/competitions/${competitionId}/wcif`);
73+
74+
export const patchWcif = (
75+
competitionId: string,
76+
wcif: Partial<Competition>
77+
): Promise<Competition> =>
78+
wcaApiFetch(`/competitions/${competitionId}/wcif`, {
79+
method: 'PATCH',
80+
body: JSON.stringify(wcif),
81+
});
82+
83+
export const saveWcifChanges = (
84+
previousWcif: Competition,
85+
newWcif: Competition
86+
): Promise<Competition | void> => {
87+
const keysDiff = Object.keys(newWcif).filter(
88+
(key) => previousWcif[key as keyof Competition] !== newWcif[key as keyof Competition]
89+
);
90+
if (keysDiff.length === 0) return Promise.resolve();
91+
return patchWcif(newWcif.id, pick(newWcif, keysDiff));
92+
};
93+
94+
export const searchPersons = (query: string): Promise<WcaPerson[]> =>
95+
wcaApiFetch(`/persons?q=${query}`);
96+
97+
export const getPerson = (personId: number): Promise<WcaPerson> =>
98+
wcaApiFetch(`/persons/${personId}`);
99+
100+
export const searchUsers = (query: string): Promise<{ result: WcaUser[] }> =>
101+
wcaApiFetch(`/search/users?q=${query}`);
102+
103+
export const getUser = (userId: number): Promise<{ user: WcaUser }> =>
104+
wcaApiFetch(`/users/${userId}`);
105+
106+
export const wcaApiFetch = async <T = any>(
107+
path: string,
108+
fetchOptions: RequestInit = {}
109+
): Promise<T> => {
110+
const baseApiUrl = `${WCA_ORIGIN}/api/v0`;
111+
112+
const res = await fetch(
113+
`${baseApiUrl}${path}`,
114+
Object.assign({}, fetchOptions, {
115+
headers: new Headers({
116+
Authorization: `Bearer ${wcaAccessToken()}`,
117+
'Content-Type': 'application/json',
118+
}),
119+
})
120+
);
121+
122+
if (!res.ok) {
123+
if (res.statusText) {
124+
throw new Error(`${res.status}: ${res.statusText}`);
125+
} else {
126+
throw new Error(`Something went wrong: Status code ${res.status}`);
127+
}
128+
}
129+
130+
return await res.json();
131+
};

src/lib/assignmentGenerators/generateCompetingAssignmentsForStaff.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { ActivityWithParent, findGroupActivitiesByRound, parseActivityCode } from '../domain';
2-
import {
3-
hasStaffAssignment,
4-
InProgressAssignmment,
5-
isStaffAssignment,
6-
missingCompetitorAssignments,
7-
} from '../domain';
2+
import { hasStaffAssignment, isStaffAssignment, missingCompetitorAssignments } from '../domain';
83
import { personsShouldBeInRound } from '../domain';
4+
import { InProgressAssignmment } from '../types';
95
import { createGroupAssignment, previousGroupForActivity } from '../wcif';
106
import { Competition, Event, Person } from '@wca/helpers';
117

src/lib/assignmentGenerators/generateCompetingGroupActitivitesForEveryone.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { findGroupActivitiesByRound, parseActivityCode } from '../domain';
2-
import {
3-
InProgressAssignmment,
4-
isCompetitorAssignment,
5-
missingCompetitorAssignments,
6-
} from '../domain';
2+
import { isCompetitorAssignment, missingCompetitorAssignments } from '../domain';
73
import { byPROrResult, personsShouldBeInRound } from '../domain';
4+
import { InProgressAssignmment } from '../types';
85
import { byName } from '../utils';
96
import { createGroupAssignment } from '../wcif';
107
import { Competition, Event } from '@wca/helpers';

src/lib/assignmentGenerators/generateGroupAssignmentsForDelegatesAndOrganizers.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { findRoundActivitiesById, parseActivityCode } from '../domain';
2-
import { InProgressAssignmment, missingCompetitorAssignments } from '../domain';
3-
import { byPROrResult, isOrganizerOrDelegate, personsShouldBeInRound } from '../domain';
1+
import {
2+
byPROrResult,
3+
findRoundActivitiesById,
4+
isOrganizerOrDelegate,
5+
missingCompetitorAssignments,
6+
parseActivityCode,
7+
personsShouldBeInRound,
8+
} from '../domain';
9+
import { InProgressAssignmment } from '../types';
410
import { byName } from '../utils';
511
import { createGroupAssignment } from '../wcif';
612
import { Activity, Competition, Event } from '@wca/helpers';

src/lib/assignmentGenerators/generateJudgeAssignmentsFromCompetingAssignments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { findGroupActivitiesByRound, parseActivityCode } from '../domain';
22
import {
33
findCompetingAssignment,
44
hasCompetitorAssignment,
5-
InProgressAssignmment,
65
missingStaffAssignments,
76
} from '../domain';
87
import { isOrganizerOrDelegate, personsShouldBeInRound } from '../domain';
8+
import { InProgressAssignmment } from '../types';
99
import { createGroupAssignment, nextGroupForActivity } from '../wcif';
1010
import { Competition, Event } from '@wca/helpers';
1111

src/lib/domain/activities.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,7 @@ export const cumulativeGroupCount = (round) => {
289289
/**
290290
* Searches for an activity recursively and returns a new version of the activity
291291
*/
292-
export const findAndReplaceActivity = (
293-
where: Partial<Activity> & { id: string },
294-
what: Partial<Activity>
295-
) => {
292+
export const findAndReplaceActivity = (where: Partial<Activity>, what: Partial<Activity>) => {
296293
return (activity: Activity): Activity => {
297294
if (activity.id === where.id) {
298295
return {

src/lib/domain/assignments.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1+
import { InProgressAssignmment } from '../types';
12
import { Assignment, Person } from '@wca/helpers';
23

3-
export interface InProgressAssignmment {
4-
registrantId: number;
5-
assignment: Assignment;
6-
}
7-
84
/**
95
* So that I don't have to remember the data format
106
*/
117
export const createGroupAssignment = (
128
registrantId: number,
13-
activityId: any,
9+
activityId: number,
1410
assignmentCode: string,
1511
stationNumber: number | null = null
1612
): InProgressAssignmment => ({

0 commit comments

Comments
 (0)