|
| 1 | +import { Component } from 'react'; |
1 | 2 | import { Container, Col, Row, Image } from 'react-bootstrap';
|
| 3 | +import { ImageModal } from './Modal'; |
2 | 4 |
|
3 |
| -export const ImageGallery = ({ images }) => ( |
4 |
| - <Container className="justify-content-center"> |
5 |
| - <Row xs={1} sm={2} md={3} lg={4} className="g-4"> |
6 |
| - <ImageGalleryItem images={images} /> |
7 |
| - </Row> |
8 |
| - </Container> |
9 |
| -); |
10 |
| - |
11 |
| -const ImageGalleryItem = ({ images }) => ( |
12 |
| - <> |
13 |
| - {images.map(({ id, webformatURL, largeImageURL, tags }) => ( |
14 |
| - <Col key={id}> |
| 5 | +class ImageGallery extends Component { |
| 6 | + constructor(props) { |
| 7 | + super(props); |
| 8 | + |
| 9 | + this.state = { |
| 10 | + showModal: false, |
| 11 | + selectedImage: null, |
| 12 | + }; |
| 13 | + } |
| 14 | + |
| 15 | + openModal = image => { |
| 16 | + this.setState({ |
| 17 | + showModal: true, |
| 18 | + selectedImage: image, |
| 19 | + }); |
| 20 | + }; |
| 21 | + |
| 22 | + closeModal = () => { |
| 23 | + this.setState({ |
| 24 | + showModal: false, |
| 25 | + selectedImage: null, |
| 26 | + }); |
| 27 | + }; |
| 28 | + |
| 29 | + render() { |
| 30 | + const { images } = this.props; |
| 31 | + const { showModal, selectedImage } = this.state; |
| 32 | + |
| 33 | + return ( |
| 34 | + <Container className="justify-content-center"> |
| 35 | + <Row xs={1} sm={2} md={3} lg={4} className="g-4"> |
| 36 | + {images.map( |
| 37 | + ({ id, webformatURL, largeImageURL, tags, user, userImageURL }) => ( |
| 38 | + <ImageGallery.Item |
| 39 | + key={id} |
| 40 | + id={id} |
| 41 | + webformatURL={webformatURL} |
| 42 | + largeImageURL={largeImageURL} |
| 43 | + tags={tags} |
| 44 | + user={user} |
| 45 | + userImageURL={userImageURL} |
| 46 | + openModal={this.openModal} |
| 47 | + /> |
| 48 | + ) |
| 49 | + )} |
| 50 | + </Row> |
| 51 | + |
| 52 | + <ImageModal |
| 53 | + showModal={showModal} |
| 54 | + selectedImage={selectedImage} |
| 55 | + closeModal={this.closeModal} |
| 56 | + /> |
| 57 | + </Container> |
| 58 | + ); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class ImageGalleryItem extends Component { |
| 63 | + render() { |
| 64 | + const { id, largeImageURL, tags, openModal, user, userImageURL } = |
| 65 | + this.props; |
| 66 | + |
| 67 | + return ( |
| 68 | + <Col |
| 69 | + key={id} |
| 70 | + onClick={() => |
| 71 | + openModal({ id, largeImageURL, tags, user, userImageURL }) |
| 72 | + } |
| 73 | + > |
15 | 74 | <Image
|
16 |
| - src={webformatURL} |
| 75 | + src={largeImageURL} |
17 | 76 | alt={tags}
|
| 77 | + role="button" |
18 | 78 | rounded
|
19 | 79 | style={{ objectFit: 'cover', height: '100%', width: '100%' }}
|
20 | 80 | />
|
21 | 81 | </Col>
|
22 |
| - ))} |
23 |
| - </> |
24 |
| -); |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
25 | 85 |
|
26 | 86 | ImageGallery.Item = ImageGalleryItem;
|
| 87 | + |
| 88 | +export default ImageGallery; |
0 commit comments