diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 90b513d49..15fc087a6 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -201,6 +201,9 @@ document.addEventListener("turbolinks:load", function(e) { // Testing section on source page Sources.init(); + // Approve/reject user curation buttons + Curation.init(); + var setStarButtonState = function (button) { if (button.data('starred')) { button.html(" "); diff --git a/app/assets/javascripts/curation.js b/app/assets/javascripts/curation.js new file mode 100644 index 000000000..bc396dda1 --- /dev/null +++ b/app/assets/javascripts/curation.js @@ -0,0 +1,36 @@ +const Curation = { + applyParam: function (select, param) { + const value = select.value; + const url = new URL(window.location.href); + if (!value) { + url.searchParams.delete(param); + } else { + url.searchParams.set(param, value); + } + window.location.replace(url.toString()); + }, + + curateUser: function (e) { + e.preventDefault(); + const url = $(this).parents('.curate-user-buttons').data('actionUrl'); + const panel = $(this).parents('.curate-user'); + panel.fadeOut('fast'); + + $.ajax({ + url: url, + method: 'PUT', + dataType: 'script', + data: { user: { role_id: $(this).data('roleId') } } + }).fail(function (e) { + panel.show(); + console.error(e); + alert('An error occurred while attempting to curate the user.'); + }); + + return false; + }, + + init: function () { + $('.curate-user-buttons .btn').click(Curation.curateUser); + } +} diff --git a/app/controllers/curator_controller.rb b/app/controllers/curator_controller.rb index 5eb4952d6..e325040c2 100644 --- a/app/controllers/curator_controller.rb +++ b/app/controllers/curator_controller.rb @@ -36,7 +36,16 @@ def topic_suggestions def users @role = Role.fetch(params[:role]) if current_user.is_admin? @role ||= Role.fetch('unverified_user') - @users = User.with_role(@role).order('created_at DESC') + @users = User.with_role(@role) + max_age = nil + if params[:max_age] + begin + max_age = ActiveSupport::Duration.parse(params[:max_age]) + rescue ArgumentError + end + end + @users = @users.where('users.created_at > ?', max_age.ago) if max_age + @users = @users.order('created_at DESC') if params[:with_content] @users = @users.includes(*User::CREATED_RESOURCE_TYPES).with_created_resources end diff --git a/app/helpers/curators_helper.rb b/app/helpers/curators_helper.rb index 52d97e201..3c912f57e 100644 --- a/app/helpers/curators_helper.rb +++ b/app/helpers/curators_helper.rb @@ -1,6 +1,13 @@ # The controller for actions related to the Ban model module CuratorsHelper + MAX_AGE_OPTIONS = [ + { period: nil, key: 'any' }.with_indifferent_access, + { period: 1.week, key: 'week' }.with_indifferent_access, + { period: 1.month, key: 'month' }.with_indifferent_access, + { period: 1.year, key: 'year' }.with_indifferent_access + ].freeze + def print_curation_action(action) resource, action = action.split('.') if action @@ -20,6 +27,14 @@ def role_options(selected_role, scope: User) options_for_select(array, selected_role.name) end + def max_age_options(selected_age = nil) + array = MAX_AGE_OPTIONS.map do |age| + [t("curation.users.filters.max_age.options.#{age[:key]}"), age[:period]&.iso8601] + end + + options_for_select(array, selected_age) + end + def recent_approvals PublicActivity::Activity.where(key: 'user.change_role').where('created_at > ?', 3.months.ago).order('created_at DESC').select do |activity| [Role.rejected.id, Role.approved.id].include?(activity.parameters[:new]) @@ -28,10 +43,10 @@ def recent_approvals def approval_message(role_id) if role_id == Role.approved.id - text = 'approved' + text = t('curation.users.activity.approved') css_class = 'text-success' else - text = 'rejected' + text = t('curation.users.activity.rejected') css_class = 'text-danger' end diff --git a/app/views/curation_mailer/user_requires_approval.html.erb b/app/views/curation_mailer/user_requires_approval.html.erb index 2ad705fff..69efadb20 100644 --- a/app/views/curation_mailer/user_requires_approval.html.erb +++ b/app/views/curation_mailer/user_requires_approval.html.erb @@ -23,4 +23,4 @@