Skip to content

Commit 761c169

Browse files
homehome
authored andcommitted
inifite scroll working
1 parent 9ca2bfb commit 761c169

File tree

11 files changed

+439
-73
lines changed

11 files changed

+439
-73
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ npm-debug.log*
55
yarn-debug.log*
66
yarn-error.log*
77

8+
89
# Runtime data
910
pids
1011
*.pid
@@ -35,6 +36,7 @@ build/Release
3536
# Dependency directories
3637
node_modules/
3738
jspm_packages/
39+
src/lambda
3840

3941
# Typescript v1 declaration files
4042
typings/

functions/fetch-norm.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/fetch.js

Lines changed: 325 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

netlify.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build]
2+
functions = "functions"

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
"gatsby-source-filesystem": "^2.1.5",
1919
"gatsby-transformer-sharp": "^2.2.4",
2020
"netlify-lambda": "^1.5.0",
21+
"node-fetch": "^2.6.0",
2122
"prop-types": "^15.7.2",
2223
"react": "^16.8.6",
2324
"react-dom": "^16.8.6",
2425
"react-helmet": "^5.2.1",
25-
"react-infinite-scroll-component": "^4.5.2"
26+
"react-infinite-scroll-component": "^4.5.2",
27+
"unsplash-js": "^5.0.0"
2628
},
2729
"devDependencies": {
2830
"prettier": "^1.18.2"

src/components/InfiniteImages.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { useState, useEffect } from "react"
2+
import axios from "axios"
3+
import InfiniteScroll from "react-infinite-scroll-component"
4+
import "./gallery.css"
5+
6+
const ImageGallery = ({ images, loading, fetchImages }) => {
7+
// Create gallery here
8+
return (
9+
<InfiniteScroll
10+
dataLength={images.length}
11+
next={() => fetchImages(10)}
12+
hasMore={true}
13+
loader={
14+
<p style={{ textAlign: "center", marginTop: "1%" }}>
15+
More doggo incoming 🐕 🐕...
16+
</p>
17+
}
18+
>
19+
<div className="image-grid">
20+
{!loading
21+
? images.map((image, index) => (
22+
<div className="image-item" key={index}>
23+
<img src={image.urls.regular} />
24+
</div>
25+
))
26+
: ""}
27+
</div>
28+
</InfiniteScroll>
29+
)
30+
}
31+
32+
const InfiniteImages = () => {
33+
// Hold state
34+
const [images, setImages] = useState([])
35+
const [loading, setLoading] = useState(true)
36+
37+
// Fetch images on component mount
38+
useEffect(() => {
39+
fetchImages()
40+
}, [])
41+
42+
// Fetch Images
43+
const fetchImages = (count = 10) => {
44+
const apiRoot = "https://api.unsplash.com"
45+
const accessKey =
46+
// "a5a0aed7b2bc3fba7e79cdd18b934925ba51540a80a4aefd7d8e851d73235a22"
47+
"45257f5a9499990c70f7dfc8b11909d57f88b0c45cf88bf3331631c453bdcf90"
48+
// "8a4eb76e94d02735696730edac2a8e0fc8f6e4d8e6064fd4add3006183e6e372"
49+
50+
const doggoEndpoint = `${apiRoot}/photos/random?client_id=${accessKey}&count=${count}&collections='3816141,1154337,1254279'`
51+
52+
axios.get(doggoEndpoint).then(res => {
53+
console.log(images.length)
54+
setImages([...images, ...res.data])
55+
setLoading(false)
56+
})
57+
}
58+
return (
59+
<ImageGallery images={images} loading={loading} fetchImages={fetchImages} />
60+
)
61+
}
62+
63+
export default InfiniteImages
File renamed without changes.

src/components/header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Header = ({ siteTitle }) => (
1414
}}
1515
className="has-text-white is-size-3"
1616
>
17-
{siteTitle}
17+
{siteTitle} 🐶
1818
</Link>
1919
</div>
2020
<div className="navbar-end" style={{ marginRight: "3em" }}>

src/pages/gallery.js

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,9 @@ import React, { useState, useEffect } from "react"
22

