-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactored `LantternWeb.AssessmentPointsFilterViewOverlayCo…
…mponent` adjusted filter view overlay implementation, based on `mix phx.gen.live` generators pattern. - added "/dashboard/filter_view/new" and "/dashboard/filter_view/:id/edit" routes to handle overlay display - moved `<.slide_over>` component to dashboard live view, reducing the scope of the live component to form only
- Loading branch information
Showing
8 changed files
with
224 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
lib/lanttern_web/live/dashboard_live/filter_view_form_component.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
defmodule LantternWeb.DashboardLive.FilterViewFormComponent do | ||
@moduledoc """ | ||
Assessment points filter view form component. | ||
This form is used inside a `<.slide_over>` component, | ||
where the "submit" button is rendered. | ||
""" | ||
|
||
use LantternWeb, :live_component | ||
alias Lanttern.Explorer | ||
alias Lanttern.Explorer.AssessmentPointsFilterView | ||
|
||
def render(assigns) do | ||
~H""" | ||
<div> | ||
<.form | ||
id="assessment-points-filter-view-form" | ||
for={@form} | ||
phx-change="validate" | ||
phx-submit="save" | ||
phx-target={@myself} | ||
> | ||
<.error_block :if={@form.source.action in [:insert, :update]} class="mb-6"> | ||
Oops, something went wrong! Please check the errors below. | ||
</.error_block> | ||
<.input field={@form[:id]} type="hidden" /> | ||
<.input field={@form[:profile_id]} type="hidden" /> | ||
<.input field={@form[:name]} label="Filter view name" phx-debounce="1500" class="mb-6" /> | ||
<div class="flex gap-6"> | ||
<fieldset class="flex-1"> | ||
<legend class="text-base font-semibold leading-6 text-ltrn-subtle">Classes</legend> | ||
<div class="mt-4 divide-y divide-ltrn-lighter border-b border-t border-ltrn-lighter"> | ||
<.check_field | ||
:for={opt <- @classes} | ||
id={"class-#{opt.id}"} | ||
field={@form[:classes_ids]} | ||
opt={opt} | ||
/> | ||
</div> | ||
</fieldset> | ||
<fieldset class="flex-1"> | ||
<legend class="text-base font-semibold leading-6 text-ltrn-subtle">Subjects</legend> | ||
<div class="mt-4 divide-y divide-ltrn-lighter border-b border-t border-ltrn-lighter"> | ||
<.check_field | ||
:for={opt <- @subjects} | ||
id={"subject-#{opt.id}"} | ||
field={@form[:subjects_ids]} | ||
opt={opt} | ||
/> | ||
</div> | ||
</fieldset> | ||
</div> | ||
</.form> | ||
</div> | ||
""" | ||
end | ||
|
||
# lifecycle | ||
|
||
def mount(socket) do | ||
classes = Lanttern.Schools.list_classes() | ||
subjects = Lanttern.Taxonomy.list_subjects() | ||
|
||
socket = | ||
socket | ||
|> assign(:classes, classes) | ||
|> assign(:subjects, subjects) | ||
|> assign(:action, "create") | ||
|
||
{:ok, socket} | ||
end | ||
|
||
def update(%{filter_view: filter_view} = assigns, socket) do | ||
changeset = | ||
filter_view | ||
|> Map.put(:classes_ids, Enum.map(filter_view.classes, &"#{&1.id}")) | ||
|> Map.put(:subjects_ids, Enum.map(filter_view.subjects, &"#{&1.id}")) | ||
|> Explorer.change_assessment_points_filter_view() | ||
|
||
socket = | ||
socket | ||
|> assign(assigns) | ||
|> assign(:form, to_form(changeset)) | ||
|> assign(:filter_view, filter_view) | ||
|
||
{:ok, socket} | ||
end | ||
|
||
def update(assigns, socket), | ||
do: {:ok, assign(socket, assigns)} | ||
|
||
# event handlers | ||
|
||
def handle_event("validate", %{"assessment_points_filter_view" => params}, socket) do | ||
form = | ||
%AssessmentPointsFilterView{} | ||
|> Explorer.change_assessment_points_filter_view(params) | ||
|> Map.put(:action, :validate) | ||
|> to_form() | ||
|
||
{:noreply, assign(socket, form: form)} | ||
end | ||
|
||
def handle_event("save", %{"assessment_points_filter_view" => params}, socket), | ||
do: save_filter_view(socket, socket.assigns.action, params) | ||
|
||
defp save_filter_view(socket, :new_filter_view, params) do | ||
case Explorer.create_assessment_points_filter_view(params) do | ||
{:ok, assessment_points_filter_view} -> | ||
notify_parent({:created, assessment_points_filter_view}) | ||
{:noreply, socket} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign(socket, form: to_form(changeset))} | ||
end | ||
end | ||
|
||
defp save_filter_view(socket, :edit_filter_view, params) do | ||
# force classes_ids and subjects_ids inclusion to remove filters if needed | ||
params = | ||
params | ||
|> Map.put_new("classes_ids", []) | ||
|> Map.put_new("subjects_ids", []) | ||
|
||
case Explorer.update_assessment_points_filter_view(socket.assigns.filter_view, params) do | ||
{:ok, assessment_points_filter_view} -> | ||
notify_parent({:updated, assessment_points_filter_view}) | ||
{:noreply, socket} | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
{:noreply, assign(socket, form: to_form(changeset))} | ||
end | ||
end | ||
|
||
defp notify_parent(msg), do: send(self(), {__MODULE__, msg}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.