Skip to content

Commit

Permalink
Use overloaded function for getFromLocalStorage type
Browse files Browse the repository at this point in the history
This allows clients to not provide a default value, in which case
undefined is returned they must explicitly handle it.

Also move a CSS import from App.tsx to index.tsx
  • Loading branch information
StenAL committed Feb 27, 2022
1 parent b346a23 commit 1c3a4b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import "currency-flags/dist/currency-flags.css";
import { FunctionComponent, useEffect, useReducer, useState } from "react";
import { BalanceContainer } from "./components/BalanceContainer";
import { ShortcutsContainer } from "./components/ShortcutsContainer";
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { StrictMode } from "react";
import { render } from "react-dom";
import "./index.scss";
import { App } from "./App";

import "./index.scss";
import "currency-flags/dist/currency-flags.css";

render(
<StrictMode>
<App />
Expand Down
20 changes: 12 additions & 8 deletions src/util/LocalStorageHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ export enum LocalStorageKey {
DISPLAYED_BALANCES = "displayedBalances",
}

export const getFromLocalStorage = (key: LocalStorageKey, defaultValue: string | (() => string) = ""): string => {
export function getFromLocalStorage(key: LocalStorageKey): string | undefined;
export function getFromLocalStorage(key: LocalStorageKey, defaultValue: string): string;
export function getFromLocalStorage(key: LocalStorageKey, defaultValue?: string): string | undefined {
let value = localStorage.getItem(key);
if (value === null) {
if (typeof defaultValue === "string") {
return defaultValue;
}
return defaultValue();
if (value) {
return value;
}
return value;
};

if (typeof defaultValue === "string") {
return defaultValue;
}

return undefined;
}

export const setLocalStorage = (key: LocalStorageKey, value: string) => {
localStorage.setItem(key, value);
Expand Down

0 comments on commit 1c3a4b3

Please sign in to comment.