Skip to content

Commit

Permalink
refactor: moved rubrics overlay related code from asssessment point d…
Browse files Browse the repository at this point in the history
…etails view to its own live component

- added support to `notify_component` in `LantternWeb.RubricsLive.FormComponent` using `send_update/3`
  • Loading branch information
endoooo committed Nov 7, 2023
1 parent 9f1d25d commit 658017c
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 170 deletions.
129 changes: 17 additions & 112 deletions lib/lanttern_web/live/assessment_point_live/details.ex
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
defmodule LantternWeb.AssessmentPointLive.Details do
use LantternWeb, :live_view
alias Phoenix.PubSub

import LantternWeb.DateTimeHelpers
import LantternWeb.RubricsHelpers

alias Lanttern.Assessments
alias Lanttern.Assessments.AssessmentPointEntry
alias Lanttern.Assessments.Feedback
alias Lanttern.Conversation
alias Lanttern.Conversation.Comment
alias Lanttern.Grading
alias Lanttern.Rubrics
alias Lanttern.Schools.Student

alias LantternWeb.AssessmentPointLive.AssessmentPointEntryEditorComponent
alias LantternWeb.AssessmentPointLive.AssessmentPointUpdateFormComponent
alias LantternWeb.AssessmentPointLive.RubricsOverlayComponent
alias LantternWeb.AssessmentPointLive.FeedbackFormComponent
alias LantternWeb.AssessmentPointLive.FeedbackCommentFormComponent

Expand Down Expand Up @@ -213,10 +211,6 @@ defmodule LantternWeb.AssessmentPointLive.Details do
# lifecycle

def mount(%{"id" => id}, _session, socket) do
if connected?(socket) do
PubSub.subscribe(Lanttern.PubSub, "assessment_point:#{id}")
end

