basically working for one user (no user/all users actually)

This commit is contained in:
Thelonius Kort
2020-07-13 18:53:18 +02:00
parent 24fcc831af
commit 7e5d8ebc2c
3 changed files with 51 additions and 2 deletions

View File

@ -0,0 +1,38 @@
defmodule ClipWeb.BoardLive do
use ClipWeb, :live_view
@impl true
def render(assigns) do
~L"""
<form phx-change="paste">
<input type="text" name="snippet" value="<%= @snippet %>" data-updated-val="<%= @snippet %>" phx-hook="SnippetInput" autocomplete="off"/>
</form>
Current content: <%= @snippet %>
<button phx-click="normalize">
"""
end
@impl true
def mount(_params, _session, socket) do
Phoenix.PubSub.subscribe(Clip.PubSub, "everybody") # actually only for single user
{:ok, assign(socket, snippet: "")}
end
@impl true
def handle_event("normalize", _, %{"snippet" => snippet} = socket) do
Phoenix.PubSub.broadcast(Clip.PubSub, "everybody", {:snippet_pasted, %{"snippet" => snippet}})
{:noreply, assign(socket, snippet: snippet)}
end
@impl true
def handle_event("paste", %{"snippet" => snippet}, socket) do
Phoenix.PubSub.broadcast(Clip.PubSub, "everybody", {:snippet_pasted, %{"snippet" => snippet}})
{:noreply, assign(socket, snippet: snippet)}
end
@impl true
def handle_info({:snippet_pasted, %{"snippet" => snippet}}, socket) do
{:noreply, assign(socket, snippet: snippet)}
end
end