From 47d995deba63203089e9d866d77d31b68c9d2b15 Mon Sep 17 00:00:00 2001 From: Eli Knebel Date: Wed, 26 Jun 2024 16:11:03 -0400 Subject: [PATCH 1/2] only show notes tab when either notes are enabled for a single page or course discussions are enabled load notes_enabled and discussions_enabled as live plug add handle_params to discussions to prevent crash if params change --- lib/oli_web/components/delivery/layouts.ex | 15 ++++++++- .../layouts/student_delivery.html.heex | 2 ++ .../live/delivery/student/discussions_live.ex | 26 ++++++++------- lib/oli_web/live_session_plugs/set_sidebar.ex | 33 ++++++++++++++++++- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/lib/oli_web/components/delivery/layouts.ex b/lib/oli_web/components/delivery/layouts.ex index fa3a2c1b3f2..2134b49d829 100644 --- a/lib/oli_web/components/delivery/layouts.ex +++ b/lib/oli_web/components/delivery/layouts.ex @@ -100,6 +100,8 @@ defmodule OliWeb.Components.Delivery.Layouts do attr(:section, Section, default: nil) attr(:active_tab, :atom) attr(:sidebar_expanded, :boolean, default: true) + attr(:notes_enabled, :boolean, default: false) + attr(:discussions_enabled, :boolean, default: false) attr(:preview_mode, :boolean) attr :notification_badges, :map, default: %{} @@ -147,6 +149,8 @@ defmodule OliWeb.Components.Delivery.Layouts do section={@section} preview_mode={@preview_mode} sidebar_expanded={@sidebar_expanded} + notes_enabled={@notes_enabled} + discussions_enabled={@discussions_enabled} notification_badges={@notification_badges} /> @@ -171,7 +175,13 @@ defmodule OliWeb.Components.Delivery.Layouts do " phx-click-away={JS.hide()} > - <.sidebar_links active_tab={@active_tab} section={@section} preview_mode={@preview_mode} /> + <.sidebar_links + active_tab={@active_tab} + section={@section} + preview_mode={@preview_mode} + notes_enabled={@notes_enabled} + discussions_enabled={@discussions_enabled} + />
<.tech_support_button id="mobile-tech-support" ctx={@ctx} /> @@ -214,6 +224,8 @@ defmodule OliWeb.Components.Delivery.Layouts do attr(:active_tab, :atom) attr(:preview_mode, :boolean) attr(:sidebar_expanded, :boolean, default: true) + attr(:notes_enabled, :boolean, default: true) + attr(:discussions_enabled, :boolean, default: true) attr(:notification_badges, :map, default: %{}) def sidebar_links(assigns) do @@ -250,6 +262,7 @@ defmodule OliWeb.Components.Delivery.Layouts do <.nav_link + :if={@notes_enabled || @discussions_enabled} id="discussions_nav_link" href={path_for(:discussions, @section, @preview_mode, @sidebar_expanded)} is_active={@active_tab == :discussions} diff --git a/lib/oli_web/components/layouts/student_delivery.html.heex b/lib/oli_web/components/layouts/student_delivery.html.heex index 673b949f97f..faf08455e34 100644 --- a/lib/oli_web/components/layouts/student_delivery.html.heex +++ b/lib/oli_web/components/layouts/student_delivery.html.heex @@ -17,6 +17,8 @@ preview_mode={@preview_mode} notification_badges={assigns[:notification_badges] || %{}} sidebar_expanded={@sidebar_expanded} + notes_enabled={@notes_enabled} + discussions_enabled={@discussions_enabled} />
true - _ -> false - end - default_posts_params = case socket.assigns[:has_unread_discussions] do true -> Map.merge(@default_params, %{sort_by: "unread", sort_order: :desc}) @@ -77,7 +71,7 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do assign(socket, is_instructor: is_instructor, active_tab: :discussions, - active_sub_tab: :notes, + active_sub_tab: if(socket.assigns.notes_enabled, do: :notes, else: :discussions), posts: posts, notes: notes, expanded_posts: %{}, @@ -87,7 +81,6 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do more_posts_exist?: more_posts_exist?, more_notes_exist?: more_notes_exist?, root_curriculum_resource_id: root_curriculum_resource_id, - course_discussions_enabled?: course_discussions_enabled?, posts_search_term: "", posts_search_results: nil, notes_search_term: "", @@ -97,6 +90,10 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do } end + def handle_params(_params, _uri, socket) do + {:noreply, socket} + end + def handle_event("sort_posts", %{"sort_by" => sort_by}, socket) do updated_post_params = Map.merge(socket.assigns.post_params, %{ @@ -545,9 +542,14 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do class="overflow-x-scroll md:overflow-x-auto flex flex-col py-6 px-16 mb-10 gap-6 items-start" >
- <.tab :if={not @is_instructor} label="My Notes" value={:notes} active={@active_sub_tab} /> <.tab - :if={@course_discussions_enabled?} + :if={@notes_enabled && not @is_instructor} + label="My Notes" + value={:notes} + active={@active_sub_tab} + /> + <.tab + :if={@discussions_enabled} label="Course Discussion" value={:discussions} active={@active_sub_tab} @@ -557,7 +559,7 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do <%= case @active_sub_tab do %> <% :notes -> %> <.notes_section - :if={not @is_instructor} + :if={@notes_enabled && not @is_instructor} ctx={@ctx} section_slug={@section.slug} current_user={@current_user} @@ -569,7 +571,7 @@ defmodule OliWeb.Delivery.Student.DiscussionsLive do /> <% :discussions -> %> <.posts_section - :if={@course_discussions_enabled?} + :if={@discussions_enabled} posts={@posts} ctx={@ctx} section_slug={@section.slug} diff --git a/lib/oli_web/live_session_plugs/set_sidebar.ex b/lib/oli_web/live_session_plugs/set_sidebar.ex index 2f7a2213e73..343c1aa6eff 100644 --- a/lib/oli_web/live_session_plugs/set_sidebar.ex +++ b/lib/oli_web/live_session_plugs/set_sidebar.ex @@ -7,7 +7,15 @@ defmodule OliWeb.LiveSessionPlugs.SetSidebar do import Phoenix.Component, only: [assign: 2] import Phoenix.LiveView, only: [attach_hook: 4, connected?: 1] - def on_mount(:default, _params, _session, socket) do + alias Oli.Resources.Collaboration.CollabSpaceConfig + alias Oli.Resources.Collaboration + alias Oli.Publishing.{DeliveryResolver} + + def on_mount(:default, _params, session, socket) do + socket = + socket + |> assign_notes_and_discussions_enabled(session["section_slug"]) + if connected?(socket) do socket = socket @@ -26,4 +34,27 @@ defmodule OliWeb.LiveSessionPlugs.SetSidebar do |> assign(sidebar_expanded: true)} end end + + defp assign_notes_and_discussions_enabled(socket, section_slug) do + {collab_space_pages_count, _pages_count} = + Collaboration.count_collab_spaces_enabled_in_pages_for_section(section_slug) + + notes_enabled = collab_space_pages_count > 0 + + %{slug: revision_slug} = DeliveryResolver.root_container(section_slug) + + discussions_enabled = + case Collaboration.get_collab_space_config_for_page_in_section( + revision_slug, + section_slug + ) do + {:ok, %CollabSpaceConfig{status: :enabled}} -> + true + + _ -> + false + end + + assign(socket, notes_enabled: notes_enabled, discussions_enabled: discussions_enabled) + end end From f35b78fc88a7bc7d2c50407ec25e1daee46cd806 Mon Sep 17 00:00:00 2001 From: Eli Knebel Date: Thu, 27 Jun 2024 09:52:53 -0400 Subject: [PATCH 2/2] fix tests --- lib/oli_web/live_session_plugs/set_sidebar.ex | 3 +++ test/oli_web/live/delivery/student/learn_live_test.exs | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/oli_web/live_session_plugs/set_sidebar.ex b/lib/oli_web/live_session_plugs/set_sidebar.ex index 343c1aa6eff..2e383664d95 100644 --- a/lib/oli_web/live_session_plugs/set_sidebar.ex +++ b/lib/oli_web/live_session_plugs/set_sidebar.ex @@ -35,6 +35,9 @@ defmodule OliWeb.LiveSessionPlugs.SetSidebar do end end + defp assign_notes_and_discussions_enabled(socket, nil), + do: assign(socket, notes_enabled: false, discussions_enabled: false) + defp assign_notes_and_discussions_enabled(socket, section_slug) do {collab_space_pages_count, _pages_count} = Collaboration.count_collab_spaces_enabled_in_pages_for_section(section_slug) diff --git a/test/oli_web/live/delivery/student/learn_live_test.exs b/test/oli_web/live/delivery/student/learn_live_test.exs index d6c80b01571..2075d7d6820 100644 --- a/test/oli_web/live/delivery/student/learn_live_test.exs +++ b/test/oli_web/live/delivery/student/learn_live_test.exs @@ -2764,8 +2764,14 @@ defmodule OliWeb.Delivery.Student.ContentLiveTest do test "redirects and ensures navigation to the preview Notes page", %{ conn: conn, - section: section + author: author, + section: section, + page_1: page_1, + page_2: page_2, + page_3: page_3 } do + enable_all_sidebar_links(section, author, page_1, page_2, page_3) + stub_current_time(~U[2023-11-04 20:00:00Z]) {:ok, view, _html} = live(conn, "/sections/#{section.slug}/preview")