Skip to content

Commit 7bced58

Browse files
committed
bookstore component now has books displayed!
1 parent b23b367 commit 7bced58

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

client/src/App.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import './App.css';
33
import { Route, Switch, Redirect } from 'react-router-dom';
44

55
// components
6-
import { Input, List, NavBar } from './components';
6+
import { Input, List, NavBar, Book, BookStore } from './components';
77

88
// pages
99
// import { Store, Orders, Cart } from './pages';
1010

11+
let data = {"isbn":"9781781100264","title":"Harry Potter and the Deathly Hallows (Harry Potter, #7)","year":2015,"genre":"Fiction","page_count":784,"price":10.99,"commission":0.01,"url":"http://books.google.com/books/content?id=gCtazG4ZXlQC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api","quantity":34,"is_purchasable":null,"publisher_id":10000,"publisher_name":"Pottermore Publishing","author_name":"J.K. Rowling"}
12+
1113
function App() {
1214
return (
15+
1316
<Fragment>
17+
<BookStore></BookStore>
1418
{/* <div className="container">
1519
<Input />
1620
<List />

client/src/components/Book/Book.tsx

-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { request } from 'http';
21
import React, { useEffect, useState } from 'react';
32

43
interface BookProp {
@@ -12,12 +11,6 @@ interface BookData {
1211
price: number
1312
}
1413

15-
// defined outside to supress react-hooks/exhaustive-deps
16-
async function requestBookData(isbn:string, setData:React.Dispatch<React.SetStateAction<BookData | undefined>>) {
17-
18-
19-
}
20-
2114
export const Book = (props:BookProp) => {
2215

2316
const [bookData, setBookData] = useState<undefined | BookData>(undefined);
+46-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,56 @@
1-
import {useState} from "react";
2-
1+
import {useEffect, useState} from "react";
2+
import lodash from "lodash"
3+
import {Book} from "./Book"
34
const booksPerPage = 20;
5+
const booksPerRow = 4;
6+
7+
interface BookType {
8+
isbn: string
9+
}
10+
11+
const arraysEqual = (arr1:BookType[][], arr2:BookType[][]) => {
12+
return lodash.isEmpty(lodash.differenceWith(
13+
lodash.flatten(arr1),
14+
lodash.flatten(arr2),
15+
lodash.isEqual
16+
));
17+
}
418

519
export const BookStore = () => {
620

7-
const [page, setPage] = useState(1);
21+
const [page, setPage] = useState(0);
22+
const [books, setBooks] = useState<BookType[][]>([[{"isbn":""}]]);
823

9-
//const refreshPage() = async e => {
24+
const getPage = async () => {
25+
const response = await fetch(`http://localhost:5000/books?offset=${page*booksPerPage}&limit=${(page+1)*booksPerPage}`);
26+
const jsonData = await response.json();
27+
const bookChunks:BookType[][] = lodash.chunk(jsonData["rows"], booksPerRow);
28+
if (!arraysEqual(books, bookChunks)) {
29+
console.log("set books to bookchunks");
30+
setBooks(bookChunks);
31+
}
32+
33+
34+
};
1035

11-
//}
36+
useEffect(() => {
37+
getPage();
38+
}, [books]);
1239

1340
return (
14-
<div></div>
41+
<div className="container-fluid">
42+
<div className="container">
43+
{books.map(bookrow => {
44+
return <div className="row">
45+
{bookrow.map(book => {
46+
return <div className="col">
47+
<Book ISBN={book.isbn}></Book>
48+
</div>
49+
})}
50+
</div>
51+
})}
52+
</div>
53+
<div></div> {/* TODO footer */}
54+
</div>
1555
)
1656
};

client/src/components/index.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
export * from './SampleInput/Input';
33
export * from './SampleList/List';
44
export * from './Navigation/NavBar';
5-
export * from './Book/Book';
5+
export * from './Book/Book';
6+
export * from './Book/BookStore';

0 commit comments

Comments
 (0)