-
Notifications
You must be signed in to change notification settings - Fork 4
#6 created a Client-side functionality for the Helpful and Not Helpfu… #15
Conversation
…Helpful buttons
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good start! Just a few things to touch up and then start working on the problem of how you'll get each question's button to work separately.
views/index.hbs
Outdated
<div class="entry__help"> | ||
<span id="displayCount">{{this.helpful}}</span>/<span id="totalNumber"></span> people found this helpful. Was this entry helpful? | ||
<button id="Button1" class="button button--small">Yes</button> | ||
<button id="Button2" class="button button--small">No</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Button1
and Button2
are bad names for the buttons. Consider using a more descriptive name like button-helpful
and button-not-helpful
.
public/main.js
Outdated
unhelpful++; | ||
total++; | ||
totalNumber.innerHTML = total; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're going to run into problems using getElementById
, because every id
on an HTML page must be completely unique. You can't have two elements with the same id
.
Learn about querySelectorAll which allows you to select by class name and other attributes.
You'll face the same problem with your global helpful
and total
vars. If each question has it's own values, you'll need a way to store those values separately.
views/layout.hbs
Outdated
@@ -14,5 +14,6 @@ | |||
</header> | |||
{{{body}}} | |||
</div> | |||
<script src="/main.js"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation doesn't look right here. Try formatting the document.
sending AJAX request to the route called 'rating' then resiving the change and display all the changes
…Helpful buttons
sending AJAX request to the route called 'rating' then resiving the change and display all the changes
I saw the review and changed a lot of staff |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @Yjohn and @khaledkzy! Code looks really clean. Just a few comments throughout. I haven't tested this myself but let me know when you address the comments and I'll give it a go.
helpful: helpful | ||
}) | ||
}; | ||
fetch("/rating", options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does the fetch
function come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fetch is built-in in modern browsers (i can use fetch without using Jquery or any other library)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I didn't even know about that!
}) | ||
}; | ||
fetch("/rating", options) | ||
.then(response => response.json()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's this line doing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the route "/rating" is the Url that I and Khalid communicate with the server
it's the same like url: "/api/helpful"
, it's our argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean specifically the line .then(response => response.json())
but I think I understand now. That's converting the response string into an object, yeah?
routes/index.js
Outdated
const renderEntries = function(error, entry) { | ||
if (error) { | ||
console.log(error); | ||
res.sendStatus(400) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A 400
status usually indicates an authentication error (ie - "You are not allowed to do this."). In your case, I think you want to render a 500
status (ie - "The server ran into an error.").
routes/index.js
Outdated
{$inc: { [helpful ? 'helpful' : 'unhelpful' ]: 1}}, | ||
{new: true} | ||
, renderEntries); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you might have some indentation issues here. Try formatting the document.
views/index.hbs
Outdated
{{{this.answer}}} | ||
</div> | ||
<div class="entry__help"> | ||
<span class="value-helpful" data-question-id={{this._id}}>{{this.helpful}} people found this helpful. Was this entry helpful?</span> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing quotes "
around {{this._id}}
.
Also, you're missing the total count: 11/25 people found this helpful. You may need @khaledkzy to modify the data delivered to the template for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Khalid accept only the ID and the boolean of the Helpful buttons, and he changed depends on what he gets from me
if it's false he add the total number and if it's true he adds 1 to the helpful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but in this initial template, the string displays without a total count. You only say "11 people found this helpful" but you need to display "11/25 people found this helpful". Yeah?
@@ -0,0 +1,32 @@ | |||
const clickEvent = function (event) { | |||
const id = event.target.getAttribute('data-question-id'); | |||
const helpful = JSON.parse(event.target.getAttribute('data-helpful')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need the JSON.parse()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I need JSON.parse() to change the string to boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
@NateWr I commented all the feedback, let me know ... :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! The code looks really good. I haven't tested it yet but let me know when you make the last few changes requested and I'll take it for a spin.
views/index.hbs
Outdated
<span class="value-helpful" data-question-id={{this._id}}>{{this.helpful}}/{{this.total}} people found this helpful. Was this entry helpful?</span> | ||
<button class="button button--small button-feedback" data-helpful="true" data-question-id="{{this._id}}">Yes</button> | ||
<button class="button button--small button-feedback" data-helpful="false" data-question-id="{{this._id}}">No</button> | ||
<div id="loading" class="hidden"></div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The id
attribute needs to be unique across the entire page. So if you have lots of elements with id="loading"
, it can cause problems if someone tries to use document.getElementById('loading')
.
It works in your case because you're limiting the selection document.querySelector('.class-<id> #loading')
. But it should generally be avoided. You can either:
-
Add a unique value to each id:
id="loading-{this._id}"
. -
Turn the selector into a class: `class="hidden loading".
It's up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created an id only to apply the animation and style. If someone tries to use document.getElementById('loading')
, only select empty <div>
with animations.
I tried to make it class="hidden loading"
but isn't working
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Show me the code that you've tried to get it working with a class
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public/main.js
Outdated
let valueElId = valueEl.getAttribute('data-question-id') | ||
if (res._id === valueElId) { | ||
let totalNumber = res.helpful + res.unhelpful; | ||
valueEl.innerHTML = res.helpful + '/' + totalNumber + ' people found this helpful. was this entry helpful'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this entry helpful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how I received changes from the database and server, then display the number of people who select the button of helpful and the total number of people whose give their votes.
which part makes you confused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have explained. I meant to change the words at the end of the line from was this entry helpful
to Was this entry helpful?
This is amazing @Yjohn! I love the loading animation too. I've merged it now. 👍 |
#6 it needs more work to make the buttons functional