Skip to content

Commit

Permalink
refactor: fix RuboCop and ESLint issues in controllers and JS
Browse files Browse the repository at this point in the history
  • Loading branch information
salmoneatenbybear committed Jan 21, 2025
1 parent caf71a1 commit f2e3b9e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 74 deletions.
38 changes: 22 additions & 16 deletions app/controllers/users/noticed_notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,7 @@ def mark_as_read
notification = NoticedNotification.find(params[:notification_id])
notification.update(read_at: Time.zone.now)
answer = NotifyUser.new(params).call
case answer.type
when "new_assignment"
redirect_to group_assignment_path(answer.first_param, answer.second)
when "star", "fork"
redirect_to user_project_path(answer.first_param, answer.second)
when "forum_comment"
redirect_to simple_discussion.forum_thread_path(answer.first_param, anchor: "forum_post_#{answer.second}")
when "forum_thread"
redirect_to simple_discussion.forum_thread_path(answer.first_param)
when "new_contest"
redirect_to contest_page_path(answer.first_param)
when "contest_winner"
redirect_to featured_circuits_path
else
redirect_to root_path
end
redirect_to redirect_path_for(answer)
end

def mark_all_as_read
Expand All @@ -39,4 +24,25 @@ def read_all_notifications
NoticedNotification.where(recipient: current_user, read_at: nil).update_all(read_at: Time.zone.now) # rubocop:disable Rails/SkipsModelValidations
redirect_back(fallback_location: root_path)
end

private

def redirect_path_for(answer)
case answer.type
when "new_assignment"
group_assignment_path(answer.first_param, answer.second)
when "star", "fork"
user_project_path(answer.first_param, answer.second)
when "forum_comment"
simple_discussion.forum_thread_path(answer.first_param, anchor: "forum_post_#{answer.second}")
when "forum_thread"
simple_discussion.forum_thread_path(answer.first_param)
when "new_contest"
contest_page_path(answer.first_param)
when "contest_winner"
featured_circuits_path
else
root_path
end
end
end
100 changes: 64 additions & 36 deletions app/javascript/controllers/contest_controller.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,86 @@
import { Controller } from 'stimulus';

