From 758b6edbc119a0023f8a7475bfe907e7da711d50 Mon Sep 17 00:00:00 2001 From: Giulia Date: Sat, 7 Apr 2018 16:31:44 +0100 Subject: [PATCH 1/9] update file names, add logic.js, add profileHandler route --- public/logic.js | 17 ++++++++++++++ public/{userlist.html => user_list.html} | 2 +- public/{dom.js => user_list.js} | 23 ++----------------- ...rnewprofile.html => user_new_profile.html} | 0 .../{userprofile.html => user_profile.html} | 3 ++- public/user_profile.js | 0 src/handler.js | 9 ++++++-- src/router.js | 10 ++++---- 8 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 public/logic.js rename public/{userlist.html => user_list.html} (85%) rename public/{dom.js => user_list.js} (84%) rename public/{usernewprofile.html => user_new_profile.html} (100%) rename public/{userprofile.html => user_profile.html} (73%) create mode 100644 public/user_profile.js diff --git a/public/logic.js b/public/logic.js new file mode 100644 index 0000000..747c2f0 --- /dev/null +++ b/public/logic.js @@ -0,0 +1,17 @@ +// Generic Fetch Request +function fetch(url, callback) { + var xhr = new XMLHttpRequest(); + + xhr.addEventListener("load", function () { + if (xhr.readyState === 4 && xhr.status === 200) { + console.log("fetch is working", url); + var response = JSON.parse(xhr.responseText); + callback(response); + } else { + console.log("XHR error", xhr.readyState); + } + }); + xhr.open('GET', url, true); + xhr.send(); + } + \ No newline at end of file diff --git a/public/userlist.html b/public/user_list.html similarity index 85% rename from public/userlist.html rename to public/user_list.html index 727afbc..0d028ec 100644 --- a/public/userlist.html +++ b/public/user_list.html @@ -14,5 +14,5 @@

STACKMATCH USERS LIST

- + \ No newline at end of file diff --git a/public/dom.js b/public/user_list.js similarity index 84% rename from public/dom.js rename to public/user_list.js index 34b2742..0847ad8 100644 --- a/public/dom.js +++ b/public/user_list.js @@ -1,24 +1,8 @@ +import './logic.js'; + // Variables var usersList = document.getElementById('js-users-list'); -// Generic Fetch Request -function fetch(url, callback) { - var xhr = new XMLHttpRequest(); - - xhr.addEventListener("load", function () { - if (xhr.readyState === 4 && xhr.status === 200) { - console.log("fetch is working", url); - var response = JSON.parse(xhr.responseText); - callback(response); - } else { - console.log("XHR error", xhr.readyState); - } - }); - xhr.open('GET', url, true); - xhr.send(); -} - - // User List Population @@ -92,9 +76,6 @@ function populateUserList(userData) { } - - - // IFFE on load (function () { diff --git a/public/usernewprofile.html b/public/user_new_profile.html similarity index 100% rename from public/usernewprofile.html rename to public/user_new_profile.html diff --git a/public/userprofile.html b/public/user_profile.html similarity index 73% rename from public/userprofile.html rename to public/user_profile.html index cdd8ca4..47ba3e1 100644 --- a/public/userprofile.html +++ b/public/user_profile.html @@ -7,6 +7,7 @@ Document - +

User profile

