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

fixes up fetch promises. runs code formatter. #38

Open
wants to merge 1 commit into
base: master
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ jspm_packages

# Optional REPL history
.node_repl_history

.prettierrc
Copy link
Member

Choose a reason for hiding this comment

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

Feels a bit redundant to have this in the .gitignore

71 changes: 32 additions & 39 deletions public/script.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

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

Might as well remove the console.log here

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";
Expand Down