defmodule PostlandWeb.ProfileLive do use PostlandWeb, :live_view alias Postland.Accounts.User alias Postland.Repo def render(assigns) do ~H"""
<.form for={@form} id="profile-form" phx-change="validate" phx-submit="submit">

<%= @account.name %>

@<%= @account.username %>@<%= @host %> <.input :if={@editing} field={@form[:name]} label="Display Name" />

<%= {:safe, Earmark.as_html!(@account.summary || "")} %>

<.input :if={@editing} field={@form[:summary]} type="textarea" label="Bio" />

<.button :if={!@editing} phx-click="edit">Edit Profile <.button :if={@editing} phx-click="edit" form="profile-form">Save Profile
""" end def mount(_params, _session, socket) do %{host: host} = URI.parse(Postland.my_actor_id()) account = Postland.Accounts.solo_user() form = account |> User.profile_changeset(%{}) |> to_form() {:ok, assign(socket, account: account, host: host, editing: false, form: form)} end def handle_event("edit", _, socket) do {:noreply, assign(socket, :editing, true)} end def handle_event("validate", params, socket) do changeset = User.profile_changeset(socket.assigns.account, params) form = to_form(changeset) {:noreply, assign(socket, form: form)} end def handle_event("submit", %{"user" => params}, socket) do changeset = User.profile_changeset(socket.assigns.account, params) case Repo.update(changeset) do {:ok, account} -> form = account |> User.profile_changeset(%{}) |> to_form() {:noreply, assign(socket, account: account, editing: false, form: form)} {:error, changeset} -> form = to_form(changeset) {:noreply, socket |> put_flash(:error, "An error occurred.") |> assign(form: form)} end end end