-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import React from 'react'
import ReactDOM from 'react-dom'
const { useState, useEffect, useRef } = React;
const clientID = "t-FQWYk2PUt13LidWIblzu7SNd9HVOQsK3QA7Lg1Mg4";
const utm = "?utm_source=scrimba_degree&utm_medium=referral"
var API_KEY = 'NpRvp4rxQt7jYkbu95fWvCMrZKxyQKlWcNZfzeopGfI';
const loadData = (options) => {
fetch(options.url)
.then(function(response){
return response.json()
})
.then(function(data){
if (options.onSuccess) options.onSuccess(data)
})
}
const App = (props) => {
let [photos, setPhotos] = useState([]);
// CHALLENGE:
// Change the query to one of your interests
let [query, setQuery] = useState("vampires");
const queryInput = useRef(null);
const numberOfPhotos = 50;
const url =
"https://api.unsplash.com/photos/random/?count=" +
numberOfPhotos +
"&client_id=" +
clientID;
useEffect(() => {
const photosUrl = query ? `${url}&query=${query}` : url;
loadData({
url: photosUrl,
onSuccess: res => {
setPhotos(res);
}
});
}, [query, url]);
const searchPhotos = e => {
e.preventDefault();
setQuery(queryInput.current.value);
};
return (
<div className="box">
<h2>{props.emoji}</h2>
<h1>{props.name}'s website</h1>
<div className="grid">
{ query ?
photos.map(photo => {
return (
<div key={photo.id} className="item">
<img
className="img"
src={photo.urls.regular}
/>
<div className="caption">
<span className="credits">Photo by
<a href={photo.user.links.html + utm}> {photo.user.name}
</a>
<span> on </span>
<a href={"https://unsplash.com" + utm}>
Unsplash
</a>
</span>
</div>
</div>
);
}) : ""}
</div>
<footer>Made with ❤️ by <a href="https://github.com/ajay-dhangar" target="_blank">Ajay-Dhangar</a></footer>
</div>
);
};
// CHALLENGE: add your own name and emoji to the website
ReactDOM.render(<App name="Ajay Dhangar" emoji="👨💻"/>, document.getElementById("root"));