Skip to content

Commit

Permalink
[#27] Fixed issue with voting not redirecting when not logged in.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmckissock committed Jul 1, 2024
1 parent 80b9b39 commit 4788af0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 52 deletions.
16 changes: 12 additions & 4 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ document.addEventListener("DOMContentLoaded", function() {
const upvoteButtons = document.querySelectorAll('.upvote-button');
const downvoteButtons = document.querySelectorAll('.downvote-button');

// Function to handle voting
function handleVote(votableId, votableType, path, articleId = null) {
let url;
if (votableType === 'Article') {
Expand All @@ -21,9 +20,17 @@ document.addEventListener("DOMContentLoaded", function() {
},
body: JSON.stringify({})
})
.then(response => response.json())
.then(response => {
if (response.redirected) {
window.location.href = response.url;
} else {
return response.json();
}
})
.then(data => {
document.getElementById(`score-${votableType}-${votableId}`).textContent = `${data.new_score} points`;
if (data) {
document.getElementById(`score-${votableType}-${votableId}`).textContent = `${data.new_score} points`;
}
})
.catch(error => console.log('Error:', error));
}
Expand All @@ -34,7 +41,7 @@ document.addEventListener("DOMContentLoaded", function() {
const votableId = this.dataset.votableId;
const votableType = this.dataset.votableType;
const articleId = this.dataset.articleId;
handleVote(votableId, votableType, 'upvote');
handleVote(votableId, votableType, 'upvote', articleId);
});
});

Expand All @@ -49,3 +56,4 @@ document.addEventListener("DOMContentLoaded", function() {
});
});


7 changes: 6 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ def logged_in?
end

def authorized
redirect_to login_path unless logged_in?
unless logged_in?
respond_to do |format|
format.html { redirect_to login_path, alert: "You must be logged in to perform that action." }
format.json { render json: { error: "Unauthorized" }, status: :unauthorized }
end
end
end

def home?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ArticlesController < ApplicationController
before_action :authorized, only: [:new, :create]
before_action :authorized, only: [:new, :create, :upvote, :downvote]

def index
@articles = Article.order(created_at: :desc).page(params[:page]).per(20)
Expand Down
2 changes: 0 additions & 2 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<!-- app/views/articles/index.html.erb -->

<tr style="height:10px"></tr>
<tr>
<td>
Expand Down
88 changes: 44 additions & 44 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
<!DOCTYPE html>
<html>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

<head>
<title>Haxxor News</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
<center>
<table border="0" cellpadding="0" cellspacing="0" width="85%" style="padding:2px" bgcolor="#f6f6ef">
<tbody>
<tr>
<td bgcolor="#ff6600">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding:2px">
<tbody>
<tr>
<td style="line-height:12pt; height:10px;">
<span class="pagetop">
<b class="hnname">
<%= link_to 'Haxxor News', root_path %>
</b>
<%= link_to 'new', new_article_path %>
</td>
</span>
<td style="text-align:right; padding-right:4px;">
<div style="display: flex; justify-content: flex-end; gap: 5px;">
<body>
<center>
<table border="0" cellpadding="0" cellspacing="0" width="85%" style="padding:2px" bgcolor="#f6f6ef">
<tbody>
<tr>
<td bgcolor="#ff6600">
<table border="0" cellpadding="0" cellspacing="0" width="100%" style="padding:2px">
<tbody>
<tr>
<td style="line-height:12pt; height:10px;">
<span class="pagetop">
<% if logged_in? %>
<%= link_to current_user.name, user_path(current_user), class: "user-link" %>
|
<%= link_to "logout", logout_path, method: :delete, class: "logout-link" %>
<% else %>
<%= link_to "login", login_path, method: :get %>
|
<%= link_to "signup", signup_path, method: :get %>
<% end %>
<b class="hnname">
<%= link_to 'Haxxor News', root_path %>
</b>
<%= link_to 'new', new_article_path %>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<%=yield%>
</td>
<td style="text-align:right; padding-right:4px;">
<div style="display: flex; justify-content: flex-end; gap: 5px;">
<span class="pagetop">
<% if logged_in? %>
<%= link_to current_user.name, user_path(current_user), class: "user-link" %>
|
<%= link_to "logout", logout_path, method: :delete, class: "logout-link" %>
<% else %>
<%= link_to "login", login_path, method: :get %>
|
<%= link_to "signup", signup_path, method: :get %>
<% end %>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<%= yield %>
</tbody>
</table>
</center>
</body>
</table>
</center>
</body>
</html>

0 comments on commit 4788af0

Please sign in to comment.