diff options
author | ana <ana@ana.st> | 2021-08-08 13:56:29 +0200 |
---|---|---|
committer | ana <ana@ana.st> | 2021-08-08 13:56:29 +0200 |
commit | 986068d95eb1c4101e5c56c92baab961455fddc0 (patch) | |
tree | 3d752ebe88a0283714ab8f419536ab7921051296 | |
parent | f331da02a0bc3cc5d3f9a6ece466db5531e316ef (diff) | |
download | eureka-marks-986068d95eb1c4101e5c56c92baab961455fddc0.tar.gz |
feat: implement localstorage store
-rw-r--r-- | src/App.svelte | 14 | ||||
-rw-r--r-- | src/stores.js | 22 |
2 files changed, 34 insertions, 2 deletions
diff --git a/src/App.svelte b/src/App.svelte index a273fcc..27f7f73 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,7 +1,17 @@ -<script></script> +<script> + import { level } from "./stores"; + + function levelChanged(evt) { + level.set(Number(evt.data)); + } +</script> <div class="app"> - <h2>Level: <input type="number" min="1" max="60" /></h2> + <h2> + Level: <input on:input={levelChanged} type="number" min="1" max="60" /> + </h2> + + Level is {$level} </div> <style></style> diff --git a/src/stores.js b/src/stores.js new file mode 100644 index 0000000..f203bfd --- /dev/null +++ b/src/stores.js @@ -0,0 +1,22 @@ +import { writable } from "svelte/store"; + +const localStorage = window.localStorage; + +export const level = localStorageStore(0); + +function localStorageStore(key) { + const item = localStorage.getItem(key); + const { subscribe, set } = writable(JSON.parse(item) || null); + + return { + subscribe, + set: (value) => { + localStorage.setItem(key, JSON.stringify(value)); + set(value); + }, + clear: () => { + localStorage.removeItem(key); + set(null); + }, + }; +} |