feat(pastes): implement editing for logged in users

Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2021-08-15 03:51:31 +05:30
parent f5525d2d20
commit a195688bdc
No known key found for this signature in database
GPG Key ID: ED5C54FBBB920E51
6 changed files with 63 additions and 12 deletions

View File

@ -15,16 +15,29 @@ defmodule KetbinWeb.PageController do
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
paste = Pastes.get_paste!(id) # fetch paste from db paste = Pastes.get_paste!(id) # fetch paste from db
# pull off current user if exists
current_user = conn.assigns.current_user
# show edit if current user matches creator of paste
show_edit = current_user && current_user.id || false
if paste.is_url do # paste is a url, redirect if paste.is_url do # paste is a url, redirect
redirect(conn, external: paste.content) redirect(conn, external: paste.content)
else # regular paste, show content else # regular paste, show content
render(conn, "show.html", paste: paste) render(conn, "show.html", paste: paste, show_edit: show_edit)
end end
end end
def showlink(conn, %{"id" => id}) do def showlink(conn, %{"id" => id}) do
paste = Pastes.get_paste!(id) paste = Pastes.get_paste!(id)
render(conn, "show.html", paste: paste)
# pull off current user if exists
current_user = conn.assigns.current_user
# show edit if current user matches creator of paste
show_edit = current_user && current_user.id || false
render(conn, "show.html", paste: paste, show_edit: show_edit)
end end
def raw(conn, %{"id" => id}) do def raw(conn, %{"id" => id}) do
@ -48,7 +61,7 @@ defmodule KetbinWeb.PageController do
paste_params = paste_params =
Map.put(paste_params, "id", id) Map.put(paste_params, "id", id)
|> Map.put("is_url", is_url) |> Map.put("is_url", is_url)
|> Map.put("belongs_to", (if current_user, do: current_user.id, else: nil)) |> Map.put("belongs_to", current_user && current_user.id)
# attempt to create a paste # attempt to create a paste
case Pastes.create_paste(paste_params) do case Pastes.create_paste(paste_params) do
@ -65,4 +78,24 @@ defmodule KetbinWeb.PageController do
render(conn, "index.html", changeset: changeset) render(conn, "index.html", changeset: changeset)
end end
end end
def edit(conn, %{"id" => id}) do
paste = Pastes.get_paste!(id)
changeset = Pastes.change_paste(paste)
render(conn, "edit.html", paste: paste, changeset: changeset)
end
def update(conn, %{"id" => id, "paste" => paste_params}) do
paste = Pastes.get_paste!(id)
case Pastes.update_paste(paste, paste_params) do
{:ok, paste} ->
conn
|> put_flash(:info, "Paste updated successfully.")
|> redirect(to: Routes.page_path(conn, :show, paste))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", paste: paste, changeset: changeset)
end
end
end end

View File

@ -1,4 +1,5 @@
defmodule KetbinWeb.UserAuth do defmodule KetbinWeb.UserAuth do
require Logger
import Plug.Conn import Plug.Conn
import Phoenix.Controller import Phoenix.Controller
@ -94,6 +95,11 @@ defmodule KetbinWeb.UserAuth do
assign(conn, :current_user, user) assign(conn, :current_user, user)
end end
def owns_paste(%{assigns: %{current_user: user}} = conn, _params) do
Logger.info("USER: #{inspect(user)}")
conn
end
defp ensure_user_token(conn) do defp ensure_user_token(conn) do
if user_token = get_session(conn, :user_token) do if user_token = get_session(conn, :user_token) do
{user_token, conn} {user_token, conn}

View File

@ -17,13 +17,16 @@ defmodule KetbinWeb.Router do
end end
scope "/", KetbinWeb do scope "/", KetbinWeb do
pipe_through :browser pipe_through [:browser, :owns_paste]
get "/", PageController, :index get "/", PageController, :index
get "/:id", PageController, :show get "/:id", PageController, :show
get "/:id/raw", PageController, :raw get "/:id/raw", PageController, :raw
get "/v/:id", PageController, :showlink get "/v/:id", PageController, :showlink
get "/edit/:id", PageController, :edit
post "/", PageController, :create post "/", PageController, :create
patch "/:id", PageController, :update
put "/:id", PageController, :update
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.

View File

@ -0,0 +1 @@
<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :update, @paste)) %>

View File

@ -7,15 +7,13 @@
<div class="w-full h-full"> <div class="w-full h-full">
<%= textarea f, :content, [class: "w-full h-full px-6 py-4 outline-none bg-light-grey font-bold resize-none", placeholder: "> Paste, save, share! (Pasting just a URL will shorten it!)"] %> <%= textarea f, :content, [class: "w-full h-full px-6 py-4 outline-none bg-light-grey font-bold resize-none", placeholder: "> Paste, save, share! (Pasting just a URL will shorten it!)"] %>
<div class="absolute top-0 right-0 p-4"> <div class="flex absolute top-0 right-0 p-4">
<button type="submit"> <button type="submit">
<svg <svg
class="h-6 w-6 cursor-pointer fill-current text-white hover:text-amber" class="h-6 w-6 cursor-pointer fill-current text-white hover:text-amber"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24" viewBox="0 0 24 24">
> <path d="M17.6 3.6c-.4-.4-.9-.6-1.4-.6H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V7.8c0-.5-.2-1-.6-1.4l-2.8-2.8zM12 19c-1.7 0-3-1.3-3-3s1.3-3 3-3 3 1.3 3 3-1.3 3-3 3zm1-10H7c-1.1 0-2-.9-2-2s.9-2 2-2h6c1.1 0 2 .9 2 2s-.9 2-2 2z"/>
<path d="M17.6 3.6c-.4-.4-.9-.6-1.4-.6H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V7.8c0-.5-.2-1-.6-1.4l-2.8-2.8zM12 19c-1.7 0-3-1.3-3-3s1.3-3 3-3 3 1.3 3 3-1.3 3-3 3zm1-10H7c-1.1 0-2-.9-2-2s.9-2 2-2h6c1.1 0 2 .9 2 2s-.9 2-2 2z"
/>
</svg> </svg>
</button> </button>
</div> </div>

View File

@ -1,2 +1,12 @@
<div class="flex relative flex-col w-full h-full">
<div class="flex absolute top-0 right-0 p-4 ">
<%= if @show_edit do%>
<a href="<%= Routes.page_path(@conn, :edit, @paste.id) %>" class="text-white hover:text-amber">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" class="h-6 w-6 cursor-pointer fill-current">
<path d="M3 17.46v3.04c0 .28.22.5.5.5h3.04c.13 0 .26-.05.35-.15L17.81 9.94l-3.75-3.75L3.15 17.1c-.1.1-.15.22-.15.36zM20.71 7.04a.996.996 0 0 0 0-1.41l-2.34-2.34a.996.996 0 0 0-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"></path>
</svg>
</a>
<% end %>
</div>
<code class="break-word pl-2 h-full w-full nomarkdown overflow-y-auto"><%= @paste.content %></code> <code class="break-word pl-2 h-full w-full nomarkdown overflow-y-auto"><%= @paste.content %></code>
</div>