vkrastev97

book

Nov 1st, 2020
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import React, { useState } from 'react';
  2. import PropTypes from 'prop-types';
  3. import './Book.css';
  4. import Card from 'react-bootstrap/Card';
  5. //import decode from 'jwt-decode';
  6. import Reviews from './Reviews';
  7. import { useAuth } from '../../auth/authContext';
  8.  
  9. //import Reviews from './Reviews';
  10. /* eslint-disable */
  11. const Book = (props) => {
  12. const { book, borrowBook, returnBook } = props;
  13. const [showTab, setShowTab] = useState(false);
  14. const [status, setStatus] = useState('');
  15. const handleBorrow = () => {
  16. setStatus('borrowed');
  17. borrowBook(book.id);
  18. };
  19. const handleReturn = () => returnBook(book.id);
  20.  
  21. const { user } = useAuth();
  22.  
  23. // console.log(book);
  24. const imgSource = () => {
  25. if (book.id % 2 === 0) {
  26. return 'https://forum.telerikacademy.com/uploads/default/original/2X/b/b89c87e412a2ac743f3f6ce63c5e34123b1c8713.jpeg';
  27. } else {
  28. return 'https://forum.telerikacademy.com/uploads/default/optimized/2X/2/2be7497b93dc1f3f0dca455d1d5bfc2adaecf900_2_750x750.jpeg';
  29. }
  30. };
  31.  
  32. const Buttons = () => {
  33. if (book.book_status == 'free') {
  34. return <button onClick={handleBorrow}>Borrow</button>;
  35. } else if (
  36. book.book_status == 'borrowed' &&
  37. user.username === book.borrowed_by
  38. ) {
  39. return <button onClick={handleReturn}>Return</button>;
  40. } else {
  41. return <div></div>;
  42. }
  43. };
  44.  
  45. const extra = (
  46. <div>
  47. <p>Author: {book.author}</p>
  48. <p>Status: {book.book_status}</p>
  49. <Buttons />
  50. <br />
  51. <Reviews book={book} />
  52. </div>
  53. );
  54.  
  55. const handleToggle = () => {
  56. setShowTab((preState) => !preState);
  57. };
  58.  
  59. return (
  60. <Card style={{ width: '18rem' }}>
  61. <Card.Img variant='top' src={imgSource()} />
  62. <Card.Body>
  63. <Card.Title>{book.name}</Card.Title>
  64. <input
  65. type='button'
  66. value={`${showTab ? 'Hide' : 'Show'} Info`}
  67. onClick={handleToggle}
  68. ></input>
  69. {showTab && extra}
  70. </Card.Body>
  71. </Card>
  72. );
  73. };
  74.  
  75. Book.propTypes = {
  76. book: PropTypes.object.isRequired,
  77. };
  78.  
  79. export default Book;
  80.  
Add Comment
Please, Sign In to add comment