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

Create Toy App #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,98 @@
let addToy = false;
const toyCollection = document.querySelector('#toy-collection')

const handleGETToys = () => {
fetch('http://localhost:3000/toys')
.then(res => res.json())
.then(handleFetchSuccess)
.catch(handleError)
}

const handleFetchSuccess = (data) => data.forEach(renderToyCard)

const createHTMLElement = (tag, className, textContent, src, id) => {
const element = document.createElement(tag)
if(className) element.className = className
if(textContent) element.textContent = textContent
if(src) element.src = src
if(id) element.id = id
return element
}

const renderToyCard = (toy) => {
const div = createHTMLElement('div', 'card')
div.appendChild(createHTMLElement('h2', null, toy.name))
div.appendChild(createHTMLElement('img', 'toy-avatar', null,toy.image))
div.appendChild(createHTMLElement('p', null, `${toy.likes} Likes`))
const button = createHTMLElement('button', 'like-btn', `Like ❤️`, null, `${toy.id}`)
button.style.cursor = 'pointer'
button.addEventListener('click', handlePATCHLikes)
div.appendChild(button)
toyCollection.appendChild(div)
}
Comment on lines +13 to +32

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the idea, but it's certainly confusing. Contrary to the saying, sometimes more is less. Sacrificing readability to make something shorter is always a tough dilemma, but I think in this case being more explicit is the way to go. But again, I love the creativity.


const handleError = () => alert('Something went wrong when fetching!')

const createToy = (e) => {
e.preventDefault()
const newToyName = e.target[0].value
const newToyImgURL = e.target[1].value
handlePOSTToys(newToyName, newToyImgURL)
e.target[0].value = ''
e.target[1].value = ''
Comment on lines +41 to +42

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
e.target[0].value = ''
e.target[1].value = ''
e.target.reset()

}

const handlePOSTToys = (newToyName, newToyImgURL) => {
fetch('http://localhost:3000/toys', {
method: 'POST',
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify({
"name" : newToyName,
"image" : newToyImgURL,
"likes" : 0
})
})
.then(res => res.json())
.then(data => handlePOSTSuccess(data))
.catch(handleError)
}

const handlePATCHLikes = (event) => {
let numOfLikes = parseInt(event.target.previousSibling.textContent.split(' ')[0])
numOfLikes += 1
const toyId = event.target.id
fetch(`http://localhost:3000/toys/${toyId}`,{
method: 'PATCH',
headers: {
"Content-Type": "application/json",
Accept: "application/json"
},
body: JSON.stringify({
'likes': numOfLikes
})
})
.then(res => res.json())
.then(data => updateLikes(data, event))
.catch(handleError)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}

const updateLikes = (toyData,e) => e.target.previousSibling.textContent = `${toyData.likes} Likes`

const handlePOSTSuccess = (toyData) => renderToyCard(toyData)

const initApp = () => {
handleGETToys()
}

document.addEventListener("DOMContentLoaded", () => {
initApp()
const addBtn = document.querySelector("#new-toy-btn");
const toyFormContainer = document.querySelector(".container");
const toyForm = document.querySelector(".add-toy-form");
toyForm.addEventListener('submit', createToy)
addBtn.addEventListener("click", () => {
// hide & seek with the form
addToy = !addToy;
Expand Down