This commit is contained in:
Ondřej 2024-08-08 02:43:58 +02:00
parent 4d327d751b
commit 0cbf22b025
2 changed files with 7 additions and 9 deletions

View file

@ -4,6 +4,7 @@ defmodule ChoreTracker.Chores do
""" """
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias ChoreTracker.Chores.ChoreLog
alias ChoreTracker.Repo alias ChoreTracker.Repo
alias ChoreTracker.Chores.Chore alias ChoreTracker.Chores.Chore
alias ChoreTracker.Accounts.User alias ChoreTracker.Accounts.User
@ -18,7 +19,8 @@ defmodule ChoreTracker.Chores do
""" """
def list_chores do def list_chores do
from(chore in Chore, preload: [:logs]) |> Repo.all() logs = from(log in ChoreLog, limit: 1)
from(chore in Chore, preload: [logs: ^logs]) |> Repo.all()
end end
@doc """ @doc """

View file

@ -16,7 +16,10 @@ defmodule ChoreTrackerWeb.OverviewLive do
<:col :let={chore}><span class="text-2xl"><%= chore.emoji %></span></:col> <: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 :let={chore} label="Last executed">
<%= display_latest_log(chore) %> <%= case chore.logs do
[log | _] -> log.inserted_at
[] -> "Never"
end %>
</:col> </:col>
<:action :let={chore}> <:action :let={chore}>
<.link <.link
@ -51,11 +54,4 @@ defmodule ChoreTrackerWeb.OverviewLive do
chores = Chores.list_chores() chores = Chores.list_chores()
assign(socket, chores: chores) assign(socket, chores: chores)
end end
def display_latest_log(%Chores.Chore{} = chore) do
case chore.logs |> List.first() do
nil -> "Never"
log -> log.inserted_at
end
end
end end