[Replacements] Use ajax for approve/reject/toggle/promote actions

This commit is contained in:
Earlopain 2021-06-26 18:25:25 +02:00
parent 08150d7cca
commit 5d430e8210
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
5 changed files with 98 additions and 32 deletions

View File

@ -1,5 +1,5 @@
class PostReplacementsController < ApplicationController
respond_to :html
respond_to :html, :json
before_action :moderator_only, only: [:destroy]
before_action :janitor_only, only: [:create, :new, :approve, :reject, :promote, :toggle_penalize]
content_security_policy only: [:new] do |p|
@ -26,22 +26,14 @@ class PostReplacementsController < ApplicationController
def approve
@post_replacement = PostReplacement.find(params[:id])
@post_replacement.approve!(penalize_current_uploader: params[:penalize_current_uploader])
if @post_replacement.errors.any?
flash[:notice] = @post_replacement.errors.full_messages.join("; ")
else
flash[:notice] = "Post replacement accepted"
end
respond_with(@post_replacement, location: post_path(@post_replacement.post))
respond_with(@post_replacement)
end
def toggle_penalize
@post_replacement = PostReplacement.find(params[:id])
@post_replacement.toggle_penalize!
if @post_replacement.errors.any?
flash[:notice] = @post_replacement.errors.full_messages.join("; ")
else
flash[:notice] = "Updated user upload limit"
end
respond_with(@post_replacement)
end
@ -49,13 +41,7 @@ class PostReplacementsController < ApplicationController
@post_replacement = PostReplacement.find(params[:id])
@post_replacement.reject!
if @post_replacement.errors.any?
flash[:notice] = @post_replacement.errors.full_messages.join("; ")
else
flash[:notice] = "Post replacement rejected"
end
respond_with(@post_replacement)
respond_with(@post_replacement, location: post_path(@post_replacement.post))
end
def destroy
@ -68,16 +54,12 @@ class PostReplacementsController < ApplicationController
def promote
@post_replacement = PostReplacement.find(params[:id])
@upload = @post_replacement.promote!
if @post_replacement.errors.any?
flash[:notice] = @post_replacement.errors.full_messages.join("; ")
respond_with(@upload)
respond_with(@post_replacement)
elsif @upload.errors.any?
flash[:notice] = @upload.errors.full_messages.join("; ")
respond_with(@upload)
else
flash[:notice] = "Post replacement promoted to post ##{@upload.post.id}"
respond_with(@upload.post)
respond_with(@upload)
end
end

View File

@ -37,6 +37,7 @@ export { default as Note } from '../src/javascripts/notes.js';
export { default as Post } from '../src/javascripts/posts.js';
export { default as PostDeletion } from "../src/javascripts/post_delete.js";
export { default as PostModeMenu } from '../src/javascripts/post_mode_menu.js';
export { default as PostReplacement } from '../src/javascripts/post_replacement.js';
export { default as PostVersions } from '../src/javascripts/post_versions.js';
export { default as RelatedTag } from '../src/javascripts/related_tag.js';
export { default as Shortcuts } from '../src/javascripts/shortcuts.js';

View File

@ -0,0 +1,83 @@
import Utility from './utility'
let PostReplacement = {};
PostReplacement.initialize_all = function () {
$(".replacement-approve-action").on("click", e => {
const target = $(e.target);
e.preventDefault();
PostReplacement.approve(target.data("replacement-id"), target.data("penalize"));
});
$(".replacement-reject-action").on("click", e => {
e.preventDefault();
PostReplacement.reject($(e.target).data("replacement-id"));
});
$(".replacement-promote-action").on("click", e => {
e.preventDefault();
PostReplacement.promote($(e.target).data("replacement-id"));
});
$(".replacement-toggle-penalize-action").on("click", e => {
e.preventDefault();
PostReplacement.toggle_penalize($(e.target).data("replacement-id"));
});
};
PostReplacement.approve = function (id, penalize_current_uploader) {
$.ajax({
type: "PUT",
url: `/post_replacements/${id}/approve.json`,
data: {
penalize_current_uploader: penalize_current_uploader
},
dataType: 'json'
}).done(function () {
Utility.notice("Post Replacement accepted");
}).fail(function (data, status, xhr) {
Utility.error(data.responseText);
});
};
PostReplacement.reject = function (id) {
$.ajax({
type: "PUT",
url: `/post_replacements/${id}/reject.json`,
dataType: 'json'
}).done(function () {
Utility.notice("Post Replacement rejected");
}).fail(function (data, status, xhr) {
Utility.error(data.responseText);
});
}
PostReplacement.promote = function (id) {
$.ajax({
type: "POST",
url: `/post_replacements/${id}/promote.json`,
dataType: 'json'
}).done(function (data) {
console.log(data);
Utility.notice(`Replacement promoted to post #${data.post_id}`)
}).fail(function (data, status, xhr) {
Utility.error(data.responseText);
});
}
PostReplacement.toggle_penalize = function (id) {
$.ajax({
type: "PUT",
url: `/post_replacements/${id}/toggle_penalize.json`,
dataType: 'json'
}).done(function (data) {
Utility.notice("User upload limit updated");
}).fail(function (data, status, xhr) {
Utility.error(data.responseText);
});
}
$(function () {
if ($("#c-post-replacements").length)
PostReplacement.initialize_all();
});
export default PostReplacement

View File

@ -75,7 +75,7 @@
<%= link_to_user post_replacement.uploader_on_approve %>,
penalized: <%= post_replacement.penalize_uploader_on_approve ? "yes" : "no" %>
<% if CurrentUser.is_janitor? %>
<%= link_to "toggle", toggle_penalize_post_replacement_path(post_replacement), method: :PUT %>
<%= link_to "toggle", "#toggle", class: "replacement-toggle-penalize-action", data: { replacement_id: post_replacement.id} %><br>
<% end %>
</dd>
<dt>Approver</dt>
@ -93,13 +93,13 @@
<td>
<% if CurrentUser.is_janitor? %>
<% if post_replacement.status == "pending"%>
<%= link_to "Approve And Penalize Current Uploader", approve_post_replacement_path(post_replacement, penalize_current_uploader: true), method: :PUT %><br>
<%= link_to "Approve", approve_post_replacement_path(post_replacement, penalize_current_uploader: false), method: :PUT %><br>
<%= link_to "Reject", reject_post_replacement_path(post_replacement), method: :PUT %><br>
<%= link_to "As New Post", promote_post_replacement_path(post_replacement), method: :PUT %><br>
<%= link_to "Approve And Penalize Current Uploader", "#approve", class: "replacement-approve-action", data: { replacement_id: post_replacement.id, penalize: true} %><br>
<%= link_to "Approve", "#approve", class: "replacement-approve-action", data: { replacement_id: post_replacement.id, penalize: false} %><br>
<%= link_to "Reject", "#reject", class: "replacement-reject-action", data: { replacement_id: post_replacement.id} %><br>
<%= link_to "As New Post", "#promote", class: "replacement-promote-action", data: { replacement_id: post_replacement.id} %><br>
<% end %>
<% if post_replacement.status == "original" %>
<%= link_to "Reset To", approve_post_replacement_path(post_replacement, penalize_current_uploader: false), method: :PUT %><br>
<%= link_to "Reset To", "#approve", class: "replacement-approve-action", data: { replacement_id: post_replacement.id, penalize: false} %><br>
<% end %>
<% end %>
<% if CurrentUser.is_moderator? %>

View File

@ -250,7 +250,7 @@ Rails.application.routes.draw do
member do
put :approve
put :reject
put :promote
post :promote
put :toggle_penalize
end
end