Getting Images from Firebase Storage (Working)
This commit is contained in:
parent
0d7d7d575a
commit
c0278b9688
|
@ -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 (
|
||||||
|
<div style={defstyle}>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (
|
||||||
|
<Card.Img variant='top' src={image} />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,73 +3,38 @@ import Col from 'react-bootstrap/Col';
|
||||||
import Container from "react-bootstrap/Container";
|
import Container from "react-bootstrap/Container";
|
||||||
import Card from "react-bootstrap/Card";
|
import Card from "react-bootstrap/Card";
|
||||||
|
|
||||||
import { ref, getDownloadURL } from "firebase/storage"
|
|
||||||
import { useState, useEffect, useContext } from "react"
|
import { useState, useEffect, useContext } from "react"
|
||||||
import { StorageContext } from '../contexts/StorageContext';
|
import { ItemImage } from "../components/ItemImage"
|
||||||
|
|
||||||
export function Home(props) {
|
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(() => {
|
useEffect(() => {
|
||||||
|
console.log( props.items )
|
||||||
|
setBooks( props.items )
|
||||||
|
}, [props.items])
|
||||||
|
|
||||||
|
|
||||||
}, [props.name, image])
|
|
||||||
const imgRef = ref(Storage, `book_covers/${props.name}`)
|
const ItemCards = books.map( ( book, itemkey ) => {
|
||||||
if (image) {
|
|
||||||
return(
|
return(
|
||||||
<Card.Img variant="top" src={image} />
|
<Col md={4}>
|
||||||
)
|
<Card key={itemkey}>
|
||||||
}
|
<ItemImage source={ book.cover_image} />
|
||||||
|
|
||||||
else {
|
|
||||||
return (
|
|
||||||
<p>Loading...</p>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!books) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// collection
|
|
||||||
const Items = books.map((item, key) => {
|
|
||||||
return (
|
|
||||||
<Col key={key} md="4">
|
|
||||||
<Card>
|
|
||||||
<Card.Body>
|
<Card.Body>
|
||||||
<ItemImage name={item.cover_image} />
|
<Card.Title>{ book.book_title }</Card.Title>
|
||||||
<Card.Title>{item.book_title}</Card.Title>
|
|
||||||
<Card.Text>
|
|
||||||
By {item.author}
|
|
||||||
</Card.Text>
|
|
||||||
<Card.Text>{item.cover_image}</Card.Text>
|
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
</Col>
|
</Col>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
<Row>
|
<Row>
|
||||||
{Items}
|
{ItemCards}
|
||||||
</Row>
|
</Row>
|
||||||
</Container>
|
</Container>
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue