From 0cbf22b025204ddac50192efe5fa80bd08a38174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Thu, 8 Aug 2024 02:43:58 +0200 Subject: [PATCH] WIP --- lib/chore_tracker/chores.ex | 4 +++- lib/chore_tracker_web/live/overview_live.ex | 12 ++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/chore_tracker/chores.ex b/lib/chore_tracker/chores.ex index c5d5e4a..ebc8624 100644 --- a/lib/chore_tracker/chores.ex +++ b/lib/chore_tracker/chores.ex @@ -4,6 +4,7 @@ defmodule ChoreTracker.Chores do """ import Ecto.Query, warn: false + alias ChoreTracker.Chores.ChoreLog alias ChoreTracker.Repo alias ChoreTracker.Chores.Chore alias ChoreTracker.Accounts.User @@ -18,7 +19,8 @@ defmodule ChoreTracker.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 @doc """ diff --git a/lib/chore_tracker_web/live/overview_live.ex b/lib/chore_tracker_web/live/overview_live.ex index e042dbc..2968c05 100644 --- a/lib/chore_tracker_web/live/overview_live.ex +++ b/lib/chore_tracker_web/live/overview_live.ex @@ -16,7 +16,10 @@ defmodule ChoreTrackerWeb.OverviewLive do <:col :let={chore}><%= chore.emoji %> <:col :let={chore} label="Chore"><%= chore.name %> <:col :let={chore} label="Last executed"> - <%= display_latest_log(chore) %> + <%= case chore.logs do + [log | _] -> log.inserted_at + [] -> "Never" + end %> <:action :let={chore}> <.link @@ -51,11 +54,4 @@ defmodule ChoreTrackerWeb.OverviewLive do chores = Chores.list_chores() assign(socket, chores: chores) end - - def display_latest_log(%Chores.Chore{} = chore) do - case chore.logs |> List.first() do - nil -> "Never" - log -> log.inserted_at - end - end end