From e9c2dc7aa9974318d5745b2cfd4e8f677408a49d Mon Sep 17 00:00:00 2001 From: Troy Martin Date: Sat, 22 Feb 2020 14:26:37 -0500 Subject: [PATCH] Fix for #16 Forward flash on 409 conflict --- CHANGELOG.md | 10 ++++++++++ README.md | 8 ++++---- lib/inertia_phoenix/plug.ex | 10 +++++++++- mix.exs | 2 +- .../controllers/page_controller_test.exs | 18 ++++++++++++++++++ test/support/conn_case.ex | 1 + test/support/endpoint.ex | 5 ++--- test/support/router.ex | 2 +- 8 files changed, 46 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bd0aa8..1b1d5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## [0.2.4](https://github.com/devato/inertia_phoenix/tree/0.2.4) (2020-02-22) + +[Full Changelog](https://github.com/devato/inertia_phoenix/compare/0.2.3...0.2.4) + +**Merged pull requests:** + +- Feature/refactor tests [\#23](https://github.com/devato/inertia_phoenix/pull/23) ([tmartin314](https://github.com/tmartin314)) +- Fix Hex.pm link in README [\#22](https://github.com/devato/inertia_phoenix/pull/22) ([szTheory](https://github.com/szTheory)) +- Add syntax highlighting to README for Github [\#21](https://github.com/devato/inertia_phoenix/pull/21) ([szTheory](https://github.com/szTheory)) + ## [0.2.3](https://github.com/devato/inertia_phoenix/tree/0.2.3) (2020-02-21) [Full Changelog](https://github.com/devato/inertia_phoenix/compare/0.2.2...0.2.3) diff --git a/README.md b/README.md index 63c43fe..5dd10ed 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Getting started with Inertia.js in a few steps. Add to mix.exs: ```elixir -{:inertia_phoenix, "~> 0.2.4"} +{:inertia_phoenix, "~> 0.2.5"} ``` Add Plug to `WEB_PATH/router.ex` @@ -165,8 +165,9 @@ axios.defaults.xsrfHeaderName = "x-csrf-token"; ## Complete - Render React/Vue/Svelte from controllers -- Flash data pass to props via Plug +- Flash data passed to props via Plug - Assets Versioning: https://inertiajs.com/asset-versioning +- Lazy Evaluation: https://inertiajs.com/responses#lazy-evaluation - Auto put response cookie for crsf token: https://inertiajs.com/security#csrf-protection - Override redirect codes: https://inertiajs.com/redirects#303-response-code - Partial reloads: https://inertiajs.com/requests#partial-reloads @@ -174,8 +175,7 @@ axios.defaults.xsrfHeaderName = "x-csrf-token"; ## In Progress -- Hex Documentation: See [Issue #3](https://github.com/devato/inertia_phoenix/issues/5) -- Error Handling: https://inertiajs.com/error-handling. See [Issue #5](https://github.com/devato/inertia_phoenix/issues/5) +[See Issue Tracker](https://github.com/devato/inertia_phoenix/issues) # Example Apps diff --git a/lib/inertia_phoenix/plug.ex b/lib/inertia_phoenix/plug.ex index 663ba16..3648eb3 100644 --- a/lib/inertia_phoenix/plug.ex +++ b/lib/inertia_phoenix/plug.ex @@ -37,11 +37,12 @@ defmodule InertiaPhoenix.Plug do |> put_resp_header("x-inertia", "true") |> put_resp_header("x-inertia-location", request_url(conn)) |> put_resp_content_type("text/html") + |> maybe_forward_flash() |> send_resp(:conflict, "") |> halt() end - def check_redirect(conn) do + defp check_redirect(conn) do conn |> register_before_send(fn conn -> if conn.method in ["PUT", "PATCH", "DELETE"] and conn.status in [301, 302] do @@ -51,4 +52,11 @@ defmodule InertiaPhoenix.Plug do end end) end + + defp maybe_forward_flash(%{private: %{phoenix_flash: flash}} = conn) + when is_map(flash) and map_size(flash) > 0 do + put_session(conn, "phoenix_flash", flash) + end + + defp maybe_forward_flash(conn), do: conn end diff --git a/mix.exs b/mix.exs index d241765..4260216 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule InertiaPhoenix.MixProject do def project do [ app: :inertia_phoenix, - version: "0.2.4", + version: "0.2.5", elixir: "~> 1.6", elixirc_paths: elixirc_paths(Mix.env()), start_permanent: Mix.env() == :prod, diff --git a/test/inertia_phoenix/controllers/page_controller_test.exs b/test/inertia_phoenix/controllers/page_controller_test.exs index 886f33e..afb1358 100644 --- a/test/inertia_phoenix/controllers/page_controller_test.exs +++ b/test/inertia_phoenix/controllers/page_controller_test.exs @@ -2,6 +2,11 @@ defmodule InertiaPhoenix.PageControllerTest do use InertiaPhoenix.ConnCase alias Phoenix.HTML.Tag + setup do + Logger.disable(self()) + :ok + end + test "GET / non-inertia no props", %{conn: conn} do conn = conn @@ -79,6 +84,19 @@ defmodule InertiaPhoenix.PageControllerTest do |> put_req_header("x-inertia-version", "123") |> get("/") + assert get_flash(conn) == %{} + assert html = html_response(conn, 409) + end + + test "GET / x-inertia-version mismatch forwards flash", %{conn: conn} do + conn = + conn + |> Plug.Test.init_test_session(%{"phoenix_flash" => %{error: "something went wrong"}}) + |> put_req_header("x-inertia", "true") + |> put_req_header("x-inertia-version", "123") + |> get("/") + + assert get_flash(conn) == %{error: "something went wrong"} assert html = html_response(conn, 409) end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index b238bd7..d591e3d 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -8,6 +8,7 @@ defmodule InertiaPhoenix.ConnCase do import Plug.Conn import Phoenix.ConnTest alias Router.Helpers, as: Routes + @endpoint Endpoint end end diff --git a/test/support/endpoint.ex b/test/support/endpoint.ex index ae9f37c..45f6f56 100644 --- a/test/support/endpoint.ex +++ b/test/support/endpoint.ex @@ -10,17 +10,16 @@ defmodule InertiaPhoenix.TestWeb.Endpoint do signing_salt: "yKZ6VvPl" ] - plug(Plug.RequestId) - plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint]) - plug(Plug.Parsers, parsers: [:urlencoded, :multipart, :json], pass: ["*/*"], json_decoder: Phoenix.json_library() ) + plug(Plug.RequestId) plug(Plug.MethodOverride) plug(Plug.Head) plug(Plug.Session, @session_options) + plug(InertiaPhoenix.TestWeb.Router) end diff --git a/test/support/router.ex b/test/support/router.ex index f79c7e0..708a5d0 100644 --- a/test/support/router.ex +++ b/test/support/router.ex @@ -11,7 +11,7 @@ defmodule InertiaPhoenix.TestWeb.Router do end scope "/", InertiaPhoenix.TestWeb do - pipe_through([:browser]) + pipe_through(:browser) get("/", PageController, :index) put("/", PageController, :index)