This commit is contained in:
Ondřej 2024-08-08 01:10:55 +02:00
parent 0468b926c7
commit ca5546c35e
4 changed files with 38 additions and 10 deletions

View file

@ -5,8 +5,8 @@ defmodule ChoreTracker.Chores do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias ChoreTracker.Repo alias ChoreTracker.Repo
alias ChoreTracker.Chores.Chore alias ChoreTracker.Chores.Chore
alias ChoreTracker.Accounts.User
@doc """ @doc """
Returns the list of chores. Returns the list of chores.
@ -145,9 +145,10 @@ defmodule ChoreTracker.Chores do
{:error, %Ecto.Changeset{}} {:error, %Ecto.Changeset{}}
""" """
def create_chore_log(attrs \\ %{}) do
def log_chore_execution(%User{} = user, %Chore{} = chore) do
%ChoreLog{} %ChoreLog{}
|> ChoreLog.changeset(attrs) |> ChoreLog.changeset(%{user_id: user.id, chore_id: chore.id})
|> Repo.insert() |> Repo.insert()
end end

View file

@ -17,6 +17,6 @@ defmodule ChoreTracker.Chores.Chore do
def changeset(chore, attrs) do def changeset(chore, attrs) do
chore chore
|> cast(attrs, [:name, :description, :emoji, :period, :period_unit, :starts_at]) |> 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
end end

View file

@ -12,10 +12,9 @@
rows={@streams.chores} rows={@streams.chores}
row_click={fn {_id, chore} -> JS.navigate(~p"/manage/chores/#{chore}") end} row_click={fn {_id, chore} -> JS.navigate(~p"/manage/chores/#{chore}") end}
> >
<:col :let={{_id, chore}}><%= chore.emoji %></:col> <:col :let={{_id, chore}}><span class="text-2xl"><%= chore.emoji %></span></:col>
<:col :let={{_id, chore}} label="Chore"><%= chore.name %></:col> <:col :let={{_id, chore}} label="Chore"><strong><%= chore.name %></strong></:col>
<:col :let={{_id, chore}} label="Period"><%= chore.period %></:col> <:col :let={{_id, chore}} label="Every"><%= chore.period %> <%= chore.period_unit %></:col>
<:col :let={{_id, chore}} label="Period unit"><%= chore.period_unit %></:col>
<:col :let={{_id, chore}} label="Starts at"><%= chore.starts_at %></:col> <:col :let={{_id, chore}} label="Starts at"><%= chore.starts_at %></:col>
<:action :let={{_id, chore}}> <:action :let={{_id, chore}}>
<div class="sr-only"> <div class="sr-only">

View file

@ -1,5 +1,6 @@
defmodule ChoreTrackerWeb.OverviewLive do defmodule ChoreTrackerWeb.OverviewLive do
use ChoreTrackerWeb, :live_view use ChoreTrackerWeb, :live_view
alias ChoreTracker.Chores
def render(assigns) do def render(assigns) do
~H""" ~H"""
@ -8,13 +9,40 @@ defmodule ChoreTrackerWeb.OverviewLive do
</.header> </.header>
<.table id="chores" rows={@chores}> <.table id="chores" rows={@chores}>
<:col :let={chore}><span class="text-2xl"><%= chore.emoji %></span></:col>
<:col :let={chore} label="Chore"><%= chore.name %></:col> <:col :let={chore} label="Chore"><%= chore.name %></:col>
<:col :let={chore} label="Last executed">...</:col>
<:action :let={chore}>
<.link
phx-click={JS.push("log_execution", value: %{id: chore.id})}
data-confirm="Are you sure?"
>
Log execution
</.link>
</:action>
</.table> </.table>
""" """
end end
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
chores = ChoreTracker.Chores.list_chores() {:ok, socket |> load_chores()}
{:ok, assign(socket, chores: 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
end end