mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FEATURE: ability to reorder links in custom sidebar sections (#20626)
Drag and drop to reorder custom sidebar sections
This commit is contained in:

committed by
GitHub

parent
53cadac4b8
commit
db74e9484b
@ -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? ||
|
||||
|
Reference in New Issue
Block a user