Skip to content

Commit

Permalink
impr: remove usage of jQuery getJSON (fehmer) (#5109)
Browse files Browse the repository at this point in the history
* impr: remove usage of jQuery getJSON

* use generic

* throwing if nothing found

---------

Co-authored-by: Miodec <jack@monkeytype.com>
  • Loading branch information
fehmer and Miodec authored Feb 22, 2024
1 parent fd4f1e5 commit 81b388a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
10 changes: 2 additions & 8 deletions frontend/src/ts/controllers/quotes-controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
cachedFetchJson,
randomElementFromArray,
removeLanguageSize,
shuffle,
} from "../utils/misc";
import { subscribe } from "../observables/config-event";
import * as DB from "../db";
import $ from "jquery";

type JsonQuote = {
text: string;
Expand Down Expand Up @@ -48,8 +48,7 @@ class QuotesController {
const normalizedLanguage = removeLanguageSize(language);

if (this.quoteCollection.language !== normalizedLanguage) {
// try {
const data: QuoteData = await $.getJSON(
const data = await cachedFetchJson<QuoteData>(
`quotes/${normalizedLanguage}.json`
);

Expand Down Expand Up @@ -96,11 +95,6 @@ class QuotesController {
if (quoteLengths !== undefined) {
this.updateQuoteQueue(quoteLengths);
}
// } catch (e) {
// console.error(e);
// throw new Error("Failed to parse quotes: " + e.message);
// return defaultQuoteCollection;
// }
}

return this.quoteCollection;
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/ts/test/british-english.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Config from "../config";
import { capitalizeFirstLetterOfEachWord } from "../utils/misc";
import {
cachedFetchJson,
capitalizeFirstLetterOfEachWord,
} from "../utils/misc";
import * as CustomText from "../test/custom-text";
import $ from "jquery";

type BritishEnglishReplacement = {
0: string;
Expand All @@ -13,13 +15,9 @@ let list: BritishEnglishReplacement[] = [];

export async function getList(): Promise<BritishEnglishReplacement[]> {
if (list.length === 0) {
return $.getJSON("languages/britishenglish.json", function (data) {
list = data;
return list;
});
} else {
return list;
list = await cachedFetchJson("languages/britishenglish.json");
}
return list;
}

export async function replace(
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/ts/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,20 @@ export function median(arr: number[]): number {
}

export async function getLatestReleaseFromGitHub(): Promise<string> {
const releases = await $.getJSON(
type releaseType = { name: string };
const releases = await cachedFetchJson<releaseType[]>(
"https://api.github.com/repos/monkeytypegame/monkeytype/releases?per_page=1"
);
if (releases[0] === undefined || releases[0].name === undefined) {
throw new Error("No release found");
}
return releases[0].name;
}

export async function getReleasesFromGitHub(): Promise<
MonkeyTypes.GithubRelease[]
> {
return $.getJSON(
return cachedFetchJson(
"https://api.github.com/repos/monkeytypegame/monkeytype/releases?per_page=5"
);
}
Expand Down

0 comments on commit 81b388a

Please sign in to comment.