app/controllers/maglev/editor/sections_controller.rb



# frozen_string_literal: true

module Maglev
  module Editor
    class SectionsController < Maglev::Editor::BaseController
      helper Maglev::Editor::SettingsHelper
      helper_method :source_lock_version

      before_action :ensure_turbo_frame_request, only: [:new]
      before_action :set_section, only: %i[edit update]

      def show
        redirect_to edit_editor_section_path(params[:id], maglev_editing_route_context)
      end

      def new
        set_query_and_category_id
        @position = (params[:position] || -1).to_i
        @theme_sections = maglev_theme.sections.filter(current_maglev_sections, keyword: @query,
                                                                                category_id: @category_id)
        render layout: false
      end

      def create
        @section = services.add_section.call(
          page: current_maglev_page,
          section_type: params[:section_type],
          position: params[:position].to_i
        )
        redirect_to edit_editor_section_path(@section[:id], maglev_editing_route_context),
                    flash: newly_added_section_to_flash,
                    notice: flash_t(:success),
                    status: :see_other
      end

      def edit
        newly_added_section_to_headers
      end

      def update
        update_section
        flash.now[:notice] = flash_t(:success)
      end

      def sort
        services.sort_sections.call(
          page: current_maglev_page,
          section_ids: params[:item_ids],
          lock_version: params[:lock_version]
        )
        redirect_to_sections_path
      end

      def destroy
        services.delete_section.call(page: current_maglev_page, section_id: params[:id])
        redirect_to_sections_path
      end

      private

      def set_section
        @section = current_maglev_sections.find { |section| section.id == params[:id] }
        redirect_to editor_sections_path_with_context unless @section
      end

      def update_section
        services.update_section.call(
          page: current_maglev_page,
          section_id: @section.id,
          content: params[:section].to_unsafe_h,
          lock_version: params[:lock_version]
        )
      end

      def render_index_with_error
        flash.now[:alert] = flash_t(:error)
        render 'index', status: :unprocessable_content
      end

      def set_query_and_category_id
        # we can't filter by both query and category_id in the same time
        @query = params[:category_id].present? ? nil : params[:query]
        # if no category_id is provided AND we don't have a query, we take the first category
        @category_id = params[:category_id] || maglev_theme.section_categories.first.id
        @category_id = nil if @query.present?
      end

      def newly_added_section_to_flash
        # use flash because we can't pass directly the information to the redirect
        { section_id: @section[:id], position: current_maglev_page.position_of_section(@section[:id]) }
      end

      def newly_added_section_to_headers
        headers['X-Section-Id'] = flash[:section_id]
        headers['X-Section-Position'] = flash[:position]
      end

      def lock_source
        @section.site_scoped? ? maglev_site : current_maglev_page
      end

      def source_lock_version
        lock_source.lock_version || 0
      end

      def redirect_to_sections_path
        redirect_to editor_sections_path_with_context, notice: flash_t(:success), status: :see_other
      end

      def editor_sections_path_with_context
        editor_sections_path(maglev_editing_route_context)
      end
    end
  end
end