FEATURE: ability to reorder links in custom sidebar sections (#20626)

Drag and drop to reorder custom sidebar sections
This commit is contained in:
Krzysztof Kotlarek
2023-03-21 12:23:28 +11:00
committed by GitHub
parent 53cadac4b8
commit db74e9484b
17 changed files with 343 additions and 46 deletions

View File

@ -56,6 +56,26 @@ class SidebarSectionsController < ApplicationController
render json: failed_json, status: 403
end
def reorder
sidebar_section = SidebarSection.find_by(id: reorder_params["sidebar_section_id"])
@guardian.ensure_can_edit!(sidebar_section)
order = reorder_params["links_order"].map(&:to_i).each_with_index.to_h
position_generator =
(0..sidebar_section.sidebar_section_links.count * 2).excluding(
sidebar_section.sidebar_section_links.map(&:position),
).each
links =
sidebar_section
.sidebar_section_links
.sort_by { |link| order[link.linkable_id] }
.map { |link| link.attributes.merge(position: position_generator.next) }
sidebar_section.sidebar_section_links.upsert_all(links, update_only: [:position])
render json: sidebar_section
rescue Discourse::InvalidAccess
render json: failed_json, status: 403
end
def destroy
sidebar_section = SidebarSection.find_by(id: section_params["id"])
@guardian.ensure_can_delete!(sidebar_section)
@ -82,6 +102,10 @@ class SidebarSectionsController < ApplicationController
params.permit(links: %i[icon name value id _destroy])["links"]
end
def reorder_params
params.permit(:sidebar_section_id, links_order: [])
end
def check_if_member_of_group
### TODO remove when enable_custom_sidebar_sections SiteSetting is removed
if !SiteSetting.enable_custom_sidebar_sections.present? ||