diff --git a/snowpack.config.js b/snowpack.config.js index e61b9af..1cdead0 100644 --- a/snowpack.config.js +++ b/snowpack.config.js @@ -21,7 +21,7 @@ module.exports = { devOptions: { open: "none", }, - alias: { "@assets": "./src/assets" }, + alias: { "@assets": "./src/assets", "@utils": "./src/utils" }, exclude: ["**/node_modules/**/*", "**/*.test.*"], env: { // API_URL: "https://publitebackend.dmitriy.icu", diff --git a/src/UploadForm/index.tsx b/src/UploadForm/index.tsx index 85d2949..83c5034 100644 --- a/src/UploadForm/index.tsx +++ b/src/UploadForm/index.tsx @@ -3,6 +3,7 @@ import React, { useState } from "react"; import plusIcon from "@assets/plus.svg"; import styles from "./UploadForm.module.css"; import { submitFile, validateResponse, validState } from "../api"; +import { saveBook } from "@utils/localStorage"; export const UploadForm = () => { const [error, setError] = useState(""); @@ -17,8 +18,10 @@ export const UploadForm = () => { const res = await submitFile(file); setLoading(false); + console.log(validateResponse(res)); + if (validateResponse(res)) { - // TODO: save book to localstorage + saveBook(res, res.hash || Date.now().toString()); // TODO: redirect to main menu } } diff --git a/src/types/book.ts b/src/types/book.ts index 1c267d9..3463afe 100644 --- a/src/types/book.ts +++ b/src/types/book.ts @@ -1,10 +1,9 @@ export const requiredBookProps = ["title", "author", "content"] as const; export const optionalBookProps = ["cover", "hash"] as const; -export type IBook = - | { - [key in typeof requiredBookProps[number]]: string; - } - | { - [key in typeof optionalBookProps[number]]: string | undefined; - }; +export type IBook = { + [key in typeof requiredBookProps[number]]: string; +} & + { + [key in typeof optionalBookProps[number]]: string | undefined; + }; diff --git a/src/types/utils.ts b/src/types/utils.ts new file mode 100644 index 0000000..d1e430b --- /dev/null +++ b/src/types/utils.ts @@ -0,0 +1,7 @@ +export const isArrOfStr = (obj: unknown): obj is string[] => { + if (Array.isArray(obj)) { + for (const el of obj) if (typeof el !== "string") return false; + return true; + } + return false; +}; diff --git a/src/utils/localStorage.ts b/src/utils/localStorage.ts new file mode 100644 index 0000000..e6c033d --- /dev/null +++ b/src/utils/localStorage.ts @@ -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)); + } +}; diff --git a/tsconfig.json b/tsconfig.json index 99b7415..f1ae2a6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,6 @@ "module": "ES2020", "moduleResolution": "node", "resolveJsonModule": true, - "paths": { "@assets/*": ["src/assets/*"] } + "paths": { "@assets/*": ["./src/assets/*"], "@utils/*": ["./src/utils/*"] } } }