Skip to content

Commit a4c56f0

Browse files
committed
v2.0 voting system on portfolio
1 parent 29fce23 commit a4c56f0

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/Page/PortfolioIdeas/PortfolioIdeas.jsx

+50
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,46 @@ const shuffleArray = (array) => {
301301
return shuffled;
302302
};
303303

304+
const VoteButton = styled.button`
305+
position: absolute;
306+
bottom: 10px;
307+
right: 10px;
308+
display: flex;
309+
flex-direction: column;
310+
align-items: center;
311+
justify-content: center;
312+
width: 40px;
313+
height: 40px;
314+
background-color: ${(props) => (props.voted ? '#00a6fb' : 'rgb(13, 25, 53)')};
315+
color: ${(props) => (props.voted ? 'white' : '#00a6fb')};
316+
border: 2px solid #00a6fb;
317+
border-radius: 8px;
318+
cursor: pointer;
319+
transition:
320+
background-color 0.3s,
321+
color 0.3s;
322+
323+
&:hover {
324+
background-color: rgb(13, 25, 53);
325+
color: white;
326+
border: 2px solid #00a6fb;
327+
}
328+
329+
.icon {
330+
font-size: 16px;
331+
}
332+
333+
.count {
334+
font-size: 12px;
335+
margin-top: 2px;
336+
}
337+
`;
338+
304339
const PortfolioIdeas = () => {
305340
const [isModalOpen, setIsModalOpen] = useState(false);
306341
const [portfolios, setPortfolios] = useState([]);
307342
const [searchTerm, setSearchTerm] = useState('');
343+
const [votes, setVotes] = useState({});
308344
const [newPortfolio, setNewPortfolio] = useState({
309345
author: '',
310346
screenshot: '',
@@ -316,8 +352,18 @@ const PortfolioIdeas = () => {
316352
useEffect(() => {
317353
const shuffledPortfolios = shuffleArray(portfoliosData); // Shuffle portfolios
318354
setPortfolios(shuffledPortfolios); // Load shuffled portfolios from JSON file
355+
356+
// Load votes from local storage
357+
const storedVotes = JSON.parse(localStorage.getItem('portfolioVotes')) || {};
358+
setVotes(storedVotes);
319359
}, []);
320360

361+
const handleVote = (index) => {
362+
const newVotes = { ...votes, [index]: (votes[index] || 0) + 1 };
363+
setVotes(newVotes);
364+
localStorage.setItem('portfolioVotes', JSON.stringify(newVotes));
365+
};
366+
321367
const handleSubmit = (e) => {
322368
e.preventDefault();
323369
setPortfolios([...portfolios, newPortfolio]);
@@ -645,6 +691,10 @@ const PortfolioIdeas = () => {
645691
alt={`${portfolio.author}'s portfolio`}
646692
className="h-full w-full rounded-lg object-cover"
647693
/>
694+
<VoteButton voted={votes[index] > 0} onClick={() => handleVote(index)}>
695+
<div className="icon"></div>
696+
<div className="count">{votes[index] || 0}</div>
697+
</VoteButton>
648698
</div>
649699
<h3 className="mb-2 flex items-center justify-between text-lg font-semibold text-white">
650700
{portfolio.author}

0 commit comments

Comments
 (0)