Skip to content

Commit

Permalink
fix(custom text): saving a book mode test completely breaking saved b…
Browse files Browse the repository at this point in the history
…ook mode tests (#4990)

* rename functions

* add set functions

* use new set functions (still need to fix type errors)

* make parameter not optional

* fix type errors
  • Loading branch information
Miodec authored Feb 1, 2024
1 parent 1f302c3 commit d9eb4b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion frontend/src/ts/popups/simple-popups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ list["deleteCustomText"] = new SimplePopup(
"Are you sure?",
"Delete",
async (_thisPopup) => {
CustomText.deleteCustomText(_thisPopup.parameters[0] as string);
CustomText.deleteCustomText(_thisPopup.parameters[0] as string, false);
CustomTextState.setCustomTextName("", undefined);
SavedTextsPopup.show(true);

Expand Down
46 changes: 28 additions & 18 deletions frontend/src/ts/test/custom-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ type CustomTextLongObject = Record<string, { text: string; progress: number }>;

export function getCustomText(name: string, long = false): string[] {
if (long) {
const customText = getCustomTextLongObject()[name];
const customTextLong = getLocalStorageLong();
const customText = customTextLong[name];
if (customText === undefined)
throw new Error(`Custom text ${name} not found`);
return customText.text.split(/ +/);
} else {
const customText = getCustomTextObject()[name];
const customText = getLocalStorage()[name];
if (customText === undefined)
throw new Error(`Custom text ${name} not found`);
return customText.split(/ +/);
Expand All @@ -86,7 +87,7 @@ export function setCustomText(
long = false
): void {
if (long) {
const customText = getCustomTextLongObject();
const customText = getLocalStorageLong();

customText[name] = {
text: "",
Expand All @@ -104,34 +105,34 @@ export function setCustomText(
textByName.text = text.join(" ");
}

window.localStorage.setItem("customTextLong", JSON.stringify(customText));
setLocalStorageLong(customText);
} else {
const customText = getCustomTextObject();
const customText = getLocalStorage();

if (typeof text === "string") {
customText[name] = text;
} else {
customText[name] = text.join(" ");
}

window.localStorage.setItem("customText", JSON.stringify(customText));
setLocalStorage(customText);
}
}

export function deleteCustomText(name: string, long = false): void {
const customText = long ? getCustomTextLongObject() : getCustomTextObject();
export function deleteCustomText(name: string, long: boolean): void {
const customText = long ? getLocalStorageLong() : getLocalStorage();

if (customText[name] != undefined) delete customText[name];
delete customText[name];

if (long) {
window.localStorage.setItem("customTextLong", JSON.stringify(customText));
setLocalStorageLong(customText as CustomTextLongObject);
} else {
window.localStorage.setItem("customText", JSON.stringify(customText));
setLocalStorage(customText as CustomTextObject);
}
}

export function getCustomTextLongProgress(name: string): number {
const customText = getCustomTextLongObject()[name];
const customText = getLocalStorageLong()[name];
if (customText === undefined) throw new Error("Custom text not found");

return customText.progress ?? 0;
Expand All @@ -141,25 +142,34 @@ export function setCustomTextLongProgress(
name: string,
progress: number
): void {
const customText = getCustomTextLongObject()[name];
const customTexts = getLocalStorageLong();
const customText = customTexts[name];
if (customText === undefined) throw new Error("Custom text not found");

customText.progress = progress;
window.localStorage.setItem("customTextLong", JSON.stringify(customText));
setLocalStorageLong(customTexts);
}

function getCustomTextObject(): CustomTextObject {
function getLocalStorage(): CustomTextObject {
return JSON.parse(window.localStorage.getItem("customText") ?? "{}");
}

function getCustomTextLongObject(): CustomTextLongObject {
function getLocalStorageLong(): CustomTextLongObject {
return JSON.parse(window.localStorage.getItem("customTextLong") ?? "{}");
}

function setLocalStorage(data: CustomTextObject): void {
window.localStorage.setItem("customText", JSON.stringify(data));
}

function setLocalStorageLong(data: CustomTextLongObject): void {
window.localStorage.setItem("customTextLong", JSON.stringify(data));
}

export function getCustomTextNames(long = false): string[] {
if (long) {
return Object.keys(getCustomTextLongObject());
return Object.keys(getLocalStorageLong());
} else {
return Object.keys(getCustomTextObject());
return Object.keys(getLocalStorage());
}
}

0 comments on commit d9eb4b9

Please sign in to comment.