diff --git a/lib/planner/tasks.ex b/lib/planner/tasks.ex index 946189c..c64d00b 100644 --- a/lib/planner/tasks.ex +++ b/lib/planner/tasks.ex @@ -8,9 +8,19 @@ defmodule Planner.Tasks do alias Planner.Tasks.Plan alias Planner.Tasks.PlanDetail - def list_all_tasks, do: Repo.all(Task) + def list_unfiled_tasks("true") do + filed_ids = from(pd in PlanDetail, select: pd.task_id) - def list_unfiled_tasks do + from( + t in Task, + where: t.id not in subquery(filed_ids), + order_by: [desc: t.updated_at] + ) + |> Repo.all() + |> Repo.preload(:plans) + end + + def list_unfiled_tasks(_done) do filed_ids = from(pd in PlanDetail, select: pd.task_id) from( @@ -22,7 +32,16 @@ defmodule Planner.Tasks do |> Repo.preload(:plans) end - def list_unfinished_tasks do + def list_unfinished_tasks("true") do + from( + t in Task, + order_by: [desc: t.updated_at] + ) + |> Repo.all() + |> Repo.preload(:plans) + end + + def list_unfinished_tasks(_done) do from( t in Task, where: is_nil(t.finished_at), @@ -32,7 +51,23 @@ defmodule Planner.Tasks do |> Repo.preload(:plans) end - def list_unfinished_tasks_by_plan_id(plan_id, task_id \\ nil) do + def list_tasks_by_plan_id("true", plan_id, task_id) do + q = + Ecto.Query.from( + t in Task, + join: pd in PlanDetail, + on: t.id == pd.task_id, + where: (pd.plan_id == ^plan_id) + or + (pd.plan_id == ^plan_id and t.id == ^task_id), + order_by: [desc: t.updated_at] + ) + + Repo.all(q) + |> Repo.preload(:plans) + end + + def list_tasks_by_plan_id(_done, plan_id, task_id) do q = Ecto.Query.from( t in Task, diff --git a/lib/planner_web/live/tasks_live.ex b/lib/planner_web/live/tasks_live.ex index 5aee936..e668c2e 100644 --- a/lib/planner_web/live/tasks_live.ex +++ b/lib/planner_web/live/tasks_live.ex @@ -4,23 +4,25 @@ defmodule PlannerWeb.TasksLive do alias Planner.Tasks alias Planner.Tasks.Plan - def mount(_params, _session, socket) do + def mount(params, _session, socket) do + done = Map.get(params, "done", "default_value") socket = - socket - |> assign(:plans, Tasks.list_unfinished_plans()) - |> assign(:plan_changeset, Tasks.change_plan(%Plan{})) + socket + |> assign(:plans, Tasks.list_unfinished_plans()) + |> assign(:plan_changeset, Tasks.change_plan(%Plan{})) + |> assign(:include_done, done) {:ok, socket} end # plan: yes, task: yes - def handle_params(%{"plan_id" => plan_id, "task_id" => task_id}, _, socket) do + def handle_params(%{"plan_id" => plan_id, "task_id" => task_id} = params, _, socket) do case Tasks.verify_plan_id_from_url(plan_id) and Tasks.verify_task_id_from_url(task_id) do true -> socket = socket |> assign(:active_task, task_id) |> assign(:active_plan, Tasks.get_plan!(plan_id)) - |> assign(:tasks, Tasks.list_unfinished_tasks_by_plan_id(plan_id, task_id)) + |> assign(:tasks, Tasks.list_tasks_by_plan_id(socket.assigns.include_done, plan_id, task_id)) |> add_plan_routes(plan_id) {:noreply, socket} @@ -31,14 +33,14 @@ defmodule PlannerWeb.TasksLive do end # plan: no, task: yes - def handle_params(%{"task_id" => task_id}, _, socket) do + def handle_params(%{"task_id" => task_id} = params, _, socket) do case Tasks.verify_task_id_from_url(task_id) do true -> socket = socket |> assign(:active_task, task_id) |> assign(:active_plan, nil) - |> assign(:tasks, Tasks.list_unfiled_tasks()) + |> assign(:tasks, Tasks.list_unfiled_tasks(socket.assigns.include_done)) |> add_task_routes() {:noreply, assign(socket, :active_task, task_id)} @@ -49,14 +51,14 @@ defmodule PlannerWeb.TasksLive do end # plan: yes, task: no - def handle_params(%{"plan_id" => plan_id}, _, socket) do + def handle_params(%{"plan_id" => plan_id} = params, _, socket) do case Tasks.verify_plan_id_from_url(plan_id) do true -> socket = socket |> assign(:active_task, nil) |> assign(:active_plan, Tasks.get_plan!(plan_id)) - |> assign(:tasks, Tasks.list_unfinished_tasks_by_plan_id(plan_id)) + |> assign(:tasks, Tasks.list_tasks_by_plan_id(socket.assigns.include_done, plan_id, nil)) |> add_plan_routes(plan_id) {:noreply, socket} @@ -67,12 +69,12 @@ defmodule PlannerWeb.TasksLive do end # plan: no, task: no - def handle_params(_, _, socket) do + def handle_params(params, _, socket) do socket = socket |> assign(:active_task, nil) |> assign(:active_plan, nil) - |> assign(:tasks, Tasks.list_unfiled_tasks()) + |> assign(:tasks, Tasks.list_unfiled_tasks(socket.assigns.include_done)) |> add_task_routes() {:noreply, socket} @@ -108,6 +110,7 @@ defmodule PlannerWeb.TasksLive do