try do
Assessments.get_assessment_point!(id,
preloads: [
Expand Down Expand Up @@ -266,39 +260,6 @@ defmodule LantternWeb.AssessmentPointLive.Details do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end

defp apply_action(socket, :rubrics, _params) do
%{
rubric_id: rubric_id,
scale_id: scale_id
} = socket.assigns.assessment_point

rubric =
case rubric_id do
nil -> nil
rubric_id -> Rubrics.get_full_rubric!(rubric_id)
end

rubric_options =
[
{"Create new rubric", "new"}
| generate_rubric_options(
is_differentiation: false,
scale_id: scale_id
)
]

assessment_point_rubric_form =
%{"rubric_id" => rubric_id}
|> to_form()

socket
|> assign(:rubric, rubric)
|> assign(:is_creating_rubric, false)
|> assign(:has_rubric_change, false)
|> assign(:rubric_options, rubric_options)
|> assign(:assessment_point_rubric_form, assessment_point_rubric_form)
end

defp apply_action(socket, :feedback, %{"student_id" => student_id}) do
feedback =
socket.assigns.entries
Expand Down Expand Up @@ -352,64 +313,10 @@ defmodule LantternWeb.AssessmentPointLive.Details do
|> stream(:comments, comments)
end

defp apply_action(socket, _show_or_edit, _params), do: socket
defp apply_action(socket, _live_action, _params), do: socket

# event handlers

def handle_event("assessment_point_rubric_selected", %{"rubric_id" => ""}, socket) do
has_rubric_change = socket.assigns.assessment_point.rubric_id != nil

socket =
socket
|> assign(:rubric, nil)
|> assign(:has_rubric_change, has_rubric_change)
|> assign(:is_creating_rubric, false)

{:noreply, socket}
end

def handle_event("assessment_point_rubric_selected", %{"rubric_id" => "new"}, socket) do
socket =
socket
|> assign(:is_creating_rubric, true)

{:noreply, socket}
end

def handle_event("assessment_point_rubric_selected", %{"rubric_id" => rubric_id}, socket) do
has_rubric_change = rubric_id != "#{socket.assigns.assessment_point.rubric_id}"

socket =
socket
|> assign(:rubric, Rubrics.get_full_rubric!(rubric_id))
|> assign(:has_rubric_change, has_rubric_change)
|> assign(:is_creating_rubric, false)

{:noreply, socket}
end

def handle_event("save_rubric", %{"rubric_id" => rubric_id}, socket) do
rubric_id = if rubric_id == "", do: nil, else: rubric_id

socket.assigns.assessment_point
|> Assessments.update_assessment_point(%{
rubric_id: rubric_id
})
|> case do
{:ok, _assessment_point} ->
socket =
socket
|> assign(:has_rubric_change, false)
|> assign(:is_creating_rubric, false)
|> update(:assessment_point, &Map.put(&1, :rubric_id, rubric_id))

{:noreply, socket}

{:error, _changeset} ->
{:noreply, put_flash(socket, :error, "Something went wrong")}
end
end

def handle_event("edit_comment", %{"id" => id}, socket) do
comment =
Conversation.get_comment!(
Expand Down Expand Up @@ -506,6 +413,21 @@ defmodule LantternWeb.AssessmentPointLive.Details do
{:noreply, socket}
end

def handle_info({RubricsOverlayComponent, {:rubric_linked, rubric_id}}, socket) do
{:noreply, update(socket, :assessment_point, &Map.put(&1, :rubric_id, rubric_id))}
end

def handle_info({RubricsOverlayComponent, {:new_rubric_linked, _rubric_id}}, socket) do
{:noreply,
push_navigate(socket,
to: ~p"/assessment_points/#{socket.assigns.assessment_point.id}/rubrics"
)}
end

def handle_info({RubricsOverlayComponent, {:error, error_msg}}, socket) do
{:noreply, put_flash(socket, :error, error_msg)}
end

def handle_info({FeedbackFormComponent, {:created, feedback}}, socket) do
socket =
socket
Expand All @@ -515,23 +437,6 @@ defmodule LantternWeb.AssessmentPointLive.Details do
{:noreply, socket}
end

def handle_info({LantternWeb.RubricsLive.FormComponent, {:created, rubric}}, socket) do
assessment_point = socket.assigns.assessment_point

assessment_point
|> Assessments.update_assessment_point(%{
rubric_id: rubric.id
})
|> case do
{:ok, _assessment_point} ->
{:noreply,
push_navigate(socket, to: ~p"/assessment_points/#{assessment_point.id}/rubrics")}

{:error, _changeset} ->
{:noreply, put_flash(socket, :error, "Something went wrong")}
end
end

def handle_info({FeedbackCommentFormComponent, {:created, comment}}, socket) do
socket =
socket
Expand Down
60 changes: 5 additions & 55 deletions lib/lanttern_web/live/assessment_point_live/details.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -103,62 +103,12 @@
</.button>
</:actions>
</.slide_over>
<.slide_over
<.live_component
:if={@live_action == :rubrics}
id="rubrics-overlay"
show={true}
on_cancel={JS.patch(~p"/assessment_points/#{@assessment_point.id}")}
>
<:title>Assessment point rubrics</:title>
<form id="assessment-point-rubric-form" class="flex items-end gap-2" phx-submit="save_rubric">
<.input
field={@assessment_point_rubric_form[:rubric_id]}
type="select"
label="Rubric"
options={@rubric_options}
prompt="No rubric"
phx-change="assessment_point_rubric_selected"
class="flex-1"
/>
<.button
type="submit"
disabled={!@has_rubric_change || @is_creating_rubric}
class={["shrink-0", if(!@has_rubric_change || @is_creating_rubric, do: "hidden")]}
>
Save
</.button>
</form>
<%= if @rubric && !@is_creating_rubric do %>
<h4 class="mt-10 -mb-2 font-display font-black text-xl text-ltrn-subtle">Descriptors</h4>
<div :for={descriptor <- @rubric.descriptors} class="mt-8">
<%= if descriptor.scale_type == "ordinal" do %>
<.badge style_from_ordinal_value={descriptor.ordinal_value}>
<%= descriptor.ordinal_value.name %>
</.badge>
<% else %>
<.badge>
<%= descriptor.score %>
</.badge>
<% end %>
<.markdown class="mt-2" text={descriptor.descriptor} />
</div>
<% end %>
<.live_component
:if={@is_creating_rubric}
module={LantternWeb.RubricsLive.FormComponent}
id={:new}
action={:new}
rubric={
%Lanttern.Rubrics.Rubric{
scale_id: @assessment_point.scale_id,
is_differentiation: false
}
}
hide_diff_and_scale
show_submit
class="mt-6"
/>
</.slide_over>
module={RubricsOverlayComponent}
id={@assessment_point.id}
assessment_point={@assessment_point}
/>
<.slide_over
:if={@live_action == :feedback}
id="feedback-overlay"
Expand Down
Loading

0 comments on commit 658017c

Please sign in to comment.