Skip to content

Commit

Permalink
refactor: updateURL function for improved maintainability (@anthonypz) (
Browse files Browse the repository at this point in the history
#5817)

* refactor: updateURL function

* refactor: rename val to getVal and replace forEach loop with for of
  • Loading branch information
anthonypz authored Aug 23, 2024
1 parent e2d5744 commit 8863fb7
Showing 1 changed file with 25 additions and 46 deletions.
71 changes: 25 additions & 46 deletions frontend/src/ts/modals/share-test-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,37 @@ type SharedTestSettings = [

function updateURL(): void {
const baseUrl = location.origin + "?testSettings=";
const settings: SharedTestSettings = [
null,
null,
null,
null,
null,
null,
null,
null,
const settings: SharedTestSettings = new Array(8).fill(
null
) as SharedTestSettings;

const settingsMap = [
{ key: "mode", getValue: () => Config.mode },
{ key: "mode2", getValue: () => getMode2(Config, currentQuote) },
{ key: "customText", getValue: () => CustomText.getData() },
{ key: "punctuation", getValue: () => Config.punctuation },
{ key: "numbers", getValue: () => Config.numbers },
{ key: "language", getValue: () => Config.language },
{ key: "difficulty", getValue: () => Config.difficulty },
{ key: "funbox", getValue: () => Config.funbox },
];

if (getCheckboxValue("mode")) {
settings[0] = Config.mode;
}

if (getCheckboxValue("mode2")) {
settings[1] = getMode2(Config, currentQuote);
}

if (getCheckboxValue("customText")) {
settings[2] = CustomText.getData();
}

if (getCheckboxValue("punctuation")) {
settings[3] = Config.punctuation;
}

if (getCheckboxValue("numbers")) {
settings[4] = Config.numbers;
}

if (getCheckboxValue("language")) {
settings[5] = Config.language;
}

if (getCheckboxValue("difficulty")) {
settings[6] = Config.difficulty;
}

if (getCheckboxValue("funbox")) {
settings[7] = Config.funbox;
for (const [index, { key, getValue }] of settingsMap.entries()) {
if (getCheckboxValue(key)) {
settings[index] = getValue();
}
}

const compressed = compressToURI(JSON.stringify(settings));

const url = baseUrl + compressed;
$(`#shareTestSettingsModal textarea.url`).val(url);
if (url.length > 2000) {
$(`#shareTestSettingsModal .tooLongWarning`).removeClass("hidden");
} else {
$(`#shareTestSettingsModal .tooLongWarning`).addClass("hidden");
}

updateShareModal(url);
}

function updateShareModal(url: string): void {
const $modal = $(`#shareTestSettingsModal`);
$modal.find("textarea.url").val(url);
$modal.find(".tooLongWarning").toggleClass("hidden", url.length <= 2000);
}

function updateSubgroups(): void {
Expand Down

0 comments on commit 8863fb7

Please sign in to comment.