+ """
+ end
+
+ def handle_event("keydown", _params, socket) do
+ case socket.assigns.live_action do
+ :index -> {:noreply, socket}
+ _ -> {:noreply, push_patch(socket, to: Routes.tasks_path(socket, :index))}
+ end
+ end
+
+ def handle_event("save-task", %{"task" => task_params}, socket) do
+ task = Tasks.get_task!(task_params["id"])
+
+ case Tasks.update_task(task, task_params) do
+ {:ok, task} ->
+ # I suspect splicing in the updated task isn't much faster than just refreshing the whole list
+ socket =
+ socket
+ |> refresh_tasks_and_flash_msg("task \"#{task.value}\" updated")
+
+ {:noreply, push_patch(socket, to: Routes.tasks_path(socket, :show, task.id))}
+
+ {:error, changeset} ->
+ send_update(TaskEditComponent, id: "task_edit:#{task.id}", changeset: changeset)
+ {:noreply, socket}
+ end
+ end
+
+ def handle_event("finish-task", %{"task-id" => task_id}, socket) do
+ {_, task} = Tasks.finish_task_by_id!(task_id)
+ {:noreply, refresh_tasks_and_flash_msg(socket, "task \"#{task.value}\" completed")}
+ end
+
+ def handle_event("delete-task", %{"task-id" => task_id}, socket) do
+ {_, task} = Tasks.delete_task_by_id!(task_id)
+ {:noreply, refresh_tasks_and_flash_msg(socket, "task \"#{task.value}\" deleted")}
+ end
+
+ def handle_event("new-task", %{"task" => task_params}, socket) do
+ case Tasks.add_task(task_params) do
+ {:ok, task} ->
+ socket =
+ socket
+ |> refresh_tasks_and_flash_msg("task \"#{task.value}\" created")
+
+ {:noreply, push_patch(socket, to: Routes.tasks_path(socket, :show, task.id))}
+
+ {:error, %Ecto.Changeset{} = changeset} ->
+ send_update(TasksComponent, id: :all_unfinished_tasks, changeset: changeset)
+ {:noreply, socket}
+ end
+ end
+
+ defp refresh_tasks_and_flash_msg(socket, msg) do
+ socket
+ |> assign(:tasks, Tasks.list_unfinished_tasks())
+ |> put_flash(:info, msg)
+ end
+end
diff --git a/lib/planner_web/router.ex b/lib/planner_web/router.ex
index 4b9e2f1..89dea4d 100644
--- a/lib/planner_web/router.ex
+++ b/lib/planner_web/router.ex
@@ -54,7 +54,11 @@ defmodule PlannerWeb.Router do
live("/", LandingLive, :index)
- resources("/tasks", TaskController)
+ live("/tasks", TasksLive, :index)
+ live("/tasks/:id", TasksLive, :show)
+ live("/tasks/:id/edit", TasksLive, :edit)
+
+ resources("/tasks-old", TaskController)
get("/users/settings", UserSettingsController, :edit)
put("/users/settings/update_password", UserSettingsController, :update_password)
diff --git a/lib/planner_web/templates/layout/root.html.leex b/lib/planner_web/templates/layout/root.html.leex
index 84f3ccd..62e032a 100644
--- a/lib/planner_web/templates/layout/root.html.leex
+++ b/lib/planner_web/templates/layout/root.html.leex
@@ -22,7 +22,7 @@
- <%= link "tasks", to: Routes.task_path(@conn, :index), class: "navbar-item" %>
+ <%= link "tasks", to: Routes.tasks_path(@conn, :index), class: "navbar-item" %>