Use lib function to fetch book

This commit is contained in:
Alfred Melch 2020-02-02 10:17:32 +01:00
parent ecc9e90bec
commit 5267561d66
2 changed files with 7 additions and 18 deletions

View File

@ -1,18 +1,10 @@
import React from 'react' import React from 'react'
import { getBook } from '../lib/gutenberg'
import styles from './Book.css' import styles from './Book.css'
import { useStore } from '../store'
export const Book = ({ author, language, title, id }) => { export const Book = ({ author, language, title }) => {
const [_, { setText, setLang }] = useStore()
const handleClick = async () => {
setText(await getBook(id))
setLang(language[0])
}
return ( return (
<div className={styles.book} onClick={handleClick}> <div className={styles.book}>
<div className={styles.title}>{title.join(' ')}</div> <div className={styles.title}>{title.join(' ')}</div>
<div className={styles.author}>{author[0]}</div> <div className={styles.author}>{author[0]}</div>
<div className={styles.language}>{language.join(' ')}</div> <div className={styles.language}>{language.join(' ')}</div>

View File

@ -5,9 +5,8 @@ import { FiSearch } from 'react-icons/fi'
import { useDispatch } from '../store' import { useDispatch } from '../store'
import { useTranslation } from 'react-i18next' 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 { Spinner } from '../styles/Spinner'
import { Dropdown } from './generics/Dropdown' import { Dropdown } from './generics/Dropdown'
import { Book } from './Book' 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 { id, language } = results[idx]
const url = `https://gutenberg.muperfredi.de/texts/${id}/stripped-body` setText(await getBook(id))
const text = await axios.get(url).then(res => res.data.body)(setText(text))( setLang(language[0])
setLang(language[0])
)
} }
useEffect(() => { useEffect(() => {
@ -72,7 +69,7 @@ export const SearchBar = () => {
items={results.map(entry => ( items={results.map(entry => (
<Book {...entry} /> <Book {...entry} />
))} ))}
onSelect={handleClick} onSelect={handleSelect}
/> />
</Dropdown> </Dropdown>
</div> </div>