feat: account live view - basic functionality
This commit is contained in:
parent
4f74d54128
commit
d78d416d89
9 changed files with 389 additions and 42 deletions
|
|
@ -7,9 +7,9 @@ defmodule Mv.Accounts do
|
|||
|
||||
resources do
|
||||
resource Mv.Accounts.User do
|
||||
define :create_user, action: :create
|
||||
define :create_user, action: :create_user
|
||||
define :list_users, action: :read
|
||||
define :update_user, action: :update
|
||||
define :update_user, action: :update_user
|
||||
define :destroy_user, action: :destroy
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -63,6 +63,14 @@ defmodule Mv.Accounts.User do
|
|||
actions do
|
||||
defaults [:read, :create, :destroy, :update]
|
||||
|
||||
create :create_user do
|
||||
accept [:email]
|
||||
end
|
||||
|
||||
update :update_user do
|
||||
accept [:email]
|
||||
end
|
||||
|
||||
read :get_by_subject do
|
||||
description "Get a user by the subject claim in a JWT"
|
||||
argument :subject, :string, allow_nil?: false
|
||||
|
|
|
|||
|
|
@ -7,12 +7,34 @@ defmodule MvWeb.UserLive.Form do
|
|||
<Layouts.app flash={@flash}>
|
||||
<.header>
|
||||
{@page_title}
|
||||
<:subtitle>Use this form to manage user records in your database.</:subtitle>
|
||||
<:subtitle>{gettext("Use this form to manage user records in your database.")}</:subtitle>
|
||||
</.header>
|
||||
|
||||
<.form for={@form} id="user-form" phx-change="validate" phx-submit="save">
|
||||
<.button phx-disable-with="Saving..." variant="primary">Save User</.button>
|
||||
<.button navigate={return_path(@return_to, @user)}>Cancel</.button>
|
||||
<.input field={@form[:email]} label={gettext("Email")} required type="email" />
|
||||
|
||||
<%= if @user do %>
|
||||
<div class="mt-4 p-4 bg-blue-50 rounded-lg">
|
||||
<p class="text-sm text-blue-800">
|
||||
<strong>{gettext("Note")}:</strong> {gettext(
|
||||
"Password can only be changed through authentication functions."
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<% else %>
|
||||
<div class="mt-4 p-4 bg-yellow-50 rounded-lg">
|
||||
<p class="text-sm text-yellow-800">
|
||||
<strong>{gettext("Note")}:</strong> {gettext(
|
||||
"Users created here will need to set their password through the authentication system."
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<.button phx-disable-with={gettext("Saving...")} variant="primary">
|
||||
{gettext("Save User")}
|
||||
</.button>
|
||||
<.button navigate={return_path(@return_to, @user)}>{gettext("Cancel")}</.button>
|
||||
</.form>
|
||||
</Layouts.app>
|
||||
"""
|
||||
|
|
@ -23,11 +45,11 @@ defmodule MvWeb.UserLive.Form do
|
|||
user =
|
||||
case params["id"] do
|
||||
nil -> nil
|
||||
id -> Ash.get!(Mv.Accounts.User, id)
|
||||
id -> Ash.get!(Mv.Accounts.User, id, domain: Mv.Accounts)
|
||||
end
|
||||
|
||||
action = if is_nil(user), do: "New", else: "Edit"
|
||||
page_title = action <> " " <> "User"
|
||||
action = if is_nil(user), do: gettext("New"), else: gettext("Edit")
|
||||
page_title = action <> " " <> gettext("User")
|
||||
|
||||
{:ok,
|
||||
socket
|
||||
|
|
@ -67,9 +89,12 @@ defmodule MvWeb.UserLive.Form do
|
|||
defp assign_form(%{assigns: %{user: user}} = socket) do
|
||||
form =
|
||||
if user do
|
||||
AshPhoenix.Form.for_update(user, :update, as: "user")
|
||||
AshPhoenix.Form.for_update(user, :update_user, domain: Mv.Accounts, as: "user")
|
||||
else
|
||||
AshPhoenix.Form.for_create(Mv.Accounts.User, :create, as: "user")
|
||||
AshPhoenix.Form.for_create(Mv.Accounts.User, :create_user,
|
||||
domain: Mv.Accounts,
|
||||
as: "user"
|
||||
)
|
||||
end
|
||||
|
||||
assign(socket, form: to_form(form))
|
||||
|
|
|
|||
|
|
@ -6,10 +6,10 @@ defmodule MvWeb.UserLive.Index do
|
|||
~H"""
|
||||
<Layouts.app flash={@flash}>
|
||||
<.header>
|
||||
Listing Users
|
||||
{gettext("Listing Users")}
|
||||
<:actions>
|
||||
<.button variant="primary" navigate={~p"/users/new"}>
|
||||
<.icon name="hero-plus" /> New User
|
||||
<.icon name="hero-plus" /> {gettext("New User")}
|
||||
</.button>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
|
@ -19,24 +19,23 @@ defmodule MvWeb.UserLive.Index do
|
|||
rows={@streams.users}
|
||||
row_click={fn {_id, user} -> JS.navigate(~p"/users/#{user}") end}
|
||||
>
|
||||
<:col :let={{_id, user}} label="Id">{user.id}</:col>
|
||||
|
||||
<:col :let={{_id, user}} label="Email">{user.email}</:col>
|
||||
<:col :let={{_id, user}} label={gettext("Email")}>{user.email}</:col>
|
||||
<:col :let={{_id, user}} label={gettext("OIDC ID")}>{user.oidc_id}</:col>
|
||||
|
||||
<:action :let={{_id, user}}>
|
||||
<div class="sr-only">
|
||||
<.link navigate={~p"/users/#{user}"}>Show</.link>
|
||||
<.link navigate={~p"/users/#{user}"}>{gettext("Show")}</.link>
|
||||
</div>
|
||||
|
||||
<.link navigate={~p"/users/#{user}/edit"}>Edit</.link>
|
||||
<.link navigate={~p"/users/#{user}/edit"}>{gettext("Edit")}</.link>
|
||||
</:action>
|
||||
|
||||
<:action :let={{id, user}}>
|
||||
<.link
|
||||
phx-click={JS.push("delete", value: %{id: user.id}) |> hide("##{id}")}
|
||||
data-confirm="Are you sure?"
|
||||
data-confirm={gettext("Are you sure?")}
|
||||
>
|
||||
Delete
|
||||
{gettext("Delete")}
|
||||
</.link>
|
||||
</:action>
|
||||
</.table>
|
||||
|
|
@ -48,14 +47,14 @@ defmodule MvWeb.UserLive.Index do
|
|||
def mount(_params, _session, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Listing Users")
|
||||
|> stream(:users, Ash.read!(Mv.Accounts.User))}
|
||||
|> assign(:page_title, gettext("Listing Users"))
|
||||
|> stream(:users, Ash.read!(Mv.Accounts.User, domain: Mv.Accounts))}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
user = Ash.get!(Mv.Accounts.User, id)
|
||||
Ash.destroy!(user)
|
||||
user = Ash.get!(Mv.Accounts.User, id, domain: Mv.Accounts)
|
||||
Ash.destroy!(user, domain: Mv.Accounts)
|
||||
|
||||
{:noreply, stream_delete(socket, :users, user)}
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,23 +6,26 @@ defmodule MvWeb.UserLive.Show do
|
|||
~H"""
|
||||
<Layouts.app flash={@flash}>
|
||||
<.header>
|
||||
User {@user.id}
|
||||
<:subtitle>This is a user record from your database.</:subtitle>
|
||||
{gettext("User")} {@user.email}
|
||||
<:subtitle>{gettext("This is a user record from your database.")}</:subtitle>
|
||||
|
||||
<:actions>
|
||||
<.button navigate={~p"/users"}>
|
||||
<.icon name="hero-arrow-left" />
|
||||
</.button>
|
||||
<.button variant="primary" navigate={~p"/users/#{@user}/edit?return_to=show"}>
|
||||
<.icon name="hero-pencil-square" /> Edit User
|
||||
<.icon name="hero-pencil-square" /> {gettext("Edit User")}
|
||||
</.button>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
<.list>
|
||||
<:item title="Id">{@user.id}</:item>
|
||||
|
||||
<:item title="Email">{@user.email}</:item>
|
||||
<:item title={gettext("ID")}>{@user.id}</:item>
|
||||
<:item title={gettext("Email")}>{@user.email}</:item>
|
||||
<:item title={gettext("OIDC ID")}>{@user.oidc_id || gettext("Not set")}</:item>
|
||||
<:item title={gettext("Password Authentication")}>
|
||||
{if @user.hashed_password, do: gettext("Enabled"), else: gettext("Not enabled")}
|
||||
</:item>
|
||||
</.list>
|
||||
</Layouts.app>
|
||||
"""
|
||||
|
|
@ -32,7 +35,7 @@ defmodule MvWeb.UserLive.Show do
|
|||
def mount(%{"id" => id}, _session, socket) do
|
||||
{:ok,
|
||||
socket
|
||||
|> assign(:page_title, "Show User")
|
||||
|> assign(:user, Ash.get!(Mv.Accounts.User, id))}
|
||||
|> assign(:page_title, gettext("Show User"))
|
||||
|> assign(:user, Ash.get!(Mv.Accounts.User, id, domain: Mv.Accounts))}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue