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 +