import "./bookCard.css"; import axios from "axios"; import Skeleton from "react-loading-skeleton"; import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import IconButton from "../../md-components/IconButton"; import Icon from "../../md-components/Icon"; function personToString(person) { let tmpString; if (person.lastName) { tmpString = person.lastName; } else { tmpString = person.firstName; } if (person.middleName) { tmpString += " " + person.middleName[0] + "."; } if (person.lastName && person.firstName) { tmpString += " " + person.firstName[0] + "."; } return tmpString; } function authorsString(authors) { let authorsStr = authors .slice(0, 2) .map((author) => personToString(author)) .join(", "); if (authors.length > 2) { authorsStr += " и др."; } return authorsStr; } const BookCard = ({ id, title, authors, reader, offline, fromSearch, collectionDelId, fixed, filetype, }) => { const [loaded, setLoaded] = useState(false); const [bookImgSrc, setBookImgSrc] = useState(""); async function loadImgFromCache() { if (window.caches === undefined) { setBookImgSrc(axios.defaults.baseURL + "/book/" + id + "/cover"); return; } let cache = await window.caches.open("bookCovers"); let img = await cache.match(`/api/book/${id}/cover`); if (img === undefined) { if (window.onLine) { setBookImgSrc(axios.defaults.baseURL + "/book/" + id + "/cover"); return; } else { setBookImgSrc("/favicon.png"); return; } } let blob = await img.blob(); setBookImgSrc(URL.createObjectURL(blob)); } useEffect(() => { loadImgFromCache(); }, []); return ( {!offline && !window.onLine ? (
) : ( )}
{filetype}
{/* { collectionDelId ?
{ axios.post("/collection/"+collectionDelId, {book_id: String(id)}) }}> delete
: <> } */}
{loaded ? ( <> ) : ( )} setLoaded(true)} style={{ pointerEvents: "none", boxShadow: "0px 0px 20px gray", width: "100%", }} src={bookImgSrc} />
{title} {authorsString(authors)}
); }; export default BookCard;