Skip to content

Instantly share code, notes, and snippets.

@artismarti
Created December 20, 2018 22:10
Show Gist options
  • Save artismarti/f50aca3dfee0fd91cb803dc0e12b5139 to your computer and use it in GitHub Desktop.
Save artismarti/f50aca3dfee0fd91cb803dc0e12b5139 to your computer and use it in GitHub Desktop.
fetch functions
// GET
fetch(quotesURL)
.then(response => response.json())
.then((quotes) => renderAllQuotes(quotes))
}
// PATCH
fetch(`${quotesURL}/${quote.id}`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
quote: pTag.innerText
})
})
// POST
fetch(quotesURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
quote: newQuoteText.value,
likes: 0,
author: newQuoteAuthor.value
})
})
.then(response => response.json())
.then((quote) => {
renderSingleQuote(quote)
newQuoteForm.reset()
})
// DELETE
fetch(`${quotesURL}/${quote.id}`, {
method: 'DELETE'
})
let deleteQuoteLi = quoteListUl.querySelector(`li[data-quote-id="${quote.id}"]`)
deleteQuoteLi.remove()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment