Skip to content

Commit

Permalink
Merge pull request #22 from camino-school/liveview-refactor
Browse files Browse the repository at this point in the history
Liveview refactor
  • Loading branch information
endoooo authored Nov 5, 2023
2 parents 9a76673 + ccb7bc7 commit f521b1c
Show file tree
Hide file tree
Showing 57 changed files with 2,793 additions and 2,770 deletions.
12 changes: 8 additions & 4 deletions lib/lanttern/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ defmodule Lanttern.Assessments do

@doc """
Gets a single assessment point.
Optionally preloads associated data.
Raises `Ecto.NoResultsError` if the AssessmentPoint does not exist.
### Options:
`:preloads` – preloads associated data
## Examples
iex> get_assessment_point!(123)
Expand All @@ -61,9 +64,10 @@ defmodule Lanttern.Assessments do
** (Ecto.NoResultsError)
"""
def get_assessment_point!(id, preloads \\ []) do
Repo.get!(AssessmentPoint, id)
|> Repo.preload(preloads)
def get_assessment_point!(id, opts \\ []) do
AssessmentPoint
|> Repo.get!(id)
|> maybe_preload(opts)
end

@doc """
Expand Down
4 changes: 3 additions & 1 deletion lib/lanttern/assessments/assessment_point_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ defmodule Lanttern.Assessments.AssessmentPointEntry do
# we'll need scale for both numeric and ordinal validations
# we are querying the data here to avoid unnecessary queries
assessment_point_id = get_field(changeset, :assessment_point_id)
%{scale: scale} = Lanttern.Assessments.get_assessment_point!(assessment_point_id, :scale)

%{scale: scale} =
Lanttern.Assessments.get_assessment_point!(assessment_point_id, preloads: :scale)

changeset
|> validate_score_value(scale)
Expand Down
35 changes: 31 additions & 4 deletions lib/lanttern/conversation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule Lanttern.Conversation do
### Options:
`:preloads` – preloads associated data
`:feedback_id` – filter comments by feedback
## Examples
Expand All @@ -23,10 +24,29 @@ defmodule Lanttern.Conversation do
"""
def list_comments(opts \\ []) do
Repo.all(Comment)
from(
c in Comment,
order_by: [asc: c.inserted_at]
)
|> maybe_filter_comments_by_feedback(opts)
|> Repo.all()
|> maybe_preload(opts)
end

defp maybe_filter_comments_by_feedback(comments_query, opts) do
case Keyword.get(opts, :feedback_id) do
nil ->
comments_query

feedback_id ->
from(
c in comments_query,
join: f in assoc(c, :feedback),
where: f.id == ^feedback_id
)
end
end

@doc """
Gets a single comment.
Expand All @@ -53,6 +73,10 @@ defmodule Lanttern.Conversation do
@doc """
Creates a comment.
### Options:
`:preloads` – preloads associated data
## Examples
iex> create_comment(%{field: value})
Expand All @@ -62,10 +86,11 @@ defmodule Lanttern.Conversation do
{:error, %Ecto.Changeset{}}
"""
def create_comment(attrs \\ %{}) do
def create_comment(attrs \\ %{}, opts \\ []) do
%Comment{}
|> Comment.changeset(attrs)
|> Repo.insert()
|> maybe_preload(opts)
end

@doc """
Expand Down Expand Up @@ -125,6 +150,8 @@ defmodule Lanttern.Conversation do
If `mark_feedback_id_for_completion` is present in `comment_attrs`,
will add the created comment to `Feedback`'s `completion_comment`.
See `create_comment/2` for `opts`.
## Examples
iex> create_feedback_comment(%{comment: "good comment", profile_id: 1}, 1)
Expand All @@ -137,10 +164,10 @@ defmodule Lanttern.Conversation do
{:error, "Feedback not found"}
"""
def create_feedback_comment(comment_attrs, feedback_id) do
def create_feedback_comment(comment_attrs, feedback_id, opts \\ []) do
Repo.transaction(fn ->
comment =
case create_comment(comment_attrs) do
case create_comment(comment_attrs, opts) do
{:ok, comment} -> comment
{:error, error_changeset} -> Repo.rollback(error_changeset)
end
Expand Down
7 changes: 6 additions & 1 deletion lib/lanttern/conversation/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ defmodule Lanttern.Conversation.Comment do

belongs_to :profile, Lanttern.Identity.Profile

many_to_many :feedback, Lanttern.Assessments.Feedback,
join_through: "feedback_comments",
on_replace: :delete

timestamps()
end

Expand All @@ -39,7 +43,8 @@ defmodule Lanttern.Conversation.Comment do
end

defp handle_feedback_completion(true, changeset) do
feedback_id = get_change(changeset, :feedback_id_for_completion)
feedback_id =
get_field(changeset, :feedback_id_for_completion)

case Repo.get(Lanttern.Assessments.Feedback, feedback_id) do
nil ->
Expand Down
5 changes: 3 additions & 2 deletions lib/lanttern/explorer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Lanttern.Explorer do
end

@doc """
Creates a assessment_points_filter_view.
Creates an assessment_points_filter_view.
## Examples
Expand All @@ -78,7 +78,8 @@ defmodule Lanttern.Explorer do
"""
def create_assessment_points_filter_view(attrs \\ %{}) do
%AssessmentPointsFilterView{}
# add classes and subjects to force return with preloaded classes/subjects
%AssessmentPointsFilterView{classes: [], subjects: []}
|> AssessmentPointsFilterView.changeset(attrs)
|> Repo.insert()
end
Expand Down
20 changes: 20 additions & 0 deletions lib/lanttern_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,26 @@ defmodule LantternWeb.CoreComponents do
"""
end

attr :profile_name, :string, required: true
attr :class, :any, default: nil
attr :id, :string, default: nil
attr :rest, :global
slot :inner_block, required: true

@doc """
Renders a block with a profile icon.
"""
def user_icon_block(assigns) do
~H"""
<div id={@id} class={["flex gap-4", @class]} {@rest}>
<.profile_icon profile_name={@profile_name} class="shrink-0" />
<div class="flex-1">
<%= render_slot(@inner_block) %>
</div>
</div>
"""
end

@doc """
Renders a ping.
"""
Expand Down
1 change: 1 addition & 0 deletions lib/lanttern_web/components/form_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ defmodule LantternWeb.FormComponents do
id={@id}
class="peer block w-full border-0 bg-transparent p-4 placeholder:text-ltrn-subtle focus:ring-0"
placeholder={@label}
{@rest}
><%= Phoenix.HTML.Form.normalize_value("textarea", @value) %></textarea>
<div class={[
"flex items-center justify-between gap-6 w-full p-2 border-t",
Expand Down
4 changes: 2 additions & 2 deletions lib/lanttern_web/controllers/admin_html/home.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
<:item link={~p"/admin/schools"}>Schools</:item>
<:item link={~p"/admin/classes"}>Classes</:item>
<:item link={~p"/admin/students"}>Students</:item>
<:item link={~p"/admin/students_import"}>Import students</:item>
<:item link={~p"/admin/import_students"}>Import students</:item>
<:item link={~p"/admin/teachers"}>Teachers</:item>
<:item link={~p"/admin/teachers_import"}>Import teachers</:item>
<:item link={~p"/admin/import_teachers"}>Import teachers</:item>
</.link_list>
<.link_list title="Taxonomy">
<:item link={~p"/admin/subjects"}>Subjects</:item>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LantternWeb.AssessmentPointsFilterViewLive.FormComponent do
defmodule LantternWeb.Admin.AssessmentPointsFilterViewLive.FormComponent do
use LantternWeb, :live_component

alias Lanttern.Explorer
Expand All @@ -12,7 +12,9 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.FormComponent do
<div>
<.header>
<%= @title %>
<:subtitle>Use this form to manage assessment_points_filter_view records in your database.</:subtitle>
<:subtitle>
Use this form to manage assessment_points_filter_view records in your database.
</:subtitle>
</.header>
<.simple_form
Expand Down Expand Up @@ -71,7 +73,10 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.FormComponent do
changeset =
assessment_points_filter_view
|> Explorer.change_assessment_points_filter_view()
|> Ecto.Changeset.cast(%{subjects_ids: subjects_ids, classes_ids: classes_ids}, [:subjects_ids, :classes_ids])
|> Ecto.Changeset.cast(%{subjects_ids: subjects_ids, classes_ids: classes_ids}, [
:subjects_ids,
:classes_ids
])

profile_options = generate_profile_options()
subject_options = generate_subject_options()
Expand All @@ -87,7 +92,11 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.FormComponent do
end

@impl true
def handle_event("validate", %{"assessment_points_filter_view" => assessment_points_filter_view_params}, socket) do
def handle_event(
"validate",
%{"assessment_points_filter_view" => assessment_points_filter_view_params},
socket
) do
changeset =
socket.assigns.assessment_points_filter_view
|> Explorer.change_assessment_points_filter_view(assessment_points_filter_view_params)
Expand All @@ -96,12 +105,23 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.FormComponent do
{:noreply, assign_form(socket, changeset)}
end

def handle_event("save", %{"assessment_points_filter_view" => assessment_points_filter_view_params}, socket) do
save_assessment_points_filter_view(socket, socket.assigns.action, assessment_points_filter_view_params)
def handle_event(
"save",
%{"assessment_points_filter_view" => assessment_points_filter_view_params},
socket
) do
save_assessment_points_filter_view(
socket,
socket.assigns.action,
assessment_points_filter_view_params
)
end

defp save_assessment_points_filter_view(socket, :edit, assessment_points_filter_view_params) do
case Explorer.update_assessment_points_filter_view(socket.assigns.assessment_points_filter_view, assessment_points_filter_view_params) do
case Explorer.update_assessment_points_filter_view(
socket.assigns.assessment_points_filter_view,
assessment_points_filter_view_params
) do
{:ok, assessment_points_filter_view} ->
notify_parent({:saved, assessment_points_filter_view})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule LantternWeb.AssessmentPointsFilterViewLive.Index do
defmodule LantternWeb.Admin.AssessmentPointsFilterViewLive.Index do
use LantternWeb, {:live_view, layout: :admin}

alias Lanttern.Explorer
Expand All @@ -7,7 +7,9 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.Index do
@impl true
def mount(_params, _session, socket) do
assessment_points_filter_views =
Explorer.list_assessment_points_filter_views(preloads: [:classes, :subjects, profile: [:teacher, :student]])
Explorer.list_assessment_points_filter_views(
preloads: [:classes, :subjects, profile: [:teacher, :student]]
)

{:ok, stream(socket, :assessment_points_filter_views, assessment_points_filter_views)}
end
Expand All @@ -20,7 +22,12 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.Index do
defp apply_action(socket, :edit, %{"id" => id}) do
socket
|> assign(:page_title, "Edit Assessment points filter view")
|> assign(:assessment_points_filter_view, Explorer.get_assessment_points_filter_view!(id, preloads: [:classes, :subjects, profile: [:teacher, :student]]))
|> assign(
:assessment_points_filter_view,
Explorer.get_assessment_points_filter_view!(id,
preloads: [:classes, :subjects, profile: [:teacher, :student]]
)
)
end

defp apply_action(socket, :new, _params) do
Expand All @@ -36,19 +43,27 @@ defmodule LantternWeb.AssessmentPointsFilterViewLive.Index do
end

@impl true
def handle_info({LantternWeb.AssessmentPointsFilterViewLive.FormComponent, {:saved, assessment_points_filter_view}}, socket) do
def handle_info(
{LantternWeb.Admin.AssessmentPointsFilterViewLive.FormComponent,
{:saved, assessment_points_filter_view}},
socket
) do
assessment_points_filter_view =
Explorer.get_assessment_points_filter_view!(assessment_points_filter_view.id, preloads: [:classes, :subjects, profile: [:teacher, :student]])
Explorer.get_assessment_points_filter_view!(assessment_points_filter_view.id,
preloads: [:classes, :subjects, profile: [:teacher, :student]]
)

{:noreply, stream_insert(socket, :assessment_points_filter_views, assessment_points_filter_view)}
{:noreply,
stream_insert(socket, :assessment_points_filter_views, assessment_points_filter_view)}
end

@impl true
def handle_event("delete", %{"id" => id}, socket) do
assessment_points_filter_view = Explorer.get_assessment_points_filter_view!(id)
{:ok, _} = Explorer.delete_assessment_points_filter_view(assessment_points_filter_view)

{:noreply, stream_delete(socket, :assessment_points_filter_views, assessment_points_filter_view)}
{:noreply,
stream_delete(socket, :assessment_points_filter_views, assessment_points_filter_view)}
end

def profile_name(%{type: "teacher"} = profile),
Expand Down
Loading

0 comments on commit f521b1c

Please sign in to comment.