Add helper methods to create the tuples `{:ok, socket}` and `{:noreply, socket}`, and make them available in `SlaxWeb` module.
78 lines
1.8 KiB
Elixir
78 lines
1.8 KiB
Elixir
defmodule SlaxWeb.ChatRoomLive.Edit do
|
|
@moduledoc false
|
|
use SlaxWeb, :live_view
|
|
|
|
import SlaxWeb.RoomComponents
|
|
|
|
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>
|
|
<.room_form form={@form} />
|
|
</div>
|
|
"""
|
|
end
|
|
|
|
@impl Phoenix.LiveView
|
|
def mount(%{"id" => id}, _session, socket) do
|
|
room = Chat.get_room!(id)
|
|
|
|
socket =
|
|
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
|
|
|
|
ok(socket)
|
|
end
|
|
|
|
@impl Phoenix.LiveView
|
|
def handle_event("save-room", %{"room" => room_params}, socket) do
|
|
socket.assigns.room
|
|
|> Chat.update_room(room_params)
|
|
|> case 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()
|
|
end
|
|
|
|
@impl Phoenix.LiveView
|
|
def handle_event("validate-room", %{"room" => room_params}, socket) do
|
|
socket.assigns.room
|
|
|> Chat.change_room(room_params)
|
|
|> Map.put(:action, :validate)
|
|
|> then(&assign_form(socket, &1))
|
|
|> noreply()
|
|
end
|
|
|
|
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
|
assign(socket, :form, to_form(changeset))
|
|
end
|
|
end
|