Skip to content

Commit

Permalink
Fixed everything
Browse files Browse the repository at this point in the history
  • Loading branch information
JHM69 committed Jul 14, 2023
1 parent 449bf3c commit ab351ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
15 changes: 1 addition & 14 deletions pages/api/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@ const prisma = new PrismaClient();
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
console.log(req.query);
if (req.method === 'GET') {
// Get all books by likes
if (req.query.likes) {
const booksByLikes = await prisma.book.findMany({
where: {
visibility: 'true',
},
orderBy: {
likes: 'desc',
},
});
res.status(200).json(booksByLikes);
}
// Get a book by ID
else if (req.query.id) {
if (req.query.id) {
const { id } = req.query;
const book = await prisma.book.findUnique({
where: { id: String(id) },
Expand Down
35 changes: 21 additions & 14 deletions pages/book/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,47 @@ import { useRouter } from 'next/router';
import axios from 'axios';
import { Book } from '@prisma/client';
import { speakText } from '@/lib/text-to-speech';

const Read = () => {
const router = useRouter();

console.log(JSON.stringify(router));

let id = router.params.id;

// var bookId = id[0];
const { id } = router.query;
const bookid = id;

const [book, setBook] = useState<Book | null>(null);

console.log(id);

useEffect(() => {
const { id } = router.query;

const fetchBook = async () => {
try {
const response = await axios.get(`/api/books/${id}`);
console.log(response.data);
setBook(response.data);
if (id) {
// Check if id has a value
const response = await axios.get(`/api/books?id=${id}`);
console.log(response.data);
setBook(response.data);
}
} catch (error) {
console.error('Error fetching book:', error);
}
};

fetchBook();
}, [id]);
}, [id, router.query]);

if (!book) {
return <div>Book not found.</div>;
}

function removeHtmlTags(text: string): string {
const clean = /<[^>]*>/g;
return text.replace(clean, '');
}

const handleMenuSpeak = async (e: React.MouseEvent) => {
e.preventDefault();
await speakText(book.content);
await speakText(removeHtmlTags(book.content));
};

return (
<>
<Head>
Expand Down Expand Up @@ -67,7 +74,7 @@ const Read = () => {
</div>

<div className="book-content">
<article>{book.content}</article>
<div dangerouslySetInnerHTML={{ __html: book.content }} />

{/* More paragraphs go here */}
</div>
Expand Down

0 comments on commit ab351ca

Please sign in to comment.