Skip to content

Commit

Permalink
Merge pull request #142 from Yash-Ainapure/patch7
Browse files Browse the repository at this point in the history
Feature: upvote or downvote the secrets
  • Loading branch information
Kritika30032002 authored Dec 25, 2023
2 parents 4f5d814 + cd2a427 commit 2d23f1d
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 36 deletions.
62 changes: 58 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ app.use(passport.session());

//mongoose.connect("mongodb://127.0.0.1:27017/userDB");


const userSchema = new mongoose.Schema({
username: String,
password: String,
googleId: String,
facebookId: String,
secret: [String],
secret: [
{
title: { type: String, required: true },
upvote: { type: Number, default: 0 },
downvote: { type: Number, default: 0 },
}
],
});

userSchema.plugin(passportLocalMongoose);
Expand Down Expand Up @@ -219,7 +226,13 @@ app.get("/secrets", function (req, res) {
console.log(err);
} else {
if (foundUsers) {
res.render("secrets", { usersWithSecrets: foundUsers });


res.render("secrets", {
usersWithSecrets: foundUsers,
upvoted: false,
downvoted: false,
});
}
}
});
Expand All @@ -241,7 +254,7 @@ app.post("/submit-secret-form", function (req, res) {
} else {
if (foundUser) {

foundUser.secret.push(submittedSecret);
foundUser.secret.push({ title: submittedSecret });

foundUser.save(function (err) {
if (err) {
Expand All @@ -256,7 +269,7 @@ app.post("/submit-secret-form", function (req, res) {
});

app.get("/logout", function (req, res) {
req.logout(function(err) {
req.logout(function (err) {
if (err) {
console.error('Error during logout:', err);
return res.redirect("/"); // Handle errors gracefully, redirect to home or login page
Expand All @@ -266,6 +279,47 @@ app.get("/logout", function (req, res) {
});
});

// Update vote counts
app.post('/api/votes', async (req, res) => {
try {
const { upvoteCount, downvoteCount, index, username } = req.body;

// Fetch the user from the database based on the username
const user = await User.findOne({ username });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Check if the index is valid
if (index < 0 || index >= user.secret.length) {
return res.status(400).json({ error: 'Invalid secret index' });
}

// Update upvote and downvote for the specified secret
user.secret[index].upvote = upvoteCount;
user.secret[index].downvote = downvoteCount;

// Save the updated user object
await user.save();

res.json({ message: 'Vote updated successfully', user });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
}

// const { upvotes, downvotes } = req.body;

// try {
// const votes = await Vote.findOneAndUpdate({}, { upvotes, downvotes }, { new: true, upsert: true });
// res.json(votes);
// } catch (error) {
// console.error(error);
// res.status(500).send('Internal Server Error');
// }
});

// Catch-all route for 404 errors
app.get('*', (req, res) => {
// Redirect to a specific URL or send a custom 404 response
Expand Down
17 changes: 17 additions & 0 deletions public/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ header h1 {
.secret-text {
font-size: 1.3em;
padding: 1rem;
display: flex;
}
.vote-container{
display: flex;
}
.inner-secret-text{
margin-left: 30%;
margin-right: 15px;
}
.vote-container{
padding: 0 5px;
}
.vote-container img{
margin: 0 5px;
}
.vote-count{
margin-right: 10px;
}


Expand Down
219 changes: 187 additions & 32 deletions views/secrets.ejs
Original file line number Diff line number Diff line change
@@ -1,36 +1,191 @@
<%- include('partials/header') %>

<div class="secrets-container jumbotron text-center">
<div class="container">
<i class="fas fa-key fa-6x"></i>
<h1 class="display-3">You've Discovered My Secret!</h1>

<% usersWithSecrets.forEach(function(user){ %>
<% user.secret.forEach(function(secret){ %>
<p class="secret-text"><%= secret %></p>
<% }) %>
<% }) %>


<hr />
<div class="d-flex justify-content-center">
<a class="button_all_white" href="/logout" role="button"
><button class="secondary" style="font-size: 1em; margin: 10px">
Log Out
</button></a
>
<a class="button_all_black" href="/submit-secret-form" role="button"
><button style="font-size: 1em; margin: 10px">
Submit a Secret
</button></a
>
<div class="secrets-container jumbotron text-center">
<div class="container">
<i class="fas fa-key fa-6x"></i>
<h1 class="display-3">You've Discovered My Secret!</h1>

<% usersWithSecrets.forEach(function(user){ %>
<% user.secret.forEach(function(secret,index){ %>
<div class="card">
<div class="card-body" style="display: flex;justify-content:flex-start;">
<p class="secret-text">
<div class="inner-secret-text">
<%= secret.title %>
</div>
<div class="vote-container" id="vote-container-<%= index %>">
<img class="vote-image" alt="Upvote" data-index='<%= index %>' data-value='<%= user.username %>'
id="upvote-image-<%= index %>" data-upvotecount='<%= secret.upvote %>'
data-downvotecount='<%= secret.downvote %>' onclick="handleUpvote(this ,<%= index %>)" width="24"
height="24"
src="<%= upvoted ? 'https://img.icons8.com/material-rounded/24/thumb-up.png' : 'https://img.icons8.com/external-royyan-wijaya-detailed-outline-royyan-wijaya/24/external-thumbs-up-interface-royyan-wijaya-detailed-outline-royyan-wijaya.png' %>" />
<span class="vote-count" id="upvote-count-<%= index %>">
<%= secret.upvote %>
</span>
<img class="vote-image" width="24" height="24" data-index='<%= index %>'
id="downvote-image-<%= index %>" data-value='<%= user.username %>'
data-upvotecount='<%= secret.upvote %>' data-downvotecount='<%= secret.downvote %>'
onclick="handleDownvote(this, <%= index %>)"
src="<%= downvoted ? 'https://img.icons8.com/material/24/thumbs-down--v1.png' : 'https://img.icons8.com/external-royyan-wijaya-detailed-outline-royyan-wijaya/24/external-thumbs-down-interface-royyan-wijaya-detailed-outline-royyan-wijaya.png' %>"
alt="Downvote" />
<span class="vote-count" id="downvote-count-<%= index %>">
<%= secret.downvote %>
</span>
</div>
</p>
</div>
</div>
<% }) %>
<% }) %>
<style>
.card {
margin: 10px 0px;
background-color: #315B9A;
transition: transform 0.3s ease-in-out;
font-size: 20px;
}
.card:hover {
transform: scale(1.05);
box-shadow: 0 0 15px #64a9db;
}
.card-text {
color: #A7DAFF;
}
</style>
<script>
let upvoted = <%= upvoted %>;
let downvoted = <%= downvoted %>;
function handleUpvote(element, index) {
let upvoteCount = element.getAttribute("data-upvotecount");
let downvoteCount = element.getAttribute("data-downvotecount");
//let index = element.getAttribute('data-index');
let username = element.getAttribute('data-value');
//console.log("index: " + index + " username : " + username);
if (!upvoted) {
upvoted = true;
upvoteCount++;
if (downvoted) {
downvoted = false;
}
} else {
upvoted = false;
}
updateVotes(upvoteCount, downvoteCount, index, username);
// // Update the image source and count on the page
//console.log("upvoted: " + upvoted);
//console.log("downvoted: " + downvoted);
updateUI(upvoted, downvoted, upvoteCount, downvoteCount, index);
}
function handleDownvote(element, index) {
let upvoteCount = element.getAttribute("data-upvotecount");
let downvoteCount = element.getAttribute("data-downvotecount");
//let index = element.getAttribute('data-index');
let username = element.getAttribute('data-value');
//console.log("index: " + index + " username : " + username);
if (!downvoted) {
downvoted = true;
//upvoted = false;
downvoteCount++;
if (upvoted) {
upvoted = false;
//upvoteCount--;
}
} else {
downvoted = false;
//downvoteCount--;
}
updateVotes(upvoteCount, downvoteCount, index, username);
// Update the image source and count on the page
//console.log("upvoted: " + upvoted);
//console.log("downvoted: " + downvoted);
updateUI(upvoted, downvoted, upvoteCount, downvoteCount, index);
}
function updateVotes(upvoteCount, downvoteCount, index, username) {
// Implement logic to send an AJAX request or use fetch to update the server-side vote counts
// You would typically send a POST request to your server here
// Example using fetch:
fetch('/api/votes', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
upvoteCount,
downvoteCount,
index,
username,
}),
})
.then(response => response.json())
.then(data => console.log('Votes updated:', data))
.catch(error => console.error('Error updating votes:', error));
}
function updateUI(upvoted, downvoted, upvoteCount, downvoteCount, index) {
// Update the image sources and vote counts on the page for the specific secret
const containerId = `vote-container-${index}`;
const upvoteImageId = `upvote-image-${index}`;
const downvoteImageId = `downvote-image-${index}`;
const upvoteCountId = `upvote-count-${index}`;
const downvoteCountId = `downvote-count-${index}`;
const upvoteImage = document.getElementById(upvoteImageId);
const downvoteImage = document.getElementById(downvoteImageId);
const upvoteCountElement = document.getElementById(upvoteCountId);
const downvoteCountElement = document.getElementById(downvoteCountId);
//getting wrong values -- null
//console.log(upvoteImage)
//console.log(downvoteImage)
//getting correct values below
//console.log(upvoteCountElement)
//console.log(downvoteCountElement)
if (upvoteImage && downvoteImage && upvoteCountElement && downvoteCountElement) {
upvoteImage.src = upvoted ? 'https://img.icons8.com/material-rounded/24/thumb-up.png' : 'https://img.icons8.com/external-royyan-wijaya-detailed-outline-royyan-wijaya/24/external-thumbs-up-interface-royyan-wijaya-detailed-outline-royyan-wijaya.png';
downvoteImage.src = downvoted ? 'https://img.icons8.com/material/24/thumbs-down--v1.png' : 'https://img.icons8.com/external-royyan-wijaya-detailed-outline-royyan-wijaya/24/external-thumbs-down-interface-royyan-wijaya-detailed-outline-royyan-wijaya.png';
upvoteCountElement.innerText = upvoteCount;
downvoteCountElement.innerText = downvoteCount;
} else {
console.error(`One or more elements not found for secret at index ${index}`);
}
}
</script>


<hr />
<div class="d-flex justify-content-center">
<a class="button_all_white" href="/logout" role="button"><button class="secondary"
style="font-size: 1em; margin: 10px">
Log Out
</button></a>
<a class="button_all_black" href="/submit-secret-form" role="button"><button
style="font-size: 1em; margin: 10px">
Submit a Secret
</button></a>
</div>
</div>
</div>
</div>
<a href="#navbar" id="goToTopBtn" title="Go to top">
<img
src="https://img.icons8.com/ios-filled/50/000000/long-arrow-up.png"
style="width: 70%; height: 70%; color: white"
/>
</a>
<%- include('partials/footer') %>
<a href="#navbar" id="goToTopBtn" title="Go to top">
<img src="https://img.icons8.com/ios-filled/50/000000/long-arrow-up.png"
style="width: 70%; height: 70%; color: white" />
</a>
<%- include('partials/footer') %>

0 comments on commit 2d23f1d

Please sign in to comment.