+ \ No newline at end of file diff --git a/public/user_profile.js b/public/user_profile.js new file mode 100644 index 0000000..e69de29 diff --git a/src/handler.js b/src/handler.js index 8756627..ad7dde7 100644 --- a/src/handler.js +++ b/src/handler.js @@ -45,7 +45,7 @@ const listHandler = (req, res) => { }); }; -const profileHandler = (req, res) => { +const newProfileHandler = (req, res) => { console.log("Profile handler reached"); let body = ''; req.on('data', (chunk) => { @@ -72,5 +72,10 @@ const profileHandler = (req, res) => { } +const profileHandler = (req, res) => { + console.log("Profile hadler reached"); + +} + -module.exports = { staticHandler, listHandler, profileHandler }; +module.exports = { staticHandler, listHandler, newProfileHandler, profileHandler }; diff --git a/src/router.js b/src/router.js index bc9c1c1..f81d30c 100644 --- a/src/router.js +++ b/src/router.js @@ -1,4 +1,4 @@ -const { staticHandler, listHandler, profileHandler } = require('./handler'); +const { staticHandler, listHandler, newProfileHandler, profileHandler } = require('./handler'); const router = (req, res) => { const endpoint = req.url; @@ -9,13 +9,15 @@ const router = (req, res) => { else if (endpoint.indexOf('public') !== -1) { staticHandler(endpoint, res); } - else if (endpoint === '/userlist') { + else if (endpoint === '/user_list') { listHandler(endpoint, res); } - else if (endpoint === '/usernewprofile') { + else if (endpoint === '/user_new_profile') { + newProfileHandler(req, res); + } + else if (endpoint === '/user_profile'){ profileHandler(req, res); } - //TO DO: add paths: create or update profile else { res.writeHead(404, {'Content-Type': 'text/html'}); res.end('

404 page not found

'); From 558287d40c8ef8d8288a2c53dc046506310536c2 Mon Sep 17 00:00:00 2001 From: Giulia Date: Sun, 8 Apr 2018 15:19:18 +0100 Subject: [PATCH 2/9] add query to retrieve user ID in postProfileData --- public/user_list.html | 1 + public/user_list.js | 4 +--- public/user_new_profile.html | 35 +++++++++++++++++----------------- public/user_new_profile.js | 13 +++++++++++++ public/user_profile.html | 6 +++++- src/handler.js | 18 +++++++++-------- src/queries/postProfileData.js | 15 ++++++++++++--- src/router.js | 8 ++++---- 8 files changed, 63 insertions(+), 37 deletions(-) create mode 100644 public/user_new_profile.js diff --git a/public/user_list.html b/public/user_list.html index 0d028ec..2d8939e 100644 --- a/public/user_list.html +++ b/public/user_list.html @@ -14,5 +14,6 @@

STACKMATCH USERS LIST

+ \ No newline at end of file diff --git a/public/user_list.js b/public/user_list.js index 0847ad8..9a15fc4 100644 --- a/public/user_list.js +++ b/public/user_list.js @@ -1,5 +1,3 @@ -import './logic.js'; - // Variables var usersList = document.getElementById('js-users-list'); @@ -79,6 +77,6 @@ function populateUserList(userData) { // IFFE on load (function () { - fetch('/userlist', populateUserList); + fetch('/user_list', populateUserList); })(); \ No newline at end of file diff --git a/public/user_new_profile.html b/public/user_new_profile.html index 6a7e9ac..dcd4c79 100644 --- a/public/user_new_profile.html +++ b/public/user_new_profile.html @@ -1,33 +1,32 @@ + Document + -
+ Username: - - Name: - - Surname: - - Email: - - Cohort: - - City: - - Looking for work? + Name: + Surname: + Email: + Cohort: + City: + Looking for work? Yes - No - About me: + No About me: - -
- + + + + + + + \ No newline at end of file diff --git a/public/user_new_profile.js b/public/user_new_profile.js new file mode 100644 index 0000000..3e587d5 --- /dev/null +++ b/public/user_new_profile.js @@ -0,0 +1,13 @@ +const button = document.getElementById('form_button'); +const form = document.querySelector("form"); + +// function populateUserProfile (){ +// console.log('Populate user profile function reached'); +// }; + +// form.addEventListener('submit', function(){ +// fetch('/user_profile', populateUserProfile); +// }); + + + diff --git a/public/user_profile.html b/public/user_profile.html index 47ba3e1..83237c2 100644 --- a/public/user_profile.html +++ b/public/user_profile.html @@ -1,13 +1,17 @@ + Document +

User profile

- + + + \ No newline at end of file diff --git a/src/handler.js b/src/handler.js index ad7dde7..5981ac7 100644 --- a/src/handler.js +++ b/src/handler.js @@ -46,7 +46,7 @@ const listHandler = (req, res) => { }; const newProfileHandler = (req, res) => { - console.log("Profile handler reached"); + console.log("New profile handler reached"); let body = ''; req.on('data', (chunk) => { body+=chunk; @@ -63,19 +63,21 @@ const newProfileHandler = (req, res) => { res.end('

Sorry there was an error

'); } else { - res.writeHead(200, {'Content-Type': 'text/html'}); - res.end('Profile added to the database'); + // res.writeHead(200, {'Content-Type': 'text/html'}); + // res.end('Profile added to the database'); + console.log("Handler response:", response); + res.writeHead(303,{'Location': '/user_profile?handle=' + response}); + res.end(console.log('Successful relocation to new profile')); } - }) }) - } const profileHandler = (req, res) => { - console.log("Profile hadler reached"); - -} + console.log("Profile handler reached"); + + + } module.exports = { staticHandler, listHandler, newProfileHandler, profileHandler }; diff --git a/src/queries/postProfileData.js b/src/queries/postProfileData.js index d6eb023..706c754 100644 --- a/src/queries/postProfileData.js +++ b/src/queries/postProfileData.js @@ -7,13 +7,22 @@ const postProfileData = (values, cb)=> { values: values } - dbConnect.query(addNewUser, (err,res)=>{ + dbConnect.query(addNewUser, (err)=>{ + if (err){ + return cb(err); + } + + //get ID of new user + dbConnect.query("SELECT users.id FROM users WHERE handle ='" + values[0] +"'", (err,res) => { if (err){ return cb(err); } - cb(null, res); - console.log("Insert new data:", res); + let response = res.rows[0].id; + cb(null, response); + console.log("New user ID:", response); + }) }) + } module.exports = postProfileData; \ No newline at end of file diff --git a/src/router.js b/src/router.js index f81d30c..76dd20d 100644 --- a/src/router.js +++ b/src/router.js @@ -6,18 +6,18 @@ const router = (req, res) => { if (endpoint === '/') { staticHandler('public/index.html', res) } - else if (endpoint.indexOf('public') !== -1) { - staticHandler(endpoint, res); - } else if (endpoint === '/user_list') { listHandler(endpoint, res); } else if (endpoint === '/user_new_profile') { newProfileHandler(req, res); } - else if (endpoint === '/user_profile'){ + else if (endpoint.indexOf('/user_profile') !== -1){ profileHandler(req, res); } + else if (endpoint.indexOf('public') !== -1) { + staticHandler(endpoint, res); + } else { res.writeHead(404, {'Content-Type': 'text/html'}); res.end('

404 page not found

'); From 75c690f5c6fb0161ec49fa17266b85faa093434e Mon Sep 17 00:00:00 2001 From: Giulia Date: Sun, 8 Apr 2018 17:27:26 +0100 Subject: [PATCH 3/9] renamed usernewprofile as create_profile --- ...r_new_profile.html => create_profile.html} | 4 +- ...{user_new_profile.js => create_profile.js} | 0 public/index.html | 4 +- src/handler.js | 46 ++++++++++++------- src/router.js | 2 +- 5 files changed, 35 insertions(+), 21 deletions(-) rename public/{user_new_profile.html => create_profile.html} (89%) rename public/{user_new_profile.js => create_profile.js} (100%) diff --git a/public/user_new_profile.html b/public/create_profile.html similarity index 89% rename from public/user_new_profile.html rename to public/create_profile.html index dcd4c79..ed42938 100644 --- a/public/user_new_profile.html +++ b/public/create_profile.html @@ -9,7 +9,7 @@ -
+ Username: Name: Surname: @@ -25,7 +25,7 @@
- + diff --git a/public/user_new_profile.js b/public/create_profile.js similarity index 100% rename from public/user_new_profile.js rename to public/create_profile.js diff --git a/public/index.html b/public/index.html index 628a01d..bbb702a 100644 --- a/public/index.html +++ b/public/index.html @@ -7,7 +7,7 @@ Document -

ENTER STACKMATCH

+

ENTER STACKMATCH

+ - \ No newline at end of file diff --git a/src/handler.js b/src/handler.js index 5981ac7..44ff2b0 100644 --- a/src/handler.js +++ b/src/handler.js @@ -29,7 +29,7 @@ const staticHandler = (req, res) => { }; const listHandler = (req, res) => { - console.log('List handler reached' ); + console.log('List handler reached'); getListData((error, result) => { if (error) { res.writeHead(500, 'Content-Type:text/html'); @@ -46,38 +46,52 @@ const listHandler = (req, res) => { }; const newProfileHandler = (req, res) => { - console.log("New profile handler reached"); - let body = ''; + console.log("New profile handler reached"); + let body = ''; + req.on('data', (chunk) => { - body+=chunk; + body += chunk; }) + req.on('end', () => { - let values = querystring.parse(body); - let result = Object.values(values); + let values = querystring.parse(body); + let result = Object.values(values); result.pop();// Removes submit button 'submit' valuee from end. - - postProfileData(result, (error, response) => { + + postProfileData(result, (error, userId) => { if (error) { - console.log("Error:", error); - res.writeHead(500, {'Content-Type': 'text/html'}); + console.log("Error:", error); + res.writeHead(500, { 'Content-Type': 'text/html' }); res.end('

Sorry there was an error

'); } else { // res.writeHead(200, {'Content-Type': 'text/html'}); // res.end('Profile added to the database'); - console.log("Handler response:", response); - res.writeHead(303,{'Location': '/user_profile?handle=' + response}); - res.end(console.log('Successful relocation to new profile')); + res.writeHead(303, { 'Location': '/user_profile?id=' + userId }); + res.end(console.log('Successful relocation to new profile')); } }) }) } const profileHandler = (req, res) => { - console.log("Profile handler reached"); - + console.log("Profile handler reached"); + + getProfileData((error, result) => { + if (error) { + res.writeHead(500, 'Content-Type:text/html'); + res.end( + '

Sorry, there was a problem getting profile information

' + ); + console.log(error); + } else { + let profileData = JSON.stringify(result); + res.writeHead(200, { 'content-type': 'application/json' }); + res.end(profileData); + } + }); - } +} module.exports = { staticHandler, listHandler, newProfileHandler, profileHandler }; diff --git a/src/router.js b/src/router.js index 76dd20d..80c6447 100644 --- a/src/router.js +++ b/src/router.js @@ -9,7 +9,7 @@ const router = (req, res) => { else if (endpoint === '/user_list') { listHandler(endpoint, res); } - else if (endpoint === '/user_new_profile') { + else if (endpoint === '/create_profile') { newProfileHandler(req, res); } else if (endpoint.indexOf('/user_profile') !== -1){ From 9769b535fcbc5f78244333bbb284d0ce50410ab3 Mon Sep 17 00:00:00 2001 From: Giulia Date: Sun, 8 Apr 2018 20:27:33 +0100 Subject: [PATCH 4/9] add new route to display user data --- src/router.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/router.js b/src/router.js index 80c6447..9dee8f3 100644 --- a/src/router.js +++ b/src/router.js @@ -1,4 +1,4 @@ -const { staticHandler, listHandler, newProfileHandler, profileHandler } = require('./handler'); +const { staticHandler, listHandler, newProfileHandler, profileHandler, userDataHandler } = require('./handler'); const router = (req, res) => { const endpoint = req.url; @@ -12,8 +12,11 @@ const router = (req, res) => { else if (endpoint === '/create_profile') { newProfileHandler(req, res); } - else if (endpoint.indexOf('/user_profile') !== -1){ - profileHandler(req, res); + else if (endpoint.indexOf('/user_profile?id=') !== -1){ + profileHandler(endpoint, res); + } + else if (endpoint.indexOf('/user_data') !== -1){ + userDataHandler(endpoint, res); } else if (endpoint.indexOf('public') !== -1) { staticHandler(endpoint, res); From a633b4765dc056ab52470f559907ea97a51ba7f6 Mon Sep 17 00:00:00 2001 From: Giulia Date: Sun, 8 Apr 2018 20:28:22 +0100 Subject: [PATCH 5/9] add userDataHandler function --- src/handler.js | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/handler.js b/src/handler.js index 44ff2b0..05107a8 100644 --- a/src/handler.js +++ b/src/handler.js @@ -3,6 +3,7 @@ const fs = require('fs'); const querystring = require('querystring'); const getListData = require('./queries/getListData'); const postProfileData = require('./queries/postProfileData'); +const getProfileData = require('./queries/getProfileData'); const staticHandler = (req, res) => { @@ -20,7 +21,7 @@ const staticHandler = (req, res) => { path.join(__dirname, '..', req), 'utf8', (error, file) => { if (error) { res.writeHead(500, { 'content-type': 'text/plain' }); - res.end('server error'); + res.end('

Static file not found

'); } else { res.writeHead(200, { 'content-type': extensionType[extension] }); res.end(file); @@ -46,7 +47,6 @@ const listHandler = (req, res) => { }; const newProfileHandler = (req, res) => { - console.log("New profile handler reached"); let body = ''; req.on('data', (chunk) => { @@ -67,17 +67,34 @@ const newProfileHandler = (req, res) => { else { // res.writeHead(200, {'Content-Type': 'text/html'}); // res.end('Profile added to the database'); - res.writeHead(303, { 'Location': '/user_profile?id=' + userId }); - res.end(console.log('Successful relocation to new profile')); + res.writeHead(303, { 'Location': 'public/user_profile?id=' + userId }); + res.end(console.log('Successful redirection to new profile')); } }) }) } const profileHandler = (req, res) => { - console.log("Profile handler reached"); - getProfileData((error, result) => { + fs.readFile( + path.join(__dirname, '..', 'public', 'user_profile.html'), 'utf8', (error, file) => { + if (error) { + res.writeHead(500, { 'Content-Type': 'text/html' }); + res.end('

Profile page not found

'); + } else { + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(file); + } + }); +} + +const userDataHandler = (req, res) => { + + let userIdObject = querystring.parse(req); + let userId = Object.values(userIdObject).join(''); + console.log("UserID:", userId); + + getProfileData(userId, (error, result) => { if (error) { res.writeHead(500, 'Content-Type:text/html'); res.end( @@ -85,13 +102,13 @@ const profileHandler = (req, res) => { ); console.log(error); } else { + // console.log('Profile data handler result:', result); let profileData = JSON.stringify(result); - res.writeHead(200, { 'content-type': 'application/json' }); + res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(profileData); } }); } - -module.exports = { staticHandler, listHandler, newProfileHandler, profileHandler }; +module.exports = { staticHandler, listHandler, newProfileHandler, profileHandler, userDataHandler }; From 465f3514c628021f8eeb65549ef010dc4b475174 Mon Sep 17 00:00:00 2001 From: Giulia Date: Sun, 8 Apr 2018 21:03:14 +0100 Subject: [PATCH 6/9] populate user profile --- public/create_profile.js | 6 ----- public/user_list.html | 2 +- public/user_list.js | 2 +- public/user_profile.html | 10 ++++++-- public/user_profile.js | 44 +++++++++++++++++++++++++++++++++++ src/queries/getProfileData.js | 14 +++++++++++ 6 files changed, 68 insertions(+), 10 deletions(-) diff --git a/public/create_profile.js b/public/create_profile.js index 3e587d5..65795ac 100644 --- a/public/create_profile.js +++ b/public/create_profile.js @@ -1,13 +1,7 @@ const button = document.getElementById('form_button'); const form = document.querySelector("form"); -// function populateUserProfile (){ -// console.log('Populate user profile function reached'); -// }; -// form.addEventListener('submit', function(){ -// fetch('/user_profile', populateUserProfile); -// }); diff --git a/public/user_list.html b/public/user_list.html index 2d8939e..cee15ff 100644 --- a/public/user_list.html +++ b/public/user_list.html @@ -11,7 +11,7 @@

STACKMATCH USERS LIST

-
+
diff --git a/public/user_list.js b/public/user_list.js index 9a15fc4..84875fa 100644 --- a/public/user_list.js +++ b/public/user_list.js @@ -1,5 +1,5 @@ // Variables -var usersList = document.getElementById('js-users-list'); +var usersList = document.getElementById('js-users_list'); // User List Population diff --git a/public/user_profile.html b/public/user_profile.html index 83237c2..7c5c936 100644 --- a/public/user_profile.html +++ b/public/user_profile.html @@ -5,11 +5,17 @@ + Document - -

User profile

+
+
+
+
+
+

About me:

+
diff --git a/public/user_profile.js b/public/user_profile.js index e69de29..317f78b 100644 --- a/public/user_profile.js +++ b/public/user_profile.js @@ -0,0 +1,44 @@ +var url = location.search; +var userId = url.split('=')[1]; +// console.log("UserId", userId) +var header = document.querySelector('header'); +var info = document.getElementById('user_info') + +fetch('/user_data?id=' + userId, populateUserProfile); + +function populateUserProfile(profileData){ +var userData = profileData[0]; +console.log(userData); +var title = document.createElement('h1'); +title.textContent= userData.first_name + ' ' + userData.surname; +header.appendChild(title); + +var handle = document.createElement('p'); +handle.textContent= userData.handle; +header.appendChild(handle); + +var cohort = document.createElement('p'); +cohort.textContent= 'FAC cohort:' + userData.cohort; +info.appendChild(cohort); + +var location = document.createElement('div'); +var locationIcon = document.createElement('i'); +locationIcon.classList.add('fas', 'fa-map-marker-alt'); +location.appendChild(locationIcon); +var city = document.createElement('p'); +city.textContent = userData.city; +location.appendChild(city); +info.appendChild(location); + +var work = document.createElement('div'); +var workPara = document.createElement('p'); +if (userData.work_looking_status == 'Y'){ + workPara.textContent = 'Open to new opportunities' +}else {workPara.textContent = 'I am not looking for a new job'} +work.appendChild(workPara); +info.appendChild(work); + +document.getElementById('about_me').textContent = userData.about_me; + +} + \ No newline at end of file diff --git a/src/queries/getProfileData.js b/src/queries/getProfileData.js index e69de29..98398d7 100644 --- a/src/queries/getProfileData.js +++ b/src/queries/getProfileData.js @@ -0,0 +1,14 @@ +const dbConnect = require("../database/db_connect"); + +const getProfileData = (userId, cb) => { + + const profileData = "SELECT users.first_name, users.surname, users.handle, users.cohort, users.city, users.work_looking_status, users.about_me, (SELECT array_agg(skills.skill) FROM skills JOIN user_skills ON user_skills.skill_id = skills.id WHERE user_skills.user_id = users.id) AS skills FROM users WHERE users.id ='" + userId +"'" + + dbConnect.query(profileData, (err, res) => { + if (err) return cb(err); + // console.log("Profile data res.rows: ", res.rows); + cb(null, res.rows); + }); +}; + +module.exports = getProfileData; \ No newline at end of file From 53739f23bb2dfc1298c2549a7a6272d2e8053991 Mon Sep 17 00:00:00 2001 From: Giulia Date: Mon, 9 Apr 2018 14:00:45 +0100 Subject: [PATCH 7/9] update about me section --- public/user_list.html | 1 - public/user_profile.html | 1 + public/user_profile.js | 4 +++- src/server.js | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/public/user_list.html b/public/user_list.html index cee15ff..eca68c5 100644 --- a/public/user_list.html +++ b/public/user_list.html @@ -9,7 +9,6 @@

STACKMATCH USERS LIST

-
diff --git a/public/user_profile.html b/public/user_profile.html index 7c5c936..b819d52 100644 --- a/public/user_profile.html +++ b/public/user_profile.html @@ -9,6 +9,7 @@ Document + Users List
diff --git a/public/user_profile.js b/public/user_profile.js index 317f78b..7107e81 100644 --- a/public/user_profile.js +++ b/public/user_profile.js @@ -7,6 +7,7 @@ var info = document.getElementById('user_info') fetch('/user_data?id=' + userId, populateUserProfile); function populateUserProfile(profileData){ + var userData = profileData[0]; console.log(userData); var title = document.createElement('h1'); @@ -38,7 +39,8 @@ if (userData.work_looking_status == 'Y'){ work.appendChild(workPara); info.appendChild(work); -document.getElementById('about_me').textContent = userData.about_me; +var aboutMe = document.getElementById('about_me'); +if (userData.about_me !== null){ aboutMe.textContent += userData.about_me; } } \ No newline at end of file diff --git a/src/server.js b/src/server.js index 961db04..a7e7586 100644 --- a/src/server.js +++ b/src/server.js @@ -4,7 +4,7 @@ const router = require('./router'); require('env2')('./config.env'); const host = process.env.HOST || 'localhost'; -const port = process.env.PORT || 4000; +const port = process.env.PORT || 4300; const server = http.createServer(router); From ebbc2f957b7eb6d4a1e22c4dd82dc93fcdf894ab Mon Sep 17 00:00:00 2001 From: Giulia Date: Mon, 9 Apr 2018 15:24:26 +0100 Subject: [PATCH 8/9] create link to user profile from user list --- public/user_list.js | 8 ++++++-- src/queries/getListData.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/public/user_list.js b/public/user_list.js index 84875fa..da23364 100644 --- a/public/user_list.js +++ b/public/user_list.js @@ -21,13 +21,16 @@ function createUserCard(user) { var cardHeader = document.createElement('header'); cardHeader.className = 'usercard__header'; - var name = document.createElement('h2'); + var profileLink = document.createElement('a'); + profileLink.setAttribute('href', './user_profile?id=' + user.id); + var name = document.createElement('h2'); name.className = 'usercard__headername'; var cohort = document.createElement('span'); cohort.className = 'usercard__headercohort'; var handle = document.createElement('span'); handle.className = 'usercard__headerhandle'; - cardHeader.appendChild(name); + profileLink.appendChild(name); + cardHeader.appendChild(profileLink); cardHeader.appendChild(cohort); cardHeader.appendChild(handle); @@ -67,6 +70,7 @@ function createUserCard(user) { } function populateUserList(userData) { + console.log("userData:", userData) userData.forEach(function (user) { var userCard = createUserCard(user); usersList.appendChild(userCard); diff --git a/src/queries/getListData.js b/src/queries/getListData.js index 6a23b7e..dd9ec45 100644 --- a/src/queries/getListData.js +++ b/src/queries/getListData.js @@ -1,7 +1,7 @@ // Add code below to query your database const dbConnect = require("../database/db_connect"); -const allUsersWithSkills = 'SELECT users.first_name, users.surname, users.handle, users.cohort, users.city, users.work_looking_status, (SELECT array_agg(skills.skill) FROM skills JOIN user_skills ON user_skills.skill_id = skills.id WHERE user_skills.user_id = users.id) AS "skills" FROM users;' +const allUsersWithSkills = 'SELECT users.id, users.first_name, users.surname, users.handle, users.cohort, users.city, users.work_looking_status, (SELECT array_agg(skills.skill) FROM skills JOIN user_skills ON user_skills.skill_id = skills.id WHERE user_skills.user_id = users.id) AS "skills" FROM users;' const getListData = cb => { dbConnect.query(allUsersWithSkills, (err, res) => { From 820da635d1630a71954d83064b75de2d63f09e36 Mon Sep 17 00:00:00 2001 From: Giulia Date: Mon, 9 Apr 2018 16:08:30 +0100 Subject: [PATCH 9/9] Update server.js --- src/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server.js b/src/server.js index a7e7586..961db04 100644 --- a/src/server.js +++ b/src/server.js @@ -4,7 +4,7 @@ const router = require('./router'); require('env2')('./config.env'); const host = process.env.HOST || 'localhost'; -const port = process.env.PORT || 4300; +const port = process.env.PORT || 4000; const server = http.createServer(router);