Added book list reading for bookshelf
This commit is contained in:
@ -1,14 +1,40 @@
|
||||
import { IBook } from "@type/book";
|
||||
import { isArrOfStr } from "@type/utils";
|
||||
import { validateResponse } from "../api";
|
||||
import { BookItem } from "../Bookshelf/BookItem";
|
||||
|
||||
export const saveBook = (bookObj: IBook, key: string): void => {
|
||||
const readBookList = <T>(
|
||||
cb: (bookList: string[]) => T,
|
||||
defaultValue: T extends void ? undefined : T
|
||||
) => {
|
||||
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));
|
||||
}
|
||||
if (isArrOfStr(bookList)) return cb(bookList);
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
export const saveBook = (bookObj: IBook, key: string) =>
|
||||
readBookList((bookList) => {
|
||||
if (!bookList.includes(key)) {
|
||||
const newBookList = [key, ...bookList];
|
||||
|
||||
localStorage.setItem("list", JSON.stringify(newBookList));
|
||||
localStorage.setItem(key, JSON.stringify(bookObj));
|
||||
}
|
||||
}, undefined);
|
||||
|
||||
export const readBooks = (): IBook[] =>
|
||||
readBookList(
|
||||
(bookList) =>
|
||||
bookList
|
||||
.map((hash) => JSON.parse(localStorage.getItem(hash) || "{}"))
|
||||
.filter((e) => {
|
||||
try {
|
||||
return validateResponse(e);
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
Reference in New Issue
Block a user