From 258483b023ef034e6f363c84ebc61d64071a006d Mon Sep 17 00:00:00 2001 From: "Rayyan (Rayy)" Date: Tue, 14 Nov 2023 09:23:56 +0000 Subject: [PATCH] Add further work to Book Detail page --- src/App.js | 173 ++++++++++++++++++++++++-------------------- src/pages/Detail.js | 55 ++++++++++---- src/pages/Home.js | 2 +- 3 files changed, 136 insertions(+), 94 deletions(-) diff --git a/src/App.js b/src/App.js index 990deba..1c08379 100644 --- a/src/App.js +++ b/src/App.js @@ -2,17 +2,23 @@ import { FirebaseConfig } from "./config/Config" import { initializeApp } from "firebase/app" import { Routes, Route } from "react-router-dom" import { useState, useEffect } from "react" -import { getAuth, +import { + getAuth, createUserWithEmailAndPassword, onAuthStateChanged, signOut, - signInWithEmailAndPassword } from "firebase/auth"; -import { getFirestore, - collection, - query, - where, - getDocs } from "firebase/firestore"; - import {getStorage} from "firebase/storage" + signInWithEmailAndPassword +} from "firebase/auth"; +import { + getFirestore, + collection, + query, + where, + getDoc, + getDocs, + doc +} from "firebase/firestore"; +import { getStorage } from "firebase/storage" import { Header } from "./components/Header" import './App.css' @@ -34,8 +40,8 @@ function App() { const FBauth = getAuth(FBapp) const FBdb = getFirestore(FBapp) const FBstorage = getStorage(FBapp) - - + + // navigation array const navItems = [ { label: "Home", link: "/" }, @@ -49,35 +55,35 @@ function App() { { label: "Home", link: "/" }, { label: "About", link: "/about" }, { label: "Contact", link: "/contact" }, - + ] /// application states const [nav, setNav] = useState(navItems) const [auth, setAuth] = useState(false) const [data, setData] = useState([]) - const [ fetching , setFetching ] = useState( false ) + const [fetching, setFetching] = useState(false) - useEffect( () => { - if( data.length === 0 && fetching === false ) { + useEffect(() => { + if (data.length === 0 && fetching === false) { readData() - setFetching( true ) + setFetching(true) } }, [data]) - // authentication observer - onAuthStateChanged(FBauth, (user) => { - if( user ) { + // authentication observer + onAuthStateChanged(FBauth, (user) => { + if (user) { // currently authenticated - setAuth( user ) - setNav( AuthnavItems ) - + setAuth(user) + setNav(AuthnavItems) + } else { // currently unauthenticated - setAuth( false ) - setNav( navItems ) + setAuth(false) + setNav(navItems) } }) @@ -89,64 +95,73 @@ function App() { // signing up a user const signUp = (email, password) => { createUserWithEmailAndPassword(FBauth, email, password) - .then( (userCredential) => { + .then((userCredential) => { // do something }) .catch((error) => console.log(error.message)) - } - - const logOut = () => { - signOut(FBauth).then( () => { - // user is signed out - }) -} - -const signIn = (email, password) => { - return new Promise((resolve, reject) => { - signInWithEmailAndPassword(FBauth, email, password) - .then(() => { - // user is signed in - resolve(true) - }) - .catch((error) => { - console.log(error) - reject( error.code ) - }) - }) -} - -// function to get data -const readData = async () => { - const querySnapshot = await getDocs( collection(FBdb, "books") ) - let listdata = [] - querySnapshot.forEach( (doc) => { - let item = doc.data() - item.id = doc.id - listdata.push( item ) - }) - setData( listdata ) - -} - - - return ( -
-
- - - - } /> - } /> - } /> - } /> - } /> - } /> - } /> - - - -
- ); } - export default App; + const logOut = () => { + signOut(FBauth).then(() => { + // user is signed out + }) + } + + const signIn = (email, password) => { + return new Promise((resolve, reject) => { + signInWithEmailAndPassword(FBauth, email, password) + .then(() => { + // user is signed in + resolve(true) + }) + .catch((error) => { + console.log(error) + reject(error.code) + }) + }) + } + + // function to get data + const readData = async () => { + const querySnapshot = await getDocs(collection(FBdb, "books")) + let listdata = [] + querySnapshot.forEach((doc) => { + let item = doc.data() + item.id = doc.id + listdata.push(item) + }) + setData(listdata) + + } + + // function to get a single item + const getDocument = async (itemId) => { + const docRef = doc(FBdb, "books", itemId) + const docSnap = await getDoc(docRef) + let book = docSnap.data() + book.id = itemId + return book + } + + + return ( +
+
+ + + + } /> + } /> + } /> + } /> + } /> + } /> + } /> + + + +
+ ); +} + +export default App; diff --git a/src/pages/Detail.js b/src/pages/Detail.js index b19d52c..cb5c668 100644 --- a/src/pages/Detail.js +++ b/src/pages/Detail.js @@ -1,22 +1,49 @@ import Row from 'react-bootstrap/Row'; import Col from 'react-bootstrap/Col'; import Container from "react-bootstrap/Container"; - +import { useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; +import { ItemImage } from '../components/ItemImage'; export function Detail(props) { + const [bookData, setBookData] = useState() + let { id } = useParams(); - return( - - - -

Image

- - -

Details

-

{id}

- -
-
- ) + + useEffect( () => { + if( !bookData ) { + props.handler(id).then( (book) => setBookData(book) ) + } + }, [id]) + + + if( bookData ) { + return( + + + +

{bookData.book_title}

+ +
+ + + + + +

More information

+

Summary

+

{bookData.summary}

+

Author

+

{bookData.author}

+

ISBN

+

ISBN10 {bookData.isbn10}

+

ISBN13 {bookData.isbn13}

+ +
+
+ ) + } + else { + return null + } } \ No newline at end of file diff --git a/src/pages/Home.js b/src/pages/Home.js index 58c23e8..6464a9b 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -18,7 +18,7 @@ export function Home(props) { const ItemCards = books.map( ( book, itemkey ) => { const itemLink = `/detail/${book.id}` return( - +