export default class extends Controller {
static values = { timeleft: String }
static get values() {
return { timeleft: String };
}

connect() {
this.setCountDownTimer();
this.setShowModals();
this.setAdminModals();
}

enableSubmitButton() {
document.getElementById('submission-submit-button').disabled = false;
const button = this.element.querySelector('#submission-submit-button');
if (button) {
button.disabled = false;
}
}

setAdminModals() {
$("#update-contest-modal").on("show.bs.modal", function (e) {
let contestId = $(e.relatedTarget).data('contest').id;
let currentDeadline = $(e.relatedTarget).data('deadline');
let form = $(this).find('#update-contest-form');
let action = form.attr('action').replace(':contest_id', contestId);
form.attr('action', action)
const updateContestModal = this.element.querySelector('#update-contest-modal');
if (!updateContestModal) return;
$(updateContestModal).on('show.bs.modal', function handleUpdateContestModal(e) {
const contestId = $(e.relatedTarget).data('contest').id;
const currentDeadline = $(e.relatedTarget).data('deadline');
const form = $(this).find('#update-contest-form');
const action = form.attr('action').replace(':contest_id', contestId);
form.attr('action', action);
form.find('#contest_deadline').val(currentDeadline);
});

});
}

setShowModals() {
$("#projectModal").on("show.bs.modal", function (e) {
let projectSlug = $(e.relatedTarget).data('project').slug;
let projectId = $(e.relatedTarget).data('project').id;
let authorId = $(e.relatedTarget).data('project').author_id;
var id = projectSlug ? projectSlug : projectId;
$(e.currentTarget).find('#project-more-button').attr("href",
"/users/" + authorId + "/projects/" + id);
$(e.currentTarget).find('#project-ifram-preview').attr("src", "/simulator/" + id.toString())
})
$("#withdraw-submission-confirmation-modal").on("show.bs.modal", function (e) {
let contest = $(e.relatedTarget).data('contest').id;
let submission = $(e.relatedTarget).data('submission').id;
$(e.currentTarget).find("#withdraw-submission-button").attr("href",
`/contests/${contest}/withdraw/${submission}`
)
})
const projectModal = this.element.querySelector('#projectModal');
if (projectModal) {
$(projectModal).on('show.bs.modal', (e) => {
const projectData = $(e.relatedTarget).data('project');
if (!projectData) return;

const { slug, id, author_id: authorId } = projectData;
const projectSlugOrId = slug || id;

$(e.currentTarget)
.find('#project-more-button')
.attr('href', `/users/${authorId}/projects/${projectSlugOrId}`);

$(e.currentTarget)
.find('#project-ifram-preview')
.attr('src', `/simulator/${projectSlugOrId}`);
});
}

const withdrawModal = this.element.querySelector('#withdraw-submission-confirmation-modal');
if (withdrawModal) {
$(withdrawModal).on('show.bs.modal', (e) => {
const contestId = $(e.relatedTarget).data('contest').id;
const submissionId = $(e.relatedTarget).data('submission').id;
$(e.currentTarget)
.find('#withdraw-submission-button')
.attr('href', `/contests/${contestId}/withdraw/${submissionId}`);
});
}
}

setCountDownTimer() {
var deadline = new Date(this.timeleftValue).getTime();
var x = setInterval(() => {
var now = new Date().getTime();
var t = deadline - now;
var days = Math.floor(t / (1000 * 60 * 60 * 24));
var hours = Math.floor((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((t % (1000 * 60)) / 1000);
document.getElementById('timeLeftCounter').innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s `;
const deadline = new Date(this.timeleftValue).getTime();
const timeLeftCounter = this.element.querySelector('#timeLeftCounter');
if (!timeLeftCounter) return;

const x = setInterval(() => {
const now = new Date().getTime();
const t = deadline - now;

const days = Math.floor(t / (1000 * 60 * 60 * 24));
const hours = Math.floor((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
const minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((t % (1000 * 60)) / 1000);

timeLeftCounter.innerHTML = `${days}d ${hours}h ${minutes}m ${seconds}s `;
if (t < 0) {
clearInterval(x);
document.getElementById('timeLeftCounter').innerHTML = 'EXPIRED';
timeLeftCounter.innerHTML = 'EXPIRED';
}
}, 1000);
}
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ application.register('groups', GroupsController);
application.register('projects', ProjectsController);
application.register('notifications', NotificationsController);
application.register('assignment', AssignmentController);
application.register('contest', ContestController);
application.register('contest', ContestController);
71 changes: 50 additions & 21 deletions app/services/notify_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(params)
# @type [Project]
@project = @notification.params[:project]
@thread = @notification.params[:forum_thread]
@contest = @notification.params[:contest] # Added to handle ContestNotification
end

# @return [Boolean]
Expand All @@ -24,26 +25,54 @@ def call

private

# @return [Result]
def type_check
case @notification.type
when "StarNotification"
Result.new("true", "star", @project.author, @project)
when "ForkNotification"
Result.new("true", "fork", @project.author, @project)
when "NewAssignmentNotification"
Result.new("true", "new_assignment", @assignment.group, @assignment)
when "ForumCommentNotification"
@post = @notification.params[:forum_post]
Result.new("true", "forum_comment", @thread, @post.id)
when "ForumThreadNotification"
Result.new("true", "forum_thread", @thread)
when "ContestNotification"
Result.new("true", "new_contest", @contest)
when "ContestWinnerNotification"
Result.new("true", "contest_winner")
else
Result.new("false", "no_type", root_path)
end
# @return [Result]
def type_check
case @notification.type
when "StarNotification"
star_notification
when "ForkNotification"
fork_notification
when "NewAssignmentNotification"
new_assignment_notification
when "ForumCommentNotification"
forum_comment_notification
when "ForumThreadNotification"
forum_thread_notification
when "ContestNotification"
contest_notification
when "ContestWinnerNotification"
contest_winner_notification
else
Result.new("false", "no_type", root_path)
end
end

def star_notification
Result.new("true", "star", @project.author, @project)
end

def fork_notification
Result.new("true", "fork", @project.author, @project)
end

def new_assignment_notification
Result.new("true", "new_assignment", @assignment.group, @assignment)
end

def forum_comment_notification
@post = @notification.params[:forum_post]
Result.new("true", "forum_comment", @thread, @post.id)
end

def forum_thread_notification
Result.new("true", "forum_thread", @thread)
end

def contest_notification
Result.new("true", "new_contest", @contest)
end

def contest_winner_notification
Result.new("true", "contest_winner")
end
end

0 comments on commit f2e3b9e

Please sign in to comment.