-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
176 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,5 @@ const Book = React.memo(({ book, isFavorite, handleFavorite }) => { | |
); | ||
}); | ||
|
||
export default Book; | ||
export {Book}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,147 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import Book from './Book'; | ||
import styles from '../styles/book-search.module.css'; | ||
import React, { useEffect, useState } from "react"; | ||
|
||
const baseUrl = 'https://gutendex.com/books'; | ||
export const languages = ['pl', 'en', 'fr']; | ||
import styles from "../styles/book-search.module.css"; | ||
import { Book } from "./Book"; | ||
import type { AppContextType, Book_t, JsonResponse } from "./appContext"; | ||
|
||
const baseUrl = "https://gutendex.com/books"; | ||
export const languages = ["pl", "en", "fr"]; | ||
|
||
export function toggleStringInList(string, list) { | ||
const DELAY = 300; | ||
|
||
export function toggleStringInList(string: string, list: string[]): string[] { | ||
const index = list.indexOf(string); | ||
if (index === -1) { | ||
// String not found in the list, so add it | ||
list.push(string); | ||
// String not found in the list, so add it | ||
list.push(string); | ||
} else { | ||
// String found in the list, so remove it | ||
list.splice(index, 1); | ||
// String found in the list, so remove it | ||
list.splice(index, 1); | ||
} | ||
return list; | ||
} | ||
|
||
|
||
function BooksSearch({ favorites, handleFavorite}) { | ||
const [books, setBooks] = useState(null); | ||
const [searchQuery, setSearchQuery] = useState(''); | ||
function BooksSearch({ favorites, handleFavorite }: AppContextType) { | ||
const [books, setBooks] = useState<Book_t[] | null>(null); | ||
const [searchQuery, setSearchQuery] = useState(""); | ||
const [currUrl, setCurrUrl] = useState(baseUrl); | ||
const [prevUrl, setPrevUrl] = useState(""); | ||
const [nextUrl, setNextUrl] = useState(""); | ||
const [filterTags, setFilterdTags] = useState([]); | ||
console.log(filterTags); | ||
|
||
|
||
|
||
useEffect(() => { | ||
const delay = searchQuery ? 300 : 0; // no delay for initial fetch | ||
const getBooksByDebouncing = setTimeout(async () => { | ||
const delay = searchQuery ? DELAY : 0; // no delay for initial fetch | ||
const handler = async () => { | ||
const url = new URL(currUrl); | ||
if (searchQuery) { | ||
url.searchParams.append('search', searchQuery); | ||
} | ||
console.log(filterTags); | ||
|
||
if(filterTags.length >= 1){ | ||
url.searchParams.append('languages', filterTags.join(',')); | ||
url.searchParams.append("search", searchQuery); | ||
} | ||
|
||
console.log(url); | ||
if (filterTags.length >= 1) { | ||
url.searchParams.append("languages", filterTags.join(",")); | ||
} | ||
|
||
try { | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
const booksWithHtml = data.results.map(book => { | ||
const data = (await response.json()) as JsonResponse; | ||
const booksWithHtml = data.results.map((book) => { | ||
return { | ||
...book, | ||
htmlLink: book.formats['text/html'] | ||
htmlLink: book.formats["text/html"], | ||
}; | ||
}); | ||
setBooks(booksWithHtml); | ||
setPrevUrl(data.previous); | ||
setNextUrl(data.next); | ||
} catch (error) { | ||
console.error('An error occured:', error); | ||
// eslint-disable-next-line no-console | ||
console.error("Error fetching books", error); | ||
} | ||
}, delay); | ||
|
||
return () => clearTimeout(getBooksByDebouncing) | ||
|
||
}, [searchQuery, currUrl, filterTags]); | ||
}; | ||
const getBooksByDebouncing = setTimeout(handler, delay); | ||
|
||
return () => clearTimeout(getBooksByDebouncing); | ||
}, [searchQuery, currUrl, filterTags]); | ||
|
||
if(!books ){ | ||
if (!books) { | ||
return <div className={styles.loading}>Loading books...</div>; | ||
} | ||
|
||
return ( | ||
<div> | ||
<div className={styles.content_container}> | ||
<div className={styles.content_container}> | ||
<div> | ||
<input | ||
className={styles.search_box} | ||
type="text" | ||
value={searchQuery} | ||
onChange={(e) => { | ||
setCurrUrl(baseUrl); | ||
setSearchQuery(e.target.value); | ||
}} | ||
placeholder="Search for books..." | ||
/> | ||
</div> | ||
|
||
<div> | ||
{languages.map((clickedLanguage) => ( | ||
<button | ||
className={`${styles.language_button} ${filterTags.includes(clickedLanguage) ? styles.active : ""}`} | ||
onClick={() => { | ||
var newList = [...filterTags]; | ||
toggleStringInList(clickedLanguage, newList); | ||
setBooks(null); | ||
setCurrUrl(baseUrl); | ||
setFilterdTags(newList); | ||
}} | ||
> | ||
{clickedLanguage.toUpperCase()} | ||
</button> | ||
))} | ||
</div> | ||
|
||
<div className={styles.nav_pages}> | ||
{prevUrl ? ( | ||
<div> | ||
<button | ||
className={styles.page_button} | ||
onClick={() => { | ||
setBooks(null); | ||
setCurrUrl(prevUrl); | ||
}} | ||
> | ||
Previous | ||
</button> | ||
</div> | ||
) : null} | ||
{nextUrl ? ( | ||
<div> | ||
<button | ||
className={styles.page_button} | ||
onClick={() => { | ||
setBooks(null); | ||
setCurrUrl(nextUrl); | ||
}} | ||
> | ||
Next | ||
</button> | ||
</div> | ||
) : null} | ||
</div> | ||
</div> | ||
|
||
<div> | ||
<input className={styles.search_box} | ||
type="text" | ||
value={searchQuery} | ||
onChange={(e) => { setCurrUrl(baseUrl); setSearchQuery(e.target.value); }} | ||
placeholder="Search for books..." | ||
/> | ||
{books.map((book) => ( | ||
<Book | ||
key={book.id} | ||
book={book} | ||
isFavorite={favorites.some((f: Book_t) => f.id === book.id)} | ||
handleFavorite={handleFavorite} | ||
/> | ||
))} | ||
</div> | ||
|
||
<div> | ||
{ | ||
languages.map(clickedLanguage => ( | ||
<button className={`${styles.language_button} ${filterTags.includes(clickedLanguage) ? styles.active : ''}`} onClick={() => { | ||
var newList = [...filterTags]; | ||
toggleStringInList(clickedLanguage, newList); | ||
setBooks(null); | ||
setCurrUrl(baseUrl); | ||
setFilterdTags(newList); | ||
}}>{clickedLanguage.toUpperCase()}</button> | ||
)) | ||
} | ||
</div> | ||
|
||
<div className={styles.nav_pages}> | ||
{prevUrl && <div><button className={styles.page_button} onClick={() => {setBooks(null); setCurrUrl(prevUrl); }}>Previous</button></div>} | ||
{nextUrl && <div><button className={styles.page_button} onClick={() => {setBooks(null); setCurrUrl(nextUrl); }}>Next</button></div>} | ||
</div> | ||
|
||
|
||
|
||
</div> | ||
|
||
|
||
<div> | ||
{books.map(book => ( | ||
<Book | ||
key={book.id} | ||
book={book} | ||
isFavorite={favorites.some(f => f.id === book.id)} | ||
handleFavorite={handleFavorite} | ||
/> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default BooksSearch; | ||
export { BooksSearch }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ const Footer = () => { | |
); | ||
}; | ||
|
||
export default Footer; | ||
export {Footer} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ const Hero = () => { | |
); | ||
}; | ||
|
||
export default Hero; | ||
export {Hero} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,22 @@ | ||
import Link from 'next/link'; | ||
import React from 'react'; | ||
import styles from '../styles/navbar.module.css'; | ||
import Link from "next/link"; | ||
import React from "react"; | ||
|
||
const NavBar = () => { | ||
import styles from "../styles/navbar.module.css"; | ||
|
||
export const NavBar = () => { | ||
return ( | ||
<nav className={styles.navbar}> | ||
<ul className={styles.navLinks}> | ||
<li className={styles.navLinkItem}> | ||
<Link href="/"> | ||
Main Page | ||
</Link> | ||
<Link href="/">Main Page</Link> | ||
</li> | ||
<li className={styles.navLinkItem}> | ||
<Link href="/books"> | ||
Books | ||
</Link> | ||
<Link href="/books">Books</Link> | ||
</li> | ||
<li className={styles.navLinkItem}> | ||
<Link href="/favorites"> | ||
Favorites | ||
</Link> | ||
<Link href="/favorites">Favorites</Link> | ||
</li> | ||
</ul> | ||
</nav> | ||
); | ||
}; | ||
|
||
export default NavBar; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.