33
import Layout from "../components/layout"
44
import SEO from "../components/seo"
5-
import axios from "axios"
6-
import InfiniteScroll from "react-infinite-scroll-component"
7-
import "./gallery.css"
8-
9-
const ImageGallery = ({ images, loading, fetchImages }) => {
10-
// Create gallery here
11-
return (
12-
<InfiniteScroll
13-
dataLength={images.length}
14-
next={fetchImages(5)}
15-
hasMore={true}
16-
loader={
17-
<p style={{ textAlign: "center", marginTop: "1%" }}>
18-
More doggo incoming 🐕 🐕...
19-
</p>
20-
}
21-
>
22-
<div className="image-grid">
23-
{!loading
24-
? images.map((image, index) => (
25-
<div className="image-item" key={index}>
26-
<img src={image.urls.regular} />
27-
</div>
28-
))
29-
: ""}
30-
</div>
31-
</InfiniteScroll>
32-
)
33-
}
34-
35-
const Page = () => {
36-
// Hold state
37-
const [images, setImages] = useState([])
38-
const [loading, setLoading] = useState(true)
39-
40-
// Fetch images on page load
41-
useEffect(() => {
42-
fetchImages()
43-
}, [])
44-
45-
// Fetch Images
46-
const fetchImages = (count = 10) => {
47-
const apiRoot = "https://api.unsplash.com"
48-
const accessKey =
49-
// "a5a0aed7b2bc3fba7e79cdd18b934925ba51540a80a4aefd7d8e851d73235a22"
50-
// "1bf0ee2ba4572a36e6c0bb2ce87ab3f6bc7510fd8676791a0be3d7dbfd2c0acd"
51-
"8a4eb76e94d02735696730edac2a8e0fc8f6e4d8e6064fd4add3006183e6e372"
52-
53-
const dogEndpoint = `${apiRoot}/photos/random?client_id=${accessKey}&count=${count}&collections='3816141,1154337,1254279'`
54-
const normalEndpoint = `${apiRoot}/photos/random?client_id=${accessKey}&count=${count}`
55-
56-
axios.get(dogEndpoint).then(res => {
57-
setImages([...images, ...res.data])
58-
setLoading(false)
59-
})
60-
}
5+
import InfiniteImages from "../components/InfiniteImages"
616

7+
const Gallery = () => {
628
return (
639
<Layout>
6410
<SEO title="Gallery" />
@@ -67,13 +13,9 @@ const Page = () => {
6713
Now this is the Law of the Jungle, as old and true as the sky, for as
6814
long as you keep scrolling, you shall find more doggo images 🐶 🐕.
6915
</p>
70-
<ImageGallery
71-
images={images}
72-
loading={loading}
73-
fetchImages={fetchImages}
74-
/>
16+
<InfiniteImages />
7517
</Layout>
7618
)
7719
}
7820

79-
export default Page
21+
export default Gallery

src/pages/index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import React from "react"
22
import { Link } from "gatsby"
3-
43
import Layout from "../components/layout"
5-
// import Image from "../components/image"
64
import SEO from "../components/seo"
75
import "bulma/css/bulma.min.css"
86

@@ -12,19 +10,15 @@ const IndexPage = () => (
1210
<div className="has-text-centered" style={{ marginTop: "20%" }}>
1311
<h1 className="is-size-2">Welcome to Pride Rock! . . . or nah 😹</h1>
1412
<p className="is-size-5" style={{ marginTop: "2%" }}>
15-
Find within, an image gallery built with Gatsby and Images served using
16-
Netlify functions from Unsplash. Perfecto!
13+
Find within, a fire doggo infinite image gallery built with Gatsby, and
14+
Images served using Netlify functions from Unsplash. Perfecto!
1715
</p>
1816
<button className="button is-dark is-large" style={{ marginTop: "10%" }}>
1917
<Link to="/gallery/" className="has-text-white">
2018
Open Sesame! 🔥
2119
</Link>
2220
</button>
2321
</div>
24-
25-
{/* <div style={{ maxWidth: `300px`, marginBottom: `1.45rem` }}>
26-
<Image />
27-
</div> */}
2822
</Layout>
2923
)
3024

0 commit comments

Comments
 (0)