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

Pao S fetch homework #253

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
body {
display: flex;
flex-direction: column;
background-color: grey;
justify-content: center;
}
.container {
display: flex;
justify-content: center;
height: 25rem;
}

.presearch {
display: flex;
flex-direction: column;
justify-content: center;

}

.text {
display: flex;
flex-direction: column;
align-items: center;
}

.search {
display: flex;
align-items: center;
justify-items: center;
padding: 0;
}


label {
display: none;
border: 2px solid red;
}
input {
width: 30rem;
height: 1.6rem;
text-align: center;
}

.searchButton {
height: 2rem;
width: 8rem;
align-items: center;
margin: 0
}

.resetButton {
height: 2rem;
width: 4rem;
align-self: center;
}
19 changes: 18 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/styles.css" rel="stylesheet">
<script src="js/scripts.js" defer></script>
<title>Hello Front-End</title>
</head>
<body>
<h1>Hello Front-End</h1>
<div id="imageContainer" class="container">
<div id="preSearch" class="presearch">
<div class="text">
<h1>Reddit Image Crawler</h1>
<p>Enter a search tearn and view images found on reddit related to your search</p>
</div>
<div id="searchField" class="search">
<form>
<label>search for images</label>
<input placeholder="enter your search term here" id="input"></input>
<button class="searchButton" id="searchBTN" type="submit">Search</button>
</form>
</div>
</div>
</div>
<button id="reset" class="resetButton">Reset</button>
</body>
</html>
53 changes: 53 additions & 0 deletions js/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

const input = document.querySelector('#input')
console.log(input)
const reset = document.querySelector('#reset')
console.log(reset)
const search = document.querySelector('#searchBTN')
console.log(search)
const presearch = document.querySelector('#preSearch')
console.log(presearch)
const imageContainer = document.querySelector('#imageContainer')
console.log(imageContainer)


search.addEventListener('click', function(e){
e.preventDefault()
console.log(input.value)
presearch.style.display = "none"
fetch(`https://www.reddit.com/search.json?q=${input.value}+nsfw:no`)
.then(function(returnedData) {
return returnedData.json()
})
.then(function(data){
let imageArray = data.data.children.map(child => child.data.url)
console.log(imageArray)
let images = document.createElement('img')
imageContainer.appendChild(images)
let interval = 0
let timer = setInterval(function(){
images.src = imageArray[interval]
interval++
}, 2000)
console.log(interval)
console.log(images.src)
images.src= imageArray[interval]
images.style.width = "600px"
images.style.objectFit = 'cover'
reset.addEventListener('click', function(){
clearInterval(timer)
presearch.style.display = "block"
imageContainer.removeChild(images)
input.value =''
})
})
.catch(console.warn)
})





//have in the setInterval have something to track the an integer and it increases the index 1 and it will increase

// src