|
| 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 | +}; |
0 commit comments