From 9fe3986006287045c69aafb28d11708607498ff4 Mon Sep 17 00:00:00 2001 From: Mina Date: Thu, 22 Feb 2018 10:30:24 +0000 Subject: [PATCH] fixes up fetch promises. runs code formatter. --- .gitignore | 2 ++ public/script.js | 71 ++++++++++++++++++++++-------------------------- 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 5148e52..d0bd963 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ jspm_packages # Optional REPL history .node_repl_history + +.prettierrc \ No newline at end of file diff --git a/public/script.js b/public/script.js index b677186..3056dd7 100644 --- a/public/script.js +++ b/public/script.js @@ -1,16 +1,15 @@ -if (document.readyState !== 'loading') { +if (document.readyState !== "loading") { ready(); } else { - document.addEventListener('DOMContentLoaded', ready); + document.addEventListener("DOMContentLoaded", ready); } -function ready () { - getBlogposts('/get-posts'); +function ready() { + getBlogposts("/get-posts"); // send posts to server - var form = document.querySelector('form'); - form.addEventListener('submit', function (event) { - + var form = document.querySelector("form"); + form.addEventListener("submit", function(event) { event.preventDefault(); // prevents the form from contacting our server automatically (we want to do it ourselves) var formActionUrl = form.action; // 'form.action' is the url '/create-post' var formData = new FormData(form); @@ -22,48 +21,42 @@ function ready () { /**** * Function definitions ***/ -function postBlogposts (url, data) { - fetch(url, { - method: 'POST', - body: data - }) - .then(function (res) { - res.json() - .then(function (json) { - console.log(json); - addBlogpostsToPage(json); - document.querySelector('form').reset(); +function postBlogposts(url, data) { + fetch(url, { method: "POST", body: data }) + .then(function(res) { + res.json(); }) - }) - .catch(function (err) { - console.error(err) - }); + .then(function(json) { + console.log(json); + addBlogpostsToPage(json); + document.querySelector("form").reset(); + }) + .catch(function(err) { + console.error(err); + }); } -function getBlogposts (url) { - fetch(url, { - method: 'GET' - }) - .then(function (res) { - res.json() - .then(function (json) { +function getBlogposts(url) { + fetch(url) + .then(function(res) { + res.json(); + }) + .then(function(json) { console.log(json); addBlogpostsToPage(json); + }) + .catch(function(err) { + console.error(err); }); - }) - .catch(function (err) { - console.error(err) - }); } -function addBlogpostsToPage (data) { +function addBlogpostsToPage(data) { for (var blogpost in data) { if (data.hasOwnProperty(blogpost)) { - - var postDiv = document.createElement('div'); - var postText = document.createElement('p'); - var thumbnail = document.createElement('img'); - var postContainer = document.querySelector('.post-container'); + var postDiv = document.createElement("div"); + var postText = document.createElement("p"); + var thumbnail = document.createElement("img"); + var postContainer = document.querySelector(".post-container"); thumbnail.src = "./img/logo2.png"; thumbnail.className = "thumbnail";