2024-10-16 01:04:23 +00:00
|
|
|
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>
|
2024-12-24 00:05:51 +00:00
|
|
|
{@page_title}
|
2024-10-16 01:04:23 +00:00
|
|
|
<:actions>
|
|
|
|
<.link
|
|
|
|
class="font-normal text-xs text-blue-600 hover:text-blue-700"
|
|
|
|
navigate={~p"/rooms/#{@room}"}
|
|
|
|
>
|
|
|
|
Back
|
|
|
|
</.link>
|
|
|
|
</:actions>
|
|
|
|
</.header>
|
2024-10-18 11:07:26 +00:00
|
|
|
<.simple_form for={@form} id="room-form" phx-change="validate-room" phx-submit="save-room">
|
2024-10-21 23:54:59 +00:00
|
|
|
<.input field={@form[:name]} type="text" label="Name" phx-debounce />
|
|
|
|
<.input field={@form[:topic]} type="text" label="Topic" phx-debounce />
|
2024-10-16 01:04:23 +00:00
|
|
|
<: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)
|
|
|
|
|
|
|
|
socket =
|
2024-11-17 21:16:52 +00:00
|
|
|
if Chat.joined?(room, socket.assigns.current_user) do
|
|
|
|
changeset = Chat.change_room(room)
|
|
|
|
|
|
|
|
socket
|
|
|
|
|> assign(page_title: "Edit chat room", room: room)
|
|
|
|
|> assign_form(changeset)
|
|
|
|
else
|
|
|
|
socket
|
|
|
|
|> put_flash(:error, "Permission denied")
|
|
|
|
|> push_navigate(to: ~p"/")
|
|
|
|
end
|
2024-10-16 01:04:23 +00:00
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
|
2024-10-18 11:07:26 +00:00
|
|
|
@impl Phoenix.LiveView
|
|
|
|
def handle_event("save-room", %{"room" => room_params}, socket) do
|
|
|
|
socket =
|
|
|
|
case Chat.update_room(socket.assigns.room, room_params) do
|
|
|
|
{:ok, room} ->
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, "Room #{room.name} updated succesfully")
|
|
|
|
|> push_navigate(to: ~p"/rooms/#{room}")
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
assign_form(socket, changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Phoenix.LiveView
|
|
|
|
def handle_event("validate-room", %{"room" => room_params}, socket) do
|
|
|
|
changeset =
|
|
|
|
socket.assigns.room
|
|
|
|
|> Chat.change_room(room_params)
|
|
|
|
|> Map.put(:action, :validate)
|
|
|
|
|
|
|
|
{:noreply, assign_form(socket, changeset)}
|
|
|
|
end
|
|
|
|
|
2024-10-16 01:04:23 +00:00
|
|
|
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
|
|
|
assign(socket, :form, to_form(changeset))
|
|
|
|
end
|
|
|
|
end
|