From c0278b9688d206d1a43bb2f2d6137996928512a1 Mon Sep 17 00:00:00 2001 From: "Rayyan (Rayy)" Date: Tue, 7 Nov 2023 12:23:07 +0000 Subject: [PATCH] Getting Images from Firebase Storage (Working) --- src/components/ItemImage.js | 35 ++++++++++++++ src/pages/Home.js | 95 ++++++++++++------------------------- 2 files changed, 65 insertions(+), 65 deletions(-) create mode 100644 src/components/ItemImage.js diff --git a/src/components/ItemImage.js b/src/components/ItemImage.js new file mode 100644 index 0000000..e0bb6f2 --- /dev/null +++ b/src/components/ItemImage.js @@ -0,0 +1,35 @@ +import { useState, useEffect, useContext } from 'react' +import { StorageContext } from '../contexts/StorageContext' +import { ref, getDownloadURL } from 'firebase/storage' +import Card from 'react-bootstrap/Card' + +export function ItemImage ( props ) { + const [image,setImage] = useState() + + const storage = useContext( StorageContext) + + useEffect( () => { + if( props.source ) { + const imgref = ref( storage, `book_covers/${props.source}`) + getDownloadURL( imgref ) + .then( (url) => setImage(url) ) + .catch( err => console.log(err) ) + } + }, [props.source] ) + + if(!image) { + const defstyle={ + backgroundColor: "#cccccc", + aspectRatio: "4/3" + } + return ( +
+
+ ) + } + else { + return ( + + ) + } +} \ No newline at end of file diff --git a/src/pages/Home.js b/src/pages/Home.js index 7f7eb9b..76f2aec 100644 --- a/src/pages/Home.js +++ b/src/pages/Home.js @@ -3,73 +3,38 @@ import Col from 'react-bootstrap/Col'; import Container from "react-bootstrap/Container"; import Card from "react-bootstrap/Card"; -import { ref, getDownloadURL } from "firebase/storage" + import { useState, useEffect, useContext } from "react" -import { StorageContext } from '../contexts/StorageContext'; +import { ItemImage } from "../components/ItemImage" export function Home(props) { - const [books, setBooks] = useState([props.items]) + const [books,setBooks] = useState([]) - const Storage = useContext(StorageContext) - console.log(props.items) - - // useEffect( () => { - // if( books.length === 0 ) - // setBooks(props.items) - // } - // , [props.items]) - - // card image component - const ItemImage = (props) => { - const [image, setImage] = useState() - useEffect(() => { - - - }, [props.name, image]) - const imgRef = ref(Storage, `book_covers/${props.name}`) - if (image) { - return ( - - ) - } - - else { - return ( -

Loading...

- ) - } - } - - if (!books) { - return null - } - else { - // collection - const Items = books.map((item, key) => { - return ( - - - - - {item.book_title} - - By {item.author} - - {item.cover_image} - - - - - ) + useEffect(() => { + console.log( props.items ) + setBooks( props.items ) + }, [props.items]) + + + + const ItemCards = books.map( ( book, itemkey ) => { + return( + + + + + { book.book_title } + + + + ) }) - return ( - - - {Items} - - - - ) - } - -} \ No newline at end of file + + return ( + + + {ItemCards} + + + ) + }