Skip to content

Commit

Permalink
Remove comment about refresh token and convert functions to async
Browse files Browse the repository at this point in the history
  • Loading branch information
campos20 committed Oct 3, 2020
1 parent 9b4fd28 commit b9c3817
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
45 changes: 21 additions & 24 deletions tnoodle-ui/src/main/api/tnoodle.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,37 @@ const convertToBlob = async (result) => {
return await res.blob();
};

export const fetchWcaEvents = () => {
return fetch(tNoodleBackend + wcaEventsEndpoint)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchWcaEvents = async () => {
const response = await fetch(tNoodleBackend + wcaEventsEndpoint);
return await response.json();
};

export const fetchFormats = () => {
return fetch(tNoodleBackend + formatsEndpoint)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchFormats = async () => {
const response = await fetch(tNoodleBackend + formatsEndpoint);
return await response.json();
};

export const fetchSuggestedFmcTranslations = (wcif) => {
return postToTnoodle(suggestedFmcTranslationsEndpoint, wcif)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchSuggestedFmcTranslations = async (wcif) => {
const response = await postToTnoodle(
suggestedFmcTranslationsEndpoint,
wcif
);
return await response.json();
};

export const fetchBestMbldAttempt = (wcif) => {
return postToTnoodle(bestMbldAttemptEndpoint, wcif)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchBestMbldAttempt = async (wcif) => {
const response = await postToTnoodle(bestMbldAttemptEndpoint, wcif);
return await response.json();
};

export const fetchRunningVersion = () => {
return fetch(tNoodleBackend + versionEndpoint)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchRunningVersion = async () => {
const response = await fetch(tNoodleBackend + versionEndpoint);
return await response.json();
};

export const fetchAvailableFmcTranslations = () => {
return fetch(tNoodleBackend + fmcTranslationsEndpoint)
.then((response) => response.json())
.catch((error) => console.error(error));
export const fetchAvailableFmcTranslations = async () => {
const response = await fetch(tNoodleBackend + fmcTranslationsEndpoint);
return await response.json();
};

/**
Expand Down
41 changes: 20 additions & 21 deletions tnoodle-ui/src/main/api/wca.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,35 @@ export function gotoPreLoginPath() {
window.location.replace(preLoginHref);
}

export function fetchMe() {
return wcaApiFetch("/me")
.then((response) => response.json())
.then((json) => json.me);
export async function fetchMe() {
const response = await wcaApiFetch("/me");
const json = await response.json();
return json.me;
}

export function fetchVersionInfo() {
return wcaApiFetch("/scramble-program").then((response) => response.json());
export async function fetchVersionInfo() {
const response = await wcaApiFetch("/scramble-program");
return await response.json();
}

export function getCompetitionJson(competitionId) {
return wcaApiFetch(`/competitions/${competitionId}/wcif`).then((response) =>
response.json()
);
export async function getCompetitionJson(competitionId) {
const response = await wcaApiFetch(`/competitions/${competitionId}/wcif`);
return await response.json();
}

export function getUpcomingManageableCompetitions() {
export async function getUpcomingManageableCompetitions() {
let oneWeekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
return wcaApiFetch(
const response = await wcaApiFetch(
`/competitions?managed_by_me=true&start=${oneWeekAgo.toISOString()}`
).then((response) => response.json());
);
return await response.json();
}

const getLastLoginEnv = () => localStorage[TNOODLE_LAST_LOGIN_ENV];

const getCurrentEnv = () => (isUsingStaging() ? STAGING : PRODUCTION);

function wcaApiFetch(path, fetchOptions) {
// TODO - look into refresh token https://github.com/doorkeeper-gem/doorkeeper/wiki/Enable-Refresh-Token-Credentials
async function wcaApiFetch(path, fetchOptions) {
var baseApiUrl = toWcaUrl("/api/v0");
fetchOptions = Object.assign({}, fetchOptions, {
headers: new Headers({
Expand All @@ -149,10 +149,9 @@ function wcaApiFetch(path, fetchOptions) {
}),
});

return fetch(`${baseApiUrl}${path}`, fetchOptions).then((response) => {
if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`);
}
return response;
});
const response = await fetch(`${baseApiUrl}${path}`, fetchOptions);
if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`);
}
return response;
}

0 comments on commit b9c3817

Please sign in to comment.