Skip to content

Commit

Permalink
Fix for #16 Forward flash on 409 conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
tmartin8080 committed Feb 22, 2020
1 parent 014d7c8 commit e9c2dc7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -165,17 +165,17 @@ 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
- Shared data interface: https://inertiajs.com/shared-data

## 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

Expand Down
10 changes: 9 additions & 1 deletion lib/inertia_phoenix/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions test/inertia_phoenix/controllers/page_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule InertiaPhoenix.ConnCase do
import Plug.Conn
import Phoenix.ConnTest
alias Router.Helpers, as: Routes

@endpoint Endpoint
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/support/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/support/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e9c2dc7

Please sign in to comment.