feat(chat): initial implementation for edit live view

This commit is contained in:
João Paulo Dubas 2024-10-16 01:04:23 +00:00
parent 32350431f8
commit a0d23e5672
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
4 changed files with 63 additions and 1 deletions

View File

@ -20,6 +20,10 @@ defmodule Slax.Chat do
|> Repo.update()
end
def change_room(room, attrs \\ %{}) do
Room.changeset(room, attrs)
end
def get_first_room! do
query = from(r in Room, limit: 1, order_by: [asc: :name])
Repo.one!(query)

View File

@ -26,7 +26,15 @@ defmodule SlaxWeb.ChatRoomLive do
<div class="flex flex-col flex-grow shadow-lg">
<div class="flex justify-between items-center flex-shrink-0 h-16 bg-white border-b border-slate-300 px-4">
<div class="flex flex-col gap-1.5">
<h1 class="text-sm font-bold leading-none">#<%= @room.name %></h1>
<h1 class="text-sm font-bold leading-none">
#<%= @room.name %>
<.link
class="font-normal text-xs text-blue-600 hover:text-blue-700"
navigate={~p"/rooms/#{@room}/edit"}
>
Edit
</.link>
</h1>
<div class="text-xs leading-none h-3.5" phx-click="toggle-topic">
<%= if @hide_topic? do %>
<span class="text-slate-600">[Topic hidden]</span>

View File

@ -0,0 +1,49 @@
defmodule SlaxWeb.ChatRoomLive.Edit do
@moduledoc false
use SlaxWeb, :live_view
alias Slax.Chat
@impl Phoenix.LiveView
def render(assigns) do
~H"""
<div class="mx-auto w-96 mt-12">
<.header>
<%= @page_title %>
<:actions>
<.link
class="font-normal text-xs text-blue-600 hover:text-blue-700"
navigate={~p"/rooms/#{@room}"}
>
Back
</.link>
</:actions>
</.header>
<.simple_form for={@form} id="room-form">
<.input field={@form[:name]} type="text" label="Name" />
<.input field={@form[:topic]} type="text" label="Topic" />
<:actions>
<.button phx-disable-with="Saving..." class="w-full">Save</.button>
</:actions>
</.simple_form>
</div>
"""
end
@impl Phoenix.LiveView
def mount(%{"id" => id}, _session, socket) do
room = Chat.get_room!(id)
changeset = Chat.change_room(room)
socket =
socket
|> assign(page_title: "Edit chat room", room: room)
|> assign_form(changeset)
{:ok, socket}
end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
end

View File

@ -20,6 +20,7 @@ defmodule SlaxWeb.Router do
get "/home", PageController, :home
live "/", ChatRoomLive
live "/rooms/:id", ChatRoomLive
live "/rooms/:id/edit", ChatRoomLive.Edit
end
# Other scopes may use custom stacks.