From 4d327d751b3add0956b0c49c7a141211f54314f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20N=C3=BDvlt?= Date: Thu, 8 Aug 2024 02:31:59 +0200 Subject: [PATCH] WIP --- lib/chore_tracker/accounts/user.ex | 4 ++-- lib/chore_tracker/chores.ex | 2 +- lib/chore_tracker/chores/chore.ex | 2 +- lib/chore_tracker_web/live/chore_live/show.html.heex | 3 ++- lib/chore_tracker_web/live/overview_live.ex | 12 ++++++++++-- priv/repo/seeds.exs | 3 ++- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/chore_tracker/accounts/user.ex b/lib/chore_tracker/accounts/user.ex index b377985..c20b6d4 100644 --- a/lib/chore_tracker/accounts/user.ex +++ b/lib/chore_tracker/accounts/user.ex @@ -8,7 +8,7 @@ defmodule ChoreTracker.Accounts.User do field :hashed_password, :string, redact: true field :current_password, :string, virtual: true, redact: true field :confirmed_at, :utc_datetime - # field :display_name, :string + field :display_name, :string timestamps(type: :utc_datetime) end @@ -38,7 +38,7 @@ defmodule ChoreTracker.Accounts.User do """ def registration_changeset(user, attrs, opts \\ []) do user - |> cast(attrs, [:email, :password]) + |> cast(attrs, [:email, :password, :display_name]) |> validate_email(opts) |> validate_password(opts) end diff --git a/lib/chore_tracker/chores.ex b/lib/chore_tracker/chores.ex index 7437bc1..c5d5e4a 100644 --- a/lib/chore_tracker/chores.ex +++ b/lib/chore_tracker/chores.ex @@ -18,7 +18,7 @@ defmodule ChoreTracker.Chores do """ def list_chores do - Repo.all(Chore) |> Repo.preload(:logs) + from(chore in Chore, preload: [:logs]) |> Repo.all() end @doc """ diff --git a/lib/chore_tracker/chores/chore.ex b/lib/chore_tracker/chores/chore.ex index d1f71e7..a989e4f 100644 --- a/lib/chore_tracker/chores/chore.ex +++ b/lib/chore_tracker/chores/chore.ex @@ -12,7 +12,7 @@ defmodule ChoreTracker.Chores.Chore do timestamps(type: :utc_datetime) - has_many :logs, ChoreTracker.Chores.ChoreLog + has_many :logs, ChoreTracker.Chores.ChoreLog, preload_order: [desc: :inserted_at] end @doc false diff --git a/lib/chore_tracker_web/live/chore_live/show.html.heex b/lib/chore_tracker_web/live/chore_live/show.html.heex index 2659933..7dd023f 100644 --- a/lib/chore_tracker_web/live/chore_live/show.html.heex +++ b/lib/chore_tracker_web/live/chore_live/show.html.heex @@ -42,7 +42,8 @@

Logs

<.table id="chore_logs" rows={@chore.logs}> - <:col :let={chore_log} label="User"><%= chore_log.user.email %> + <:col :let={chore_log} label="User"><%= chore_log.user.display_name %> + <:col :let={chore_log} label="Execution date"><%= chore_log.inserted_at %>
diff --git a/lib/chore_tracker_web/live/overview_live.ex b/lib/chore_tracker_web/live/overview_live.ex index cf7551a..e042dbc 100644 --- a/lib/chore_tracker_web/live/overview_live.ex +++ b/lib/chore_tracker_web/live/overview_live.ex @@ -15,7 +15,9 @@ defmodule ChoreTrackerWeb.OverviewLive do > <:col :let={chore}><%= chore.emoji %> <:col :let={chore} label="Chore"><%= chore.name %> - <:col :let={chore} label="Last executed"><%= chore.logs |> length %> + <:col :let={chore} label="Last executed"> + <%= display_latest_log(chore) %> + <:action :let={chore}> <.link phx-click={JS.push("log_execution", value: %{id: chore.id})} @@ -47,7 +49,13 @@ defmodule ChoreTrackerWeb.OverviewLive do def load_chores(socket) do chores = Chores.list_chores() - dbg(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 diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index cd6feba..442f46e 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -15,7 +15,8 @@ admin_password = ChoreTracker.Helpers.random_string(16) {:ok, admin} = ChoreTracker.Accounts.register_user(%{ email: "admin@jednota.party", - password: admin_password + password: admin_password, + display_name: "Admin" }) ChoreTracker.Chores.create_chore(%{