From 5267561d66c094f0ad9d640ee0cc3fa31d8c79bd Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Sun, 2 Feb 2020 10:17:32 +0100 Subject: [PATCH] Use lib function to fetch book --- src/components/Book.js | 12 ++---------- src/components/SearchBar.js | 13 +++++-------- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/components/Book.js b/src/components/Book.js index e00a038..bfe3017 100644 --- a/src/components/Book.js +++ b/src/components/Book.js @@ -1,18 +1,10 @@ import React from 'react' -import { getBook } from '../lib/gutenberg' import styles from './Book.css' -import { useStore } from '../store' -export const Book = ({ author, language, title, id }) => { - const [_, { setText, setLang }] = useStore() - - const handleClick = async () => { - setText(await getBook(id)) - setLang(language[0]) - } +export const Book = ({ author, language, title }) => { return ( -
+
{title.join(' – ')}
{author[0]}
{language.join(' – ')}
diff --git a/src/components/SearchBar.js b/src/components/SearchBar.js index 40d530d..38bd950 100644 --- a/src/components/SearchBar.js +++ b/src/components/SearchBar.js @@ -5,9 +5,8 @@ import { FiSearch } from 'react-icons/fi' import { useDispatch } from '../store' import { useTranslation } from 'react-i18next' -import axios from 'axios' -import { search } from '../lib/gutenberg' +import { search, getBook } from '../lib/gutenberg' import { Spinner } from '../styles/Spinner' import { Dropdown } from './generics/Dropdown' import { Book } from './Book' @@ -32,12 +31,10 @@ export const SearchBar = () => { [] ) - const handleClick = async idx => { + const handleSelect = async idx => { const { id, language } = results[idx] - const url = `https://gutenberg.muperfredi.de/texts/${id}/stripped-body` - const text = await axios.get(url).then(res => res.data.body)(setText(text))( - setLang(language[0]) - ) + setText(await getBook(id)) + setLang(language[0]) } useEffect(() => { @@ -72,7 +69,7 @@ export const SearchBar = () => { items={results.map(entry => ( ))} - onSelect={handleClick} + onSelect={handleSelect} />