Slideshow that calls the Reddit API using fetch w/ javascript. Returns images found using the search query and displays them as a slideshow gallery.
- Go to repo on Github profile.
fork
+clone
.- Clone to local.
git clone https://github.com/ruvvet/fetch-reddit-slideshow.git
- Find
tic-tac-toe
directory. - Open
index.html
in browser.
open index.html
- Done.
const getPosts = (searchQuery) => {
if (!searchQuery) {
searchQuery = 'meme';
}
const fetchQuery = `http://www.reddit.com/search.json?q=${searchQuery}+nsfw:no`;
console.log(fetchQuery);
fetch(fetchQuery)
.then((response) => {
if (response.ok) {
return response.json();
}
})
.then((data) => {
console.log(data);
// use reduce to find only images
// if post_hint = image, it is an image, add to list
const images = data.data.children.reduce((acc, child) => {
if (child.data.post_hint == 'image') {
acc.push(child.data.url);
}
return acc;
}, []);
//returns a bunch of urls as an array
console.log(images);
// call create slides function to set DOM stuff
createSlideShow(images);
})
.catch((e) => {
console.error('Error: ', e);
});
};