From a3a7b8c9bb33d491545c4096d8eed30d04077e46 Mon Sep 17 00:00:00 2001 From: sarowarhosen03 Date: Thu, 16 Nov 2023 11:28:53 +0600 Subject: [PATCH 1/2] refactor to use useBucketList.js hooks #18 this hook allow to sync useBucketLit item with localstorage in more efficient way --- src/AddToBucket.jsx | 2 -- src/App.jsx | 6 ++---- src/BucketListItem.jsx | 3 +-- src/hooks/useBucketList.js | 20 ++++++++++++++++++++ src/util/localStorageUtil.js | 23 ----------------------- 5 files changed, 23 insertions(+), 31 deletions(-) create mode 100644 src/hooks/useBucketList.js delete mode 100644 src/util/localStorageUtil.js diff --git a/src/AddToBucket.jsx b/src/AddToBucket.jsx index ee28758..2d2e15a 100644 --- a/src/AddToBucket.jsx +++ b/src/AddToBucket.jsx @@ -1,6 +1,5 @@ import { createSignal } from "solid-js"; -import { saveWish } from "./util/localStorageUtil"; export function AddToBucket(props) { const [newItem, setNewItem] = createSignal(''); @@ -30,7 +29,6 @@ export function AddToBucket(props) { }, ...items, ]; - saveWish(allWishes); return allWishes; }); setNewItem(''); diff --git a/src/App.jsx b/src/App.jsx index 141ce20..f978cd2 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,11 +1,9 @@ -import { createSignal } from "solid-js"; import { BucketListItem } from "./BucketListItem"; import { AddToBucket } from "./AddToBucket"; - -import { getWishes } from "./util/localStorageUtil"; +import useBucketList from "./hooks/useBucketList"; function App() { - const [items, setItems] = createSignal(getWishes()); + const [items, setItems] = useBucketList(); return ( diff --git a/src/BucketListItem.jsx b/src/BucketListItem.jsx index a10f689..e27bb43 100644 --- a/src/BucketListItem.jsx +++ b/src/BucketListItem.jsx @@ -1,4 +1,4 @@ -import { saveWish } from "./util/localStorageUtil"; + export function BucketListItem(props) { return ( @@ -21,7 +21,6 @@ export function BucketListItem(props) { : item ); - saveWish(newItems); return newItems; }); }} diff --git a/src/hooks/useBucketList.js b/src/hooks/useBucketList.js new file mode 100644 index 0000000..7089618 --- /dev/null +++ b/src/hooks/useBucketList.js @@ -0,0 +1,20 @@ +// Desc: Custom hook to manage bucket list items to local storage + +import { createEffect, createSignal } from "solid-js"; + +function useBucketList() { + const [items, setItems] = createSignal([]); + + createEffect(() => { + const bucketList = localStorage.getItem('bucket-list'); + if (bucketList) setItems(JSON.parse(bucketList)); + + }); + + createEffect(() => { + localStorage.setItem('bucket-list', JSON.stringify(items())); + }); + return [items, setItems]; +} + +export default useBucketList \ No newline at end of file diff --git a/src/util/localStorageUtil.js b/src/util/localStorageUtil.js deleted file mode 100644 index 2198a59..0000000 --- a/src/util/localStorageUtil.js +++ /dev/null @@ -1,23 +0,0 @@ -const dummy_Data = [ - { id: crypto.randomUUID(), text: "Walk the dog", complete: false }, - { id: crypto.randomUUID(), text: "Do homework", complete: true }, -]; - -/** - * return saved wishes data from local-storage. - * if no data is saved to localstorage then return dummy data - */ -export function getWishes() { - const wishes = JSON.parse(localStorage.getItem("solid-bucket-list")); - - return wishes ? wishes : dummy_Data; -} - -// Save new wish to localstorage -export function saveWish(wishes) { - localStorage.setItem("solid-bucket-list", JSON.stringify(wishes)); -} - -export function setComplete(wishes) { - saveWish(wishes); -} From ccb51241f698d847ee8d7240efe1849e38c51c61 Mon Sep 17 00:00:00 2001 From: sarowarhosen03 Date: Thu, 16 Nov 2023 11:29:10 +0600 Subject: [PATCH 2/2] updated .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 76add87..ca72558 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ node_modules -dist \ No newline at end of file +dist + +#editor based +.idea \ No newline at end of file