From 6d5d1782b5644c3294c2746148a7e3232affec69 Mon Sep 17 00:00:00 2001 From: Vladimir Rubin Date: Fri, 15 Nov 2024 00:45:18 +0200 Subject: [PATCH] feat(frontend): add sorting by subject or date --- README.md | 2 +- lib/exmr_web/live/exam_live/index.ex | 36 ++++++++++++++++--- lib/exmr_web/live/exam_live/index.html.heex | 39 ++++++++++----------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 19d95da..c1f3d14 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ExMR - Exam Managment Resource -To start your Phoenix server: +o start your Phoenix server: - Run `mix setup` to install and setup dependencies - Start Phoenix endpoint with `mix phx.server` or inside IEx with `iex -S mix phx.server` diff --git a/lib/exmr_web/live/exam_live/index.ex b/lib/exmr_web/live/exam_live/index.ex index cb326a6..42716de 100644 --- a/lib/exmr_web/live/exam_live/index.ex +++ b/lib/exmr_web/live/exam_live/index.ex @@ -6,7 +6,12 @@ defmodule ExmrWeb.ExamLive.Index do @impl true def mount(_params, _session, socket) do - {:ok, stream(socket, :exams, Exams.list_exams())} + socket = + socket + |> assign(:exams, Exams.list_exams()) + |> assign(:sort_by, "date") + + {:ok, socket} end @impl true @@ -34,14 +39,37 @@ defmodule ExmrWeb.ExamLive.Index do @impl true def handle_info({ExmrWeb.ExamLive.FormComponent, {:saved, exam}}, socket) do - {:noreply, stream_insert(socket, :exams, exam)} + socket = + socket + |> assign(:exams, [exam | socket.assigns.exams]) + + {:noreply, socket} end @impl true - def handle_event("delete", %{"id" => id}, socket) do + def handle_event("remove", %{"id" => id}, socket) do exam = Exams.get_exam!(id) {:ok, _} = Exams.delete_exam(exam) - {:noreply, stream_delete(socket, :exams, exam)} + socket = + socket + |> update(:exams, fn exams -> + Enum.reject(exams, fn l -> l.id == exam.id end) + end) + + {:noreply, socket} + end + + def handle_event("sort", %{"by" => sort_by}, socket) do + exams = sort_items(socket.assigns.exams, sort_by) + {:noreply, assign(socket, exams: exams, sort_by: sort_by)} + end + + defp sort_items(items, "subject") do + Enum.sort_by(items, & &1.subject) + end + + defp sort_items(items, "date") do + Enum.sort_by(items, & &1.date, &Date.before?/2) end end diff --git a/lib/exmr_web/live/exam_live/index.html.heex b/lib/exmr_web/live/exam_live/index.html.heex index dba8d14..8f9eb3a 100644 --- a/lib/exmr_web/live/exam_live/index.html.heex +++ b/lib/exmr_web/live/exam_live/index.html.heex @@ -1,5 +1,11 @@ <.header> Listing Exams +
+ + | + + +
<:actions> <.link patch={~p"/exams/new"}> <.button>New Exam @@ -7,28 +13,21 @@ -<.table - id="exams" - rows={@streams.exams} - row_click={fn {_id, exam} -> JS.navigate(~p"/exams/#{exam}") end} -> - <:col :let={{_id, exam}} label="Subject"><%= exam.subject %> - <:col :let={{_id, exam}} label="Date"><%= exam.date %> - <:action :let={{_id, exam}}> -
- <.link navigate={~p"/exams/#{exam}"}>Show +
+
+
+
<%= exam.subject %>
+
<%= exam.date %>
- <.link patch={~p"/exams/#{exam}/edit"}>Edit - - <:action :let={{id, exam}}> - <.link - phx-click={JS.push("delete", value: %{id: exam.id}) |> hide("##{id}")} - data-confirm="Are you sure?" + +
+
<.modal :if={@live_action in [:new, :edit]} id="exam-modal" show on_cancel={JS.patch(~p"/exams")}> <.live_component