Skip to content

Commit

Permalink
small optimization, need to do some more later
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirc5 committed Oct 18, 2023
1 parent cc46522 commit 1176e67
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/Components/DOM/VoteStatusButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ const IconImg = styled.img`
margin-top: 3px;
`;

const iconMap = {
const getIconMap = (totalYesVotes, totalNoVotes, prState) => ({
visible: prState !== 'merge',

This comment has been minimized.

Copy link
@jex441

jex441 Oct 19, 2023

This looks a lot better. I am a little confused as to how this works exactly because isn't the icon not visible in other states as well? Here it looks like it's just not visible unless it's merge.

Maybe I'm not understanding correctly.

This comment has been minimized.

Copy link
@jex441
frozen: LockIcon,
upvote: UpArrow,
downvote: DownArrow
};
let iconKey;
'pre-open': totalYesVotes >= totalNoVotes ? UpArrow : DownArrow,
open: totalYesVotes >= totalNoVotes ? UpArrow : DownArrow,
merge: null

This comment has been minimized.

Copy link
@jex441

jex441 Oct 19, 2023

is merge: null necessary? @ramirc5

This comment has been minimized.

Copy link
@jex441

jex441 Oct 19, 2023

Also not 100% sure why it's a hook and not just a hash map. I have a few questions about implementation here so I wonder if it would be better if you walked me through your approach rather than me try to understand it.

I like that it's a big reduction in code, especially long if else statements, but I think I need some more info. @ramirc5

});


export default function VoteStatusButton({
user,
Expand All @@ -53,16 +55,7 @@ export default function VoteStatusButton({
const { prData, loading } = useGetVotes(user, repoID, issueID, contributorID, side, socketEvents, clicked);
const totalYesVotes = prData?.voteData?.voteTotals?.totalYesVotes;
const totalNoVotes = prData?.voteData?.voteTotals?.totalNoVotes;

if (prData?.state === 'frozen') {
iconKey = 'frozen';
} else if ((prData?.state === 'pre-open' || prData?.state === 'open') && totalYesVotes >= totalNoVotes) {
iconKey = 'upvote';
} else if ((prData?.state === 'pre-open' || prData?.state === 'open') && totalYesVotes < totalNoVotes) {
iconKey = 'downvote';
} else {
iconKey = null;
}
const iconMap = getIconMap(totalYesVotes, totalNoVotes, prData?.state);

const buttonStyle = {
vote: ['#61D25E', 'vote'],
Expand Down Expand Up @@ -101,7 +94,7 @@ export default function VoteStatusButton({
return (
<ButtonVote style={{ background: voteStatusButton.color }} onClick={e => handleClick(e)}>
<SpanVote>
{iconKey && <IconImg src={iconMap[iconKey]} alt={iconKey} />}
{iconMap.visible && <IconImg src={iconMap[prData.state]} alt={prData.state} />}
{voteStatusButton.text}
</SpanVote>
</ButtonVote>
Expand Down

0 comments on commit 1176e67

Please sign in to comment.