diff --git a/lib/chore_tracker/chores.ex b/lib/chore_tracker/chores.ex index f71dcd5..56bcfab 100644 --- a/lib/chore_tracker/chores.ex +++ b/lib/chore_tracker/chores.ex @@ -5,8 +5,8 @@ defmodule ChoreTracker.Chores do import Ecto.Query, warn: false alias ChoreTracker.Repo - alias ChoreTracker.Chores.Chore + alias ChoreTracker.Accounts.User @doc """ Returns the list of chores. @@ -145,9 +145,10 @@ defmodule ChoreTracker.Chores do {:error, %Ecto.Changeset{}} """ - def create_chore_log(attrs \\ %{}) do + + def log_chore_execution(%User{} = user, %Chore{} = chore) do %ChoreLog{} - |> ChoreLog.changeset(attrs) + |> ChoreLog.changeset(%{user_id: user.id, chore_id: chore.id}) |> Repo.insert() end diff --git a/lib/chore_tracker/chores/chore.ex b/lib/chore_tracker/chores/chore.ex index 87758dd..e8eae1f 100644 --- a/lib/chore_tracker/chores/chore.ex +++ b/lib/chore_tracker/chores/chore.ex @@ -17,6 +17,6 @@ defmodule ChoreTracker.Chores.Chore do def changeset(chore, attrs) do chore |> cast(attrs, [:name, :description, :emoji, :period, :period_unit, :starts_at]) - |> validate_required([:name, :description, :emoji, :period, :period_unit, :starts_at]) + |> validate_required([:name, :emoji, :period, :period_unit, :starts_at]) end end diff --git a/lib/chore_tracker_web/live/chore_live/index.html.heex b/lib/chore_tracker_web/live/chore_live/index.html.heex index e5870b2..26e44a5 100644 --- a/lib/chore_tracker_web/live/chore_live/index.html.heex +++ b/lib/chore_tracker_web/live/chore_live/index.html.heex @@ -12,10 +12,9 @@ rows={@streams.chores} row_click={fn {_id, chore} -> JS.navigate(~p"/manage/chores/#{chore}") end} > - <:col :let={{_id, chore}}><%= chore.emoji %> - <:col :let={{_id, chore}} label="Chore"><%= chore.name %> - <:col :let={{_id, chore}} label="Period"><%= chore.period %> - <:col :let={{_id, chore}} label="Period unit"><%= chore.period_unit %> + <:col :let={{_id, chore}}><%= chore.emoji %> + <:col :let={{_id, chore}} label="Chore"><%= chore.name %> + <:col :let={{_id, chore}} label="Every"><%= chore.period %> <%= chore.period_unit %> <:col :let={{_id, chore}} label="Starts at"><%= chore.starts_at %> <:action :let={{_id, chore}}>
diff --git a/lib/chore_tracker_web/live/overview_live.ex b/lib/chore_tracker_web/live/overview_live.ex index e5a8770..e774260 100644 --- a/lib/chore_tracker_web/live/overview_live.ex +++ b/lib/chore_tracker_web/live/overview_live.ex @@ -1,5 +1,6 @@ defmodule ChoreTrackerWeb.OverviewLive do use ChoreTrackerWeb, :live_view + alias ChoreTracker.Chores def render(assigns) do ~H""" @@ -8,13 +9,40 @@ defmodule ChoreTrackerWeb.OverviewLive do <.table id="chores" rows={@chores}> + <:col :let={chore}><%= chore.emoji %> <:col :let={chore} label="Chore"><%= chore.name %> + <:col :let={chore} label="Last executed">... + <:action :let={chore}> + <.link + phx-click={JS.push("log_execution", value: %{id: chore.id})} + data-confirm="Are you sure?" + > + Log execution + + """ end def mount(_params, _session, socket) do - chores = ChoreTracker.Chores.list_chores() - {:ok, assign(socket, chores: chores)} + {:ok, socket |> load_chores()} + end + + def handle_event("log_execution", %{"id" => chore_id}, socket) do + %{current_user: user} = socket.assigns + chore = Chores.get_chore!(chore_id) + + case Chores.log_chore_execution(user, chore) do + {:ok, _chore_log} -> + {:noreply, + socket + |> put_flash(:info, "Chore execution logged.") + |> load_chores()} + end + end + + def load_chores(socket) do + chores = Chores.list_chores() + assign(socket, chores: chores) end end