Link to userdate from profile button closes #170 #173

Merged
simon merged 15 commits from 170-userdata-for-profile-button into main 2025-10-16 12:23:28 +02:00
15 changed files with 41 additions and 24 deletions
Showing only changes of commit e3dd333e89 - Show all commits

View file

@ -14,7 +14,8 @@ defmodule MvWeb.Layouts do
embed_templates "layouts/*"
@doc """
Renders the app layout
Renders the app layout. Can be used with or without a current_user.
When current_user is present, it will show the navigation bar.
## Examples
@ -22,18 +23,25 @@ defmodule MvWeb.Layouts do
<h1>Content</h1>
</Layout.app>
<Layouts.app flash={@flash} current_user={@current_user}>
<h1>Authenticated Content</h1>
simon marked this conversation as resolved

I know the h1 tag was there before, but I actually do not get when its rendered. Do you know? Because if its an h1 tag which says authenticated content we should maybe change it but if its not rendered we could also omit?

I know the h1 tag was there before, but I actually do not get when its rendered. Do you know? Because if its an h1 tag which says authenticated content we should maybe change it but if its not rendered we could also omit?

I'm not sure if that's the case with you but for me, the forgejo review UI confused me a bit. I found out that this code is part of an example in a documentation comment, so the h1 you're referring to will never get really rendered, it's just a placeholder to illustrate the functionality.

I'm not sure if that's the case with you but for me, the forgejo review UI confused me a bit. I found out that this code is part of an example in a documentation comment, so the h1 you're referring to will never get really rendered, it's just a placeholder to illustrate the functionality.
</Layout.app>
"""
attr :flash, :map, required: true, doc: "the map of flash messages"
attr :current_user, :map, default: nil, doc: "the current user, if authenticated"
attr :current_scope, :map,
default: nil,
doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)"
default: nil,
doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)"
slot :inner_block, required: true
def app(assigns) do
~H"""
<.navbar />
<%= if @current_user do %>
<.navbar current_user={@current_user} />
<% end %>
<main class="px-4 py-20 sm:px-6 lg:px-16">
<div class="mx-auto max-full space-y-4">
{render_slot(@inner_block)}
@ -44,6 +52,7 @@ defmodule MvWeb.Layouts do
"""
end
@doc """
Shows the flash group with standard titles and content.

View file

@ -4,6 +4,9 @@ defmodule MvWeb.Layouts.Navbar do
"""
use Phoenix.Component
use Gettext, backend: MvWeb.Gettext
use MvWeb, :verified_routes
attr :current_user, :map, required: true, doc: "The current user - navbar is only shown when user is present"
def navbar(assigns) do
~H"""
@ -65,12 +68,14 @@ defmodule MvWeb.Layouts.Navbar do
class="menu menu-sm dropdown-content bg-base-100 rounded-box z-1 mt-3 w-52 p-2 shadow"
>
<li>
<a>
<.link navigate={~p"/users/#{@current_user.id}"}>
{gettext("Profil")}
</a>
</.link>
</li>
<li><a>{gettext("Settings")}</a></li>
<li><a href="sign-out">{gettext("Logout")}</a></li>
<li>
<.link href={~p"/sign-out"}>{gettext("Logout")}</.link>
</li>
</ul>
</div>
</div>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.MemberLive.Form do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{@page_title}
<:subtitle>

View file

@ -1,4 +1,4 @@
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{gettext("Members")}
<:actions>

View file

@ -5,7 +5,7 @@ defmodule MvWeb.MemberLive.Show do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>

As a future possibility (outside of this PR), we could try Phoenix's new scopes feature for this.

As a future possibility (outside of this PR), we could try Phoenix's [new scopes feature](https://hexdocs.pm/phoenix/scopes.html) for this.
<.header>
{@member.first_name} {@member.last_name}
<:subtitle>{gettext("This is a member record from your database.")}</:subtitle>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Form do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{@page_title}
<:subtitle>{gettext("Use this form to manage property records in your database.")}</:subtitle>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Index do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
Listing Properties
<:actions>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyLive.Show do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
Property {@property.id}
<:subtitle>This is a property record from your database.</:subtitle>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Form do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{@page_title}
<:subtitle>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Index do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
Listing Property types
<:actions>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.PropertyTypeLive.Show do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
Property type {@property_type.id}
<:subtitle>This is a property_type record from your database.</:subtitle>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.UserLive.Form do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{@page_title}
<:subtitle>{gettext("Use this form to manage user records in your database.")}</:subtitle>

View file

@ -1,4 +1,4 @@
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{gettext("Listing Users")}
<:actions>

View file

@ -4,7 +4,7 @@ defmodule MvWeb.UserLive.Show do
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<Layouts.app flash={@flash} current_user={@current_user}>
<.header>
{gettext("User")} {@user.email}
<:subtitle>{gettext("This is a user record from your database.")}</:subtitle>

View file

@ -28,11 +28,14 @@ defmodule MvWeb.LiveUserAuth do
end
end
def on_mount(:live_user_required, _params, _session, socket) do
if socket.assigns[:current_user] do
{:cont, socket}
else
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")}
def on_mount(:live_user_required, _params, session, socket) do
socket = AshAuthentication.Phoenix.LiveSession.assign_new_resources(socket, session)
case socket.assigns do
%{current_user: %{} = user} ->
{:cont, assign(socket, :current_user, user)}
_ ->
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")}
end
end