about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorana <ana@ana.st>2021-08-08 13:56:29 +0200
committerana <ana@ana.st>2021-08-08 13:56:29 +0200
commit986068d95eb1c4101e5c56c92baab961455fddc0 (patch)
tree3d752ebe88a0283714ab8f419536ab7921051296
parentf331da02a0bc3cc5d3f9a6ece466db5531e316ef (diff)
downloadeureka-marks-986068d95eb1c4101e5c56c92baab961455fddc0.tar.gz
feat: implement localstorage store
-rw-r--r--src/App.svelte14
-rw-r--r--src/stores.js22
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);
+    },
+  };
+}