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

JS 4: Task 2. Replaces API tutorial with a fetch() tutorial using trivia api #453

Open
wants to merge 6 commits into
base: gh-pages
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
24 changes: 24 additions & 0 deletions examples/trivia-quiz/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="styles.css">
<script src="index.js"></script>
<title>codebar.io - Trivia!</title>
</head>
<body>
<header>
<img id="logo" src='https://raw.github.com/codebar/planner/master/app/assets/images/logo.png' width='200' />
</header>
<div id="container">
<div id="selector-container">
<h2>Select Difficulty:</h2>
</div>
<h1>Questions</h1>
<div id="questions">
</div>
</div>
<footer>
<div id="content"> by <a href="http://codebar.io">codebar</a> </div>
</footer>
</body>
</html>
78 changes: 78 additions & 0 deletions examples/trivia-quiz/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// As fetch() is an asynchronous function we can add the async/await keywords
async function fetchQuizQuestions(difficulty) {
return await fetch(`https://opentdb.com/api.php?amount=10&difficulty=${difficulty}`)
.then(response => {
return response.json()
})
.then(data => {
renderQuizQuestions(data)
})
.catch(error => {
console.error('Error: ', error)
})
}

function renderQuizQuestions(data) {
const container = document.getElementById('questions')
//when new data is fetched we want to remove the pre-existing list
container.removeChild(container.firstChild)

const list = document.createElement('ul')
container.appendChild(list)

data.results.map((questionObject) => {
const listElement = document.createElement('li')
const text = document.createTextNode(questionObject.question)
listElement.appendChild(text)
list.appendChild(listElement)
})
}

// Bonus part of the tutorial functions
function createSelector() {
const difficulties = ['easy', 'medium', 'hard']

const selectorContainer = document.getElementById('selector-container')
const selector = document.createElement('select')
selector.id = 'selector'

difficulties.map((difficulty) => {
const option = new Option(difficulty, difficulty)
selector.appendChild(option)
})
selectorContainer.appendChild(selector)
}

function selectorChangeListener() {
const selector = document.getElementById('selector')

selector.addEventListener('change', event => {
const difficulty = event.target.value.toLowerCase()

fetchQuizQuestions(difficulty)
})
}
// // //

function renderApp() {
createSelector()
// set to easy to load the page with
fetchQuizQuestions('easy')
selectorChangeListener()
}

document.addEventListener('readystatechange', event => {
const readyState = event.target.readyState
const container = document.getElementById('questions')

if (readyState === 'interactive') {
const image = new Image(100, 100)
image.src = 'spinner.gif'

container.appendChild(image)
} else if (readyState === 'complete') {
//remove spinner when document is ready
container.removeChild(container.firstChild)
renderApp()
}
})
File renamed without changes
40 changes: 40 additions & 0 deletions examples/trivia-quiz/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
body {
font-family: Helvetica, Arial, sans-serif;
border-top: 5px solid #4c4066;
margin: 0px auto;
font-size: 16px;
}

header {
text-align: center;
}

li {
padding-bottom: 10px;
}

#selector {
font-size: 16px;
}

#questions {
max-width: 750px;
}

#logo {
position: relative;
top: 2px;
}

#container {
width: 1040px;
padding-left: 8px;
min-height: 750px;
}

footer {
padding-bottom: 10px;
border-bottom: 5px solid #4c4066;
position: absolute;
width: 100%;
}
23 changes: 0 additions & 23 deletions examples/tv-schedule/index.html

This file was deleted.

114 changes: 0 additions & 114 deletions examples/tv-schedule/script.js

This file was deleted.

110 changes: 0 additions & 110 deletions examples/tv-schedule/style.css

This file was deleted.

Loading