Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mm 49 large image #22

Open
wants to merge 12 commits into
base: staging
Choose a base branch
from
14 changes: 14 additions & 0 deletions src/components/photoGallery/LargeImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";

interface LargeImageProps{
url: string;
title: string;
}

export const LargeImage: React.FunctionComponent<LargeImageProps> = ({ url, title}) => {
return (
<div>
<img className="large-picture" src={url} alt={title} width="90%" />
</div>
);
}
12 changes: 11 additions & 1 deletion src/components/photoGallery/PhotoGallery.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.image-container {
display: flex;
flex-direction: column;
}

.image-gallery {
display: flex;
flex-wrap: wrap;
Expand All @@ -9,5 +14,10 @@
.space-image {
width: 200px;
height: 200px;
object-fit: cover;
object-fit: cover;
}

.large-picture {
width: 90%;
padding: 5%;
}
37 changes: 25 additions & 12 deletions src/components/photoGallery/PhotoGallery.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState, useEffect } from "react";
import "./PhotoGallery.scss";
import { LargeImage } from "./LargeImage";

interface Apod {
export interface Apod {
date: string;
explanation: string;
hdurl: string;
Expand All @@ -11,28 +12,40 @@ interface Apod {
url: string;
}

const firstLargeImage: Apod = {date:"2013-07-13",
explanation:"Reddened rays of the setting Sun flooded the skies over Cedar Creek Lake",
hdurl:"https://apod.nasa.gov/apod/image/1307/Sunspot-06July2013-johunter.jpg",
media_type:"image",
service_version:"v1",
title:"Sunspot at Sunset",
url:"https://apod.nasa.gov/apod/image/1307/Sunspot-06July2013-johunter950.jpg"}

export const PhotoGallery: React.FunctionComponent = () => {
const [imageUrls, setImageUrls] = useState<string[]>();
const [imageData, setImageData] = useState<Apod[]>();
const [largeImage, setLargeImage] = useState<Apod>(firstLargeImage);

useEffect(() => {
const url = `https://api.nasa.gov/planetary/apod?count=50&api_key=DEMO_KEY`;

const fetchData = async () => {
const response = await fetch(url);
const json: Apod[] = await response.json();
setImageUrls(json.map((item) => item.url));
const response = await fetch(url);
const json: Apod[] = await response.json();
setImageData(json)
};

fetchData();
}, []);

return (
<div className="image-gallery">
{imageUrls === undefined ? (
<p>Loading....</p>
) : (
imageUrls.map((url) => <img className="space-image" src={url} alt="" />)
)}
<div className="image-container">
<LargeImage url={largeImage.url} title={largeImage.title} />
<div className="image-gallery">
{
imageData === undefined
? <p>Loading....</p>
: imageData.map(image => <img
onClick={() => { setLargeImage(image); window.scrollTo(0, 0) }}
className="space-image" src={image.url} alt={image.title} />)}
</div>
</div>
);
};