We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Code Review Feedback
Code Review Feedback
Sort by
recency
|
67 Discussions
|
Please Login in order to post a comment
my UI is Updating the count correct but test cases not paaing why.
import React, { useState } from "react";
const FeedbackSystem = () => { const [aspects, setAspects] = useState({"Readability" : {upVote : 0 , downVote : 0}, "Performance" : {upVote : 0 , downVote : 0}, "Security" : {upVote : 0 , downVote : 0}, "Documentation" : {upVote : 0 , downVote : 0}, "Testing" : {upVote : 0 , downVote : 0}, })
const onChangeVoteCount = (aspect , action) => { setAspects((prev) => ({ ...prev , [aspect] : {...prev[aspect], [action] : prev[aspect][action] + 1 } })) }
return ( {Object.keys(aspects).map((key,i) => (
{key}
downvote-btn-{i}} onClick={() => onChangeVoteCount(key , "downVote")}> 👎 Downvote upvote-count-${i}}> Upvotes: {aspects[key].upVote} downvote-count-${i}}> Downvotes: {${aspects[key].downVote}
} ))}); };
export default FeedbackSystem;
Turns out, you must update dynamically
data-testid
fields, likedata-testid="downvote-count-0"
should be 0, 1, 2, etc for each card, otherwise tests will fail. It's not even mentioned in the description, it made me spend a lot of time on this simple task.MY solution:
I have used one of the most useful array method reduce, to solve this problem.
import React, { useState } from "react";
const ASPECTS = ["Redability", "Performence", "Security", "Documentation", "Testing"];
const FeedbackSystem = () => {
const [votes, setVotes] = useState( ASPECTS.reduce((acc, aspect) => ({ ...acc, [aspect.toLowerCase()]: {upvote: 0, downvote: 0} }), {}) );
const handleUpvote = (aspectName) => { const key = aspectName.toLowerCase(); setVotes((prevVotes) => ({ ...prevVotes, [key]: { ...prevVotes[key], upvote: prevVotes[key].upvote + 1 } })) }
const handleDownvote = (aspectName) => { const key = aspectName.toLowerCase(); setVotes((prevVotes) => ({ ...prevVotes, [key]: { ...prevVotes[key], downvote: prevVotes[key].downvote + 1 } })) }
return ( { ASPECTS.map((item, idx) => (
{item}
upvote-btn-{idx}} onClick={() => handleDownvote(item)}> 👎 Downvote upvote-count-${idx}}> Upvotes: {votes[item.toLowerCase()].upvote} downvote-count-${idx}}> Downvotes: {votes[item.toLowerCase()].downvote} )) } ); };export default FeedbackSystem;
### Simple and Easy Solution