Use username as fallback

This commit is contained in:
Ondřej 2024-08-09 11:36:32 +02:00
parent e82c9c0bd4
commit 1651d52c18
6 changed files with 12 additions and 5 deletions

View file

@ -376,4 +376,8 @@ defmodule ChoreTracker.Accounts do
{:error, :user, changeset, _} -> {:error, changeset} {:error, :user, changeset, _} -> {:error, changeset}
end end
end end
def display_user(%User{} = user) do
user.display_name || user.username
end
end end

View file

@ -1,5 +1,6 @@
defmodule ChoreTrackerWeb.Layouts do defmodule ChoreTrackerWeb.Layouts do
use ChoreTrackerWeb, :html use ChoreTrackerWeb, :html
alias ChoreTracker.Accounts
embed_templates "layouts/*" embed_templates "layouts/*"
@ -9,7 +10,7 @@ defmodule ChoreTrackerWeb.Layouts do
<nav class="flex items-center gap-4 px-6 justify-end"> <nav class="flex items-center gap-4 px-6 justify-end">
<%= if @current_user do %> <%= if @current_user do %>
<p class="text-sm font-medium mr-2"> <p class="text-sm font-medium mr-2">
<%= @current_user.display_name %> <%= Accounts.display_user(@current_user) %>
</p> </p>
<.header_link href={~p"/users/settings"}> <.header_link href={~p"/users/settings"}>
Settings Settings

View file

@ -113,6 +113,6 @@ defmodule ChoreTrackerWeb.ChoreLive.FormComponent do
|> Enum.map(& &1.data.id) |> Enum.map(& &1.data.id)
Accounts.list_users() Accounts.list_users()
|> Enum.map(&[key: &1.display_name, value: &1.id, selected: &1.id in existing_ids]) |> Enum.map(&[key: Accounts.display_user(&1), value: &1.id, selected: &1.id in existing_ids])
end end
end end

View file

@ -1,7 +1,9 @@
defmodule ChoreTrackerWeb.ChoreLive.Show do defmodule ChoreTrackerWeb.ChoreLive.Show do
alias ChoreTracker.Chores.Chore
use ChoreTrackerWeb, :live_view use ChoreTrackerWeb, :live_view
alias ChoreTracker.Chores alias ChoreTracker.Chores
alias ChoreTracker.Accounts
@impl true @impl true
def mount(_params, _session, socket) do def mount(_params, _session, socket) do

View file

@ -44,7 +44,7 @@
<% else %> <% else %>
<ul> <ul>
<%= for assignee <- @chore.assignees do %> <%= for assignee <- @chore.assignees do %>
<li><%= assignee.display_name %></li> <li><%= Accounts.display_user(assignee) %></li>
<% end %> <% end %>
</ul> </ul>
<% end %> <% end %>
@ -56,7 +56,7 @@
<p>There are no logs</p> <p>There are no logs</p>
<% else %> <% else %>
<.table id="chore_logs" rows={@chore.logs}> <.table id="chore_logs" rows={@chore.logs}>
<:col :let={chore_log} label="User"><%= chore_log.user.display_name %></:col> <:col :let={chore_log} label="User"><%= Accounts.display_user(chore_log.user) %></:col>
<:col :let={chore_log} label="Execution date"> <:col :let={chore_log} label="Execution date">
<span class="flex gap-3"> <span class="flex gap-3">
<span> <span>

View file

@ -38,7 +38,7 @@ defmodule ChoreTrackerWeb.OverviewLive do
<:col :let={chore} label="Next assignee"> <:col :let={chore} label="Next assignee">
<%= case Chores.next_chore_assignee(chore) do <%= case Chores.next_chore_assignee(chore) do
nil -> "No assignee" nil -> "No assignee"
%Accounts.User{} = user -> user.display_name %Accounts.User{} = user -> Accounts.display_user(user)
end %> end %>
</:col> </:col>
<:action :let={chore}> <:action :let={chore}>