Skip to content

Commit

Permalink
Merge pull request #59 from fac-13/domMan
Browse files Browse the repository at this point in the history
Dom man
  • Loading branch information
isnotafunction authored Apr 12, 2018
2 parents e073db2 + 2d0861d commit 5a2f685
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 57 deletions.
25 changes: 19 additions & 6 deletions public/dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(function() {
var errorMessage = document.getElementById('submitError');
// FORM VALIDATION
// login form
var login__form = document.getElementsByTagName('form')[1];
Expand Down Expand Up @@ -55,7 +56,7 @@
event.preventDefault();
}
if (reg__password.value != reg__confirmpassword.value) {
error.innerText = 'Passwords do not match';
reg__error.innerText = 'Passwords do not match';
event.preventDefault();
}

Expand All @@ -78,14 +79,19 @@
}
};

utility.fetch('/get/topics', function(err, res) {
if (err) console.log(err);
renderFunc(res);
});
var displayError = function() {
errorMessage.classList.add('is-hidden');
if (document.cookie !== 'message=OK' && document.cookie) {
errorMessage.textContent = document.cookie.split('=')[1];
errorMessage.classList.remove('is-hidden');
}
};

errorMessage.classList.add('is-hidden');

var renderFunc = function(res) {
clear(topicResults);

displayError();
res.reverse();
res.forEach(function(obj) {
var topicResult = document.createElement('div');
Expand All @@ -102,6 +108,8 @@
var topicVote = document.createElement('div');
topicVote.classList.add('vote');

var errorMessage = document.getElementById('submitError');

//radio form
var radioForm = `<form method='POST' action='/?end=create-vote&topic=${
obj.id
Expand Down Expand Up @@ -145,6 +153,11 @@
topicResults.appendChild(topicResult);
});
};

utility.fetch('/get/topics', function(err, res) {
if (err) console.log(err);
renderFunc(res);
});
})();

// -- CALLBACK FUNCTIONS
Expand Down
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</head>

<body>
<span id="submitError" class="submitError is-hidden">OK</span>
<header>
<h1>Welcome!</h1>
</header>
Expand Down
18 changes: 17 additions & 1 deletion public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ main {
border: 3px solid #fff;
border-radius: 10px;
margin: 5px;
}
}

/* / Submit error / */
.submitError {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: white;
color: black;
text-align: center;
padding: 2rem 1rem;
}

.is-hidden {
display: none;
}
102 changes: 52 additions & 50 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const jwtmodule = require('jsonwebtoken');
const addErrorCookie = (response, errormessage) => {
response.writeHead(302, {
'Content-Type': 'text/plain',
'Set-Cookie': `error=${errormessage}`,
'Set-Cookie': `message=${errormessage}`,
Location: '/'
});
response.end();
Expand All @@ -36,10 +36,14 @@ const staticHandler = (response, filepath) => {

fs.readFile(path.join(__dirname, '..', filepath), 'utf8', (error, file) => {
if (error) {
response.writeHead(500, { 'content-type': 'text/plain' });
response.writeHead(500, {
'content-type': 'text/plain'
});
response.end('server error');
} else {
response.writeHead(200, { 'content-type': extensionType[extension] });
response.writeHead(200, {
'content-type': extensionType[extension]
});
response.end(file);
}
});
Expand All @@ -50,11 +54,13 @@ const getDataHandler = response => {
getData((err, res) => {
if (err) {
console.log('ERROR AT GET DATA HANDLER');
response.writeHead(500, { 'content-type': 'text/plain' });
response.end('server error');
addErrorCookie(response, 'sorry there was a server error');
} else {
let output = JSON.stringify(res);
response.writeHead(200, { 'content-type': 'application/json' });
response.writeHead(200, {
'content-type': 'application/json',
'Set-Cookie': 'message=OK'
});
response.end(output);
}
});
Expand Down Expand Up @@ -82,11 +88,15 @@ const postTopicHandler = (request, response) => {
const description = data.description;
postTopic(topic_title, description, (err, res) => {
if (err) {
console.log(err);
response.writeHead(500, { 'content-type': 'text/plain' });
response.end('Something went wrong');
addErrorCookie(
response,
'sorry you cannot vote twice, it is undemocratic'
);
} else {
response.writeHead(303, { Location: '/' });
response.writeHead(303, {
Location: '/',
'Set-Cookie': 'message=OK'
});
response.end(`Successfully added ${topic_title}`);
}
});
Expand All @@ -101,9 +111,10 @@ const postVoteHandler = (request, response) => {
let user_id = params.user;
let body = '';
request.on('data', chunk => (body += chunk));

request.on('end', () => {
const data = querystring.parse(body);

const userCookie = request.headers.cookie;
if (!userCookie) return addErrorCookie(response, 'need to log in');
const { jwt } = cookie.parse(userCookie);
if (!jwt) return addErrorCookie(response, 'need to log in');
Expand All @@ -114,28 +125,23 @@ const postVoteHandler = (request, response) => {
'error valiating you, clear cache and login again'
);
} else {
let params = querystring.parse(request.url);
let topic_id = params.topic;
let user_id = params.user;
let body = '';
request.on('data', chunk => (body += chunk));
request.on('end', () => {
const data = querystring.parse(body);

let vote_value = data.vote;
postVote(topic_id, user_id, vote_value, (err, res) => {
if (err) {
console.log(err);
response.writeHead(500, { 'content-type': 'text/plain' });
response.end('Something went wrong');
} else {
response.writeHead(303, {
Location: '/',
'content-type': 'text/plain'
});
response.end(`Successfully added ${vote_value}`);
}
});
const data = querystring.parse(body);
let vote_value = data.vote;
postVote(topic_id, user_id, vote_value, (err, res) => {
if (err) {
console.log(err);
addErrorCookie(
response,
'sorry there was a problem posting your vote'
);
} else {
response.writeHead(303, {
Location: '/',
'content-type': 'text/plain',
'Set-Cookie': 'message=OK'
});
response.end(`Successfully added ${vote_value}`);
}
});
}
});
Expand All @@ -159,21 +165,18 @@ const loginHandler = (request, response) => {
const jwtCookie = jwtmodule.sign(userInfo, secret);
response.writeHead(302, {
location: '/',
'Set-Cookie': `jwt=${jwtCookie}; HttpOnly; Max-Age=90000`
'Set-Cookie': [
`jwt=${jwtCookie}; HttpOnly; Max-Age=90000`,
'message=you are now logged in'
]
});
response.end();
} else {
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end('password doesnt match db');
addErrorCookie(response, 'sorry your password dont match');
}
});
} else {
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end('user doesnt exist');
addErrorCookie(response, 'sorry the user doesnt exist');
}
});
});
Expand All @@ -189,10 +192,10 @@ const postUserHandler = (request, response) => {

getUserData(username, (err, res) => {
if (!res) {
response.writeHead(200, {
'content-type': 'text/plain'
});
response.end('username already taken');
addErrorCookie(
response,
'bad luck, that username is already taken, pick another!'
);
} else {
bcrypt.hash(password, 8, (err, hashedPassword) => {
if (err) {
Expand All @@ -201,13 +204,12 @@ const postUserHandler = (request, response) => {
postUser(username, hashedPassword, (err, res) => {
if (err) {
console.log(err);
response.writeHead(500, { 'content-type': 'text/plain' });
response.end('Something went wrong');
addErrorCookie(response, 'oopsy doodle there was a problemo');
} else {
console.log('posted user to db');
response.writeHead(303, {
Location: '/',
'content-type': 'text/plain'
'content-type': 'text/plain',
'Set-Cookie': 'message=thankyou for registering with us'
});
response.end(`Successfully added ${username}`);
}
Expand Down

0 comments on commit 5a2f685

Please sign in to comment.