making the snippet private to the logged in user
This commit is contained in:
@ -1,21 +1,24 @@
|
||||
defmodule ClipWeb.BoardLive do
|
||||
use ClipWeb, :live_view
|
||||
|
||||
alias Clip.Accounts
|
||||
|
||||
@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"/>
|
||||
<textarea type="text" name="snippet" data-updated-val="<%= @snippet %>" phx-hook="SnippetInput" autocomplete="off"><%= @snippet %></textarea>
|
||||
</form>
|
||||
Current content: <%= @snippet %>
|
||||
<button phx-click="normalize">
|
||||
Current content: <%= @snippet %><br>
|
||||
<button phx-click="normalize">-> 004930112</button>
|
||||
"""
|
||||
end
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
Phoenix.PubSub.subscribe(Clip.PubSub, "everybody") # actually only for single user
|
||||
{:ok, assign(socket, snippet: "")}
|
||||
def mount(_params, session, socket) do
|
||||
user = Accounts.get_user_by_session_token(session |> Map.get("user_token"))
|
||||
Phoenix.PubSub.subscribe(Clip.PubSub, user.email)
|
||||
{:ok, assign(socket, snippet: "", current_user: user)}
|
||||
end
|
||||
|
||||
defp normalize(pnumber, local_pref \\ "0351", country_pref \\ "0049") do
|
||||
@ -28,15 +31,15 @@ defmodule ClipWeb.BoardLive do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("normalize", _, %{assigns: %{snippet: snippet}} = socket) do
|
||||
def handle_event("normalize", _, %{assigns: %{snippet: snippet, current_user: user}} = socket) do
|
||||
norm_snipp = normalize(snippet)
|
||||
Phoenix.PubSub.broadcast(Clip.PubSub, "everybody", {:snippet_pasted, %{snippet: norm_snipp}})
|
||||
Phoenix.PubSub.broadcast(Clip.PubSub, user.email, {:snippet_pasted, %{snippet: norm_snipp}})
|
||||
{: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}})
|
||||
def handle_event("paste", %{"snippet" => snippet}, %{assigns: %{current_user: user}} = socket) do
|
||||
Phoenix.PubSub.broadcast(Clip.PubSub, user.email, {:snippet_pasted, %{snippet: snippet}})
|
||||
{:noreply, assign(socket, snippet: snippet)}
|
||||
end
|
||||
|
||||
|
||||
@ -17,13 +17,6 @@ defmodule ClipWeb.Router do
|
||||
plug :accepts, ["json"]
|
||||
end
|
||||
|
||||
scope "/", ClipWeb do
|
||||
pipe_through :browser
|
||||
|
||||
# live "/", PageLive, :index
|
||||
live "/", BoardLive, :index
|
||||
end
|
||||
|
||||
# Other scopes may use custom stacks.
|
||||
# scope "/api", ClipWeb do
|
||||
# pipe_through :api
|
||||
@ -63,6 +56,8 @@ defmodule ClipWeb.Router do
|
||||
scope "/", ClipWeb do
|
||||
pipe_through [:browser, :require_authenticated_user]
|
||||
|
||||
live "/", BoardLive, :index
|
||||
|
||||
get "/users/settings", UserSettingsController, :edit
|
||||
put "/users/settings/update_password", UserSettingsController, :update_password
|
||||
put "/users/settings/update_email", UserSettingsController, :update_email
|
||||
|
||||
Reference in New Issue
Block a user