From ef57771b08c5e844365505616384ab5b3c7ca35a Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 13 Jan 2023 06:47:58 +0800 Subject: [PATCH] FIX: Preload user sidebar attrs when `?enable_sidebar=1` (#19843) This allows users to preview the sidebar even when `SiteSetting.naviation_menu` is set to `false`. --- .../javascripts/bootstrap-json/index.js | 5 +++++ app/controllers/application_controller.rb | 12 +++++++++- .../concerns/user_sidebar_mixin.rb | 2 +- .../system/page_objects/components/sidebar.rb | 15 +++++++++++++ spec/system/viewing_sidebar_spec.rb | 22 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 spec/system/page_objects/components/sidebar.rb create mode 100644 spec/system/viewing_sidebar_spec.rb diff --git a/app/assets/javascripts/bootstrap-json/index.js b/app/assets/javascripts/bootstrap-json/index.js index a2087fdc162..87826a54f72 100644 --- a/app/assets/javascripts/bootstrap-json/index.js +++ b/app/assets/javascripts/bootstrap-json/index.js @@ -252,6 +252,11 @@ async function buildFromBootstrap(proxy, baseURL, req, response, preload) { url.searchParams.append("safe_mode", reqUrlSafeMode); } + const enableSidebar = forUrlSearchParams.get("enable_sidebar"); + if (enableSidebar) { + url.searchParams.append("enable_sidebar", enableSidebar); + } + const reqUrlPreviewThemeId = forUrlSearchParams.get("preview_theme_id"); if (reqUrlPreviewThemeId) { url.searchParams.append("preview_theme_id", reqUrlPreviewThemeId); diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 41bd4889bc7..7302867e75b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -640,15 +640,25 @@ class ApplicationController < ActionController::Base def preload_current_user_data store_preloaded( "currentUser", - MultiJson.dump(CurrentUserSerializer.new(current_user, scope: guardian, root: false)), + MultiJson.dump( + CurrentUserSerializer.new( + current_user, + scope: guardian, + root: false, + enable_sidebar_param: params[:enable_sidebar], + ), + ), ) + report = TopicTrackingState.report(current_user) + serializer = ActiveModel::ArraySerializer.new( report, each_serializer: TopicTrackingStateSerializer, scope: guardian, ) + store_preloaded("topicTrackingStates", MultiJson.dump(serializer)) end diff --git a/app/serializers/concerns/user_sidebar_mixin.rb b/app/serializers/concerns/user_sidebar_mixin.rb index b71fc9a1bfb..5f8c5add0a1 100644 --- a/app/serializers/concerns/user_sidebar_mixin.rb +++ b/app/serializers/concerns/user_sidebar_mixin.rb @@ -47,6 +47,6 @@ module UserSidebarMixin private def sidebar_navigation_menu? - !SiteSetting.legacy_navigation_menu? + !SiteSetting.legacy_navigation_menu? || options[:enable_sidebar_param] == "1" end end diff --git a/spec/system/page_objects/components/sidebar.rb b/spec/system/page_objects/components/sidebar.rb new file mode 100644 index 00000000000..06fde5dc8cd --- /dev/null +++ b/spec/system/page_objects/components/sidebar.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module PageObjects + module Components + class Sidebar < PageObjects::Components::Base + def visible? + page.has_css?("#d-sidebar") + end + + def has_category_section_link?(category) + page.has_link?(category.name, class: "sidebar-section-link") + end + end + end +end diff --git a/spec/system/viewing_sidebar_spec.rb b/spec/system/viewing_sidebar_spec.rb new file mode 100644 index 00000000000..4b9da86e9c4 --- /dev/null +++ b/spec/system/viewing_sidebar_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe "Viewing sidebar", type: :system, js: true do + fab!(:admin) { Fabricate(:admin) } + fab!(:user) { Fabricate(:user) } + fab!(:category_sidebar_section_link) { Fabricate(:category_sidebar_section_link, user: user) } + + describe "when using the legacy navigation menu" do + before { SiteSetting.navigation_menu = "legacy" } + + it "should display the sidebar when `enable_sidebar` query param is '1'" do + sign_in(user) + + visit("/latest?enable_sidebar=1") + + sidebar = PageObjects::Components::Sidebar.new + + expect(sidebar).to be_visible + expect(sidebar).to have_category_section_link(category_sidebar_section_link.linkable) + end + end +end