From 13728f2ef8e46da821e8262845000ad2e9f99123 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Fri, 13 Aug 2021 06:33:43 +0530 Subject: [PATCH] feat(ui): create idiomatic UI matching previous katbin iteration Signed-off-by: SphericalKat --- lib/ketbin_web/controllers/page_controller.ex | 16 +++++++++++++++- lib/ketbin_web/router.ex | 1 + lib/ketbin_web/templates/layout/app.html.eex | 2 +- lib/ketbin_web/templates/page/form.html.eex | 14 ++++++++++++++ lib/ketbin_web/templates/page/index.html.eex | 4 +--- lib/ketbin_web/templates/paste/new.html.eex | 4 ---- lib/ketbin_web/views/error_helpers.ex | 2 +- 7 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 lib/ketbin_web/templates/page/form.html.eex diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index bf07d89..f29262b 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -5,11 +5,25 @@ defmodule KetbinWeb.PageController do alias Ketbin.Pastes.Paste def index(conn, _params) do - render(conn, "index.html") + changeset = Pastes.change_paste(%Paste{}) + render(conn, "index.html", changeset: changeset) end def show(conn, %{"id" => id}) do paste = Pastes.get_paste!(id) render(conn, "show.html", paste: paste) end + + def create(conn, %{"paste" => paste_params}) do + # paste_params = Map.put(paste_params, "id", s) + case Pastes.create_paste(paste_params) do + {:ok, paste} -> + conn + |> put_flash(:info, "Paste created successfully.") + |> redirect(to: Routes.paste_path(conn, :show, paste)) + + {:error, %Ecto.Changeset{} = changeset} -> + render(conn, "index.html", changeset: changeset) + end + end end diff --git a/lib/ketbin_web/router.ex b/lib/ketbin_web/router.ex index 42d978a..0af8b63 100644 --- a/lib/ketbin_web/router.ex +++ b/lib/ketbin_web/router.ex @@ -21,6 +21,7 @@ defmodule KetbinWeb.Router do get "/", PageController, :index get "/:id", PageController, :show + post "/", PageController, :create end # Other scopes may use custom stacks. diff --git a/lib/ketbin_web/templates/layout/app.html.eex b/lib/ketbin_web/templates/layout/app.html.eex index 4ab9ab3..ad82c23 100644 --- a/lib/ketbin_web/templates/layout/app.html.eex +++ b/lib/ketbin_web/templates/layout/app.html.eex @@ -11,7 +11,7 @@ - +
diff --git a/lib/ketbin_web/templates/page/form.html.eex b/lib/ketbin_web/templates/page/form.html.eex new file mode 100644 index 0000000..8c0782b --- /dev/null +++ b/lib/ketbin_web/templates/page/form.html.eex @@ -0,0 +1,14 @@ +<%= form_for @changeset, @action, [class: "flex flex-col w-full h-full relative"], fn f -> %> + <%= if @changeset.action do %> +
+

Oops, something went wrong!

+
+ <% end %> + +
+ <%= 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!)"] %> +
+ <%= submit "Save" %> +
+
+<% end %> diff --git a/lib/ketbin_web/templates/page/index.html.eex b/lib/ketbin_web/templates/page/index.html.eex index 831b6dc..8150650 100644 --- a/lib/ketbin_web/templates/page/index.html.eex +++ b/lib/ketbin_web/templates/page/index.html.eex @@ -1,3 +1 @@ -
- hello, world! -
+<%= render "form.html", Map.put(assigns, :action, Routes.page_path(@conn, :create)) %> diff --git a/lib/ketbin_web/templates/paste/new.html.eex b/lib/ketbin_web/templates/paste/new.html.eex index 9e8ab11..b1e4f3f 100644 --- a/lib/ketbin_web/templates/paste/new.html.eex +++ b/lib/ketbin_web/templates/paste/new.html.eex @@ -1,5 +1 @@ -

New Paste

- <%= render "form.html", Map.put(assigns, :action, Routes.paste_path(@conn, :create)) %> - -<%= link "Back", to: Routes.paste_path(@conn, :index) %> diff --git a/lib/ketbin_web/views/error_helpers.ex b/lib/ketbin_web/views/error_helpers.ex index 4a37194..61b739e 100644 --- a/lib/ketbin_web/views/error_helpers.ex +++ b/lib/ketbin_web/views/error_helpers.ex @@ -11,7 +11,7 @@ defmodule KetbinWeb.ErrorHelpers do def error_tag(form, field) do Enum.map(Keyword.get_values(form.errors, field), fn error -> content_tag(:span, translate_error(error), - class: "invalid-feedback", + class: "absolute top-0 left-0 px-6 py-4", phx_feedback_for: input_name(form, field) ) end)