Added processed book saving to localstorage

This commit is contained in:
2021-07-15 19:12:35 +05:00
parent c25873158d
commit 74b3cc725d
6 changed files with 33 additions and 10 deletions

14
src/utils/localStorage.ts Normal file
View File

@ -0,0 +1,14 @@
import { IBook } from "../types/book";
import { isArrOfStr } from "../types/utils";
export const saveBook = (bookObj: IBook, key: string): void => {
const bookListStr = localStorage.getItem("list") || "[]";
const bookList: unknown = JSON.parse(bookListStr);
if (isArrOfStr(bookList) && !bookList.includes(key)) {
const newBookList = [key, ...bookList];
localStorage.setItem("list", JSON.stringify(newBookList));
localStorage.setItem(key, JSON.stringify(bookObj));
}
};