diff --git a/lib/lanttern_web/live/assessment_point_live/details.ex b/lib/lanttern_web/live/assessment_point_live/details.ex index 5bb316e7..f2b290df 100644 --- a/lib/lanttern_web/live/assessment_point_live/details.ex +++ b/lib/lanttern_web/live/assessment_point_live/details.ex @@ -363,6 +363,7 @@ defmodule LantternWeb.AssessmentPointLive.Details do socket |> assign(:rubric, nil) |> assign(:has_rubric_change, has_rubric_change) + |> assign(:is_creating_rubric, false) {:noreply, socket} end @@ -382,6 +383,7 @@ defmodule LantternWeb.AssessmentPointLive.Details do socket |> assign(:rubric, Rubrics.get_full_rubric!(rubric_id)) |> assign(:has_rubric_change, has_rubric_change) + |> assign(:is_creating_rubric, false) {:noreply, socket} end @@ -398,6 +400,7 @@ defmodule LantternWeb.AssessmentPointLive.Details do socket = socket |> assign(:has_rubric_change, false) + |> assign(:is_creating_rubric, false) |> update(:assessment_point, &Map.put(&1, :rubric_id, rubric_id)) {:noreply, socket} @@ -512,6 +515,23 @@ 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 diff --git a/lib/lanttern_web/live/assessment_point_live/details.html.heex b/lib/lanttern_web/live/assessment_point_live/details.html.heex index 495bdcd8..0728c65c 100644 --- a/lib/lanttern_web/live/assessment_point_live/details.html.heex +++ b/lib/lanttern_web/live/assessment_point_live/details.html.heex @@ -120,9 +120,15 @@ phx-change="assessment_point_rubric_selected" class="flex-1" /> - <.button :if={@has_rubric_change} type="submit" class="shrink-0">Save + <.button + type="submit" + disabled={!@has_rubric_change || @is_creating_rubric} + class={["shrink-0", if(!@has_rubric_change || @is_creating_rubric, do: "hidden")]} + > + Save + - <%= if @rubric do %> + <%= if @rubric && !@is_creating_rubric do %>
+ + Markdown supported in descriptors <.icon name="hero-information-circle" /> + +
+ """ + end + attr :ordinal_values, :list, required: true attr :ordinal_value_id, :integer, required: true @@ -133,20 +152,38 @@ defmodule LantternWeb.RubricsLive.FormComponent do socket |> assign(:scale_options, scale_options) |> assign(:scale, nil) + |> assign(:hide_diff_and_scale, false) + |> assign(:class, nil) + |> assign(:patch, nil) + |> assign(:show_submit, false) {:ok, socket} end @impl true def update(%{rubric: rubric} = assigns, socket) do - changeset = Rubrics.change_rubric(rubric) - scale = case rubric.scale_id do nil -> nil scale_id -> Grading.get_scale!(scale_id, preloads: :ordinal_values) end + changeset = + case {rubric.id, scale} do + {nil, nil} -> + Rubrics.change_rubric(rubric) + + {nil, scale} -> + # if scale is selected and rubric is new, generate empty descriptors + Rubrics.change_rubric( + rubric, + %{"descriptors" => generate_new_descriptors(scale)} + ) + + _ -> + Rubrics.change_rubric(rubric) + end + {:ok, socket |> assign(assigns) @@ -178,30 +215,7 @@ defmodule LantternWeb.RubricsLive.FormComponent do def handle_event("scale_selected", %{"rubric" => %{"scale_id" => scale_id}}, socket) do scale = Grading.get_scale!(scale_id, preloads: :ordinal_values) - - descriptors = - case scale.type do - "ordinal" -> - scale.ordinal_values - |> Enum.map( - &%{ - scale_id: &1.scale_id, - scale_type: scale.type, - ordinal_value_id: &1.id, - descriptor: "—" - } - ) - - "numeric" -> - %{ - "0" => - blank_numeric_descriptor(scale) - |> Map.put("score", scale.start), - "1" => - blank_numeric_descriptor(scale) - |> Map.put("score", scale.stop) - } - end + descriptors = generate_new_descriptors(scale) changeset = socket.assigns.rubric @@ -275,6 +289,31 @@ defmodule LantternWeb.RubricsLive.FormComponent do save_rubric(socket, socket.assigns.action, rubric_params) end + defp generate_new_descriptors(scale) do + case scale.type do + "ordinal" -> + scale.ordinal_values + |> Enum.map( + &%{ + scale_id: &1.scale_id, + scale_type: scale.type, + ordinal_value_id: &1.id, + descriptor: "—" + } + ) + + "numeric" -> + %{ + "0" => + blank_numeric_descriptor(scale) + |> Map.put("score", scale.start), + "1" => + blank_numeric_descriptor(scale) + |> Map.put("score", scale.stop) + } + end + end + defp blank_numeric_descriptor(scale) do %{ "scale_id" => scale.id, @@ -292,7 +331,7 @@ defmodule LantternWeb.RubricsLive.FormComponent do {:noreply, socket |> put_flash(:info, "Rubric updated successfully") - |> push_patch(to: socket.assigns.patch)} + |> maybe_push_patch(socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign_form(socket, changeset)} @@ -307,13 +346,16 @@ defmodule LantternWeb.RubricsLive.FormComponent do {:noreply, socket |> put_flash(:info, "Rubric created successfully") - |> push_patch(to: socket.assigns.patch)} + |> maybe_push_patch(socket.assigns.patch)} {:error, %Ecto.Changeset{} = changeset} -> {:noreply, assign_form(socket, changeset)} end end + defp maybe_push_patch(socket, nil), do: socket + defp maybe_push_patch(socket, patch_assign), do: push_patch(socket, to: patch_assign) + defp assign_form(socket, %Ecto.Changeset{} = changeset) do assign(socket, :form, to_form(changeset)) end