Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit e9c2dc7

Browse files
committed
Fix for #16 Forward flash on 409 conflict
1 parent 014d7c8 commit e9c2dc7

8 files changed

Lines changed: 46 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [0.2.4](https://github.com/devato/inertia_phoenix/tree/0.2.4) (2020-02-22)
4+
5+
[Full Changelog](https://github.com/devato/inertia_phoenix/compare/0.2.3...0.2.4)
6+
7+
**Merged pull requests:**
8+
9+
- Feature/refactor tests [\#23](https://github.com/devato/inertia_phoenix/pull/23) ([tmartin314](https://github.com/tmartin314))
10+
- Fix Hex.pm link in README [\#22](https://github.com/devato/inertia_phoenix/pull/22) ([szTheory](https://github.com/szTheory))
11+
- Add syntax highlighting to README for Github [\#21](https://github.com/devato/inertia_phoenix/pull/21) ([szTheory](https://github.com/szTheory))
12+
313
## [0.2.3](https://github.com/devato/inertia_phoenix/tree/0.2.3) (2020-02-21)
414

515
[Full Changelog](https://github.com/devato/inertia_phoenix/compare/0.2.2...0.2.3)

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Getting started with Inertia.js in a few steps.
3535

3636
Add to mix.exs:
3737
```elixir
38-
{:inertia_phoenix, "~> 0.2.4"}
38+
{:inertia_phoenix, "~> 0.2.5"}
3939
```
4040

4141
Add Plug to `WEB_PATH/router.ex`
@@ -165,17 +165,17 @@ axios.defaults.xsrfHeaderName = "x-csrf-token";
165165

166166
## Complete
167167
- Render React/Vue/Svelte from controllers
168-
- Flash data pass to props via Plug
168+
- Flash data passed to props via Plug
169169
- Assets Versioning: https://inertiajs.com/asset-versioning
170+
- Lazy Evaluation: https://inertiajs.com/responses#lazy-evaluation
170171
- Auto put response cookie for crsf token: https://inertiajs.com/security#csrf-protection
171172
- Override redirect codes: https://inertiajs.com/redirects#303-response-code
172173
- Partial reloads: https://inertiajs.com/requests#partial-reloads
173174
- Shared data interface: https://inertiajs.com/shared-data
174175

175176
## In Progress
176177

177-
- Hex Documentation: See [Issue #3](https://github.com/devato/inertia_phoenix/issues/5)
178-
- Error Handling: https://inertiajs.com/error-handling. See [Issue #5](https://github.com/devato/inertia_phoenix/issues/5)
178+
[See Issue Tracker](https://github.com/devato/inertia_phoenix/issues)
179179

180180
# Example Apps
181181

lib/inertia_phoenix/plug.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ defmodule InertiaPhoenix.Plug do
3737
|> put_resp_header("x-inertia", "true")
3838
|> put_resp_header("x-inertia-location", request_url(conn))
3939
|> put_resp_content_type("text/html")
40+
|> maybe_forward_flash()
4041
|> send_resp(:conflict, "")
4142
|> halt()
4243
end
4344

44-
def check_redirect(conn) do
45+
defp check_redirect(conn) do
4546
conn
4647
|> register_before_send(fn conn ->
4748
if conn.method in ["PUT", "PATCH", "DELETE"] and conn.status in [301, 302] do
@@ -51,4 +52,11 @@ defmodule InertiaPhoenix.Plug do
5152
end
5253
end)
5354
end
55+
56+
defp maybe_forward_flash(%{private: %{phoenix_flash: flash}} = conn)
57+
when is_map(flash) and map_size(flash) > 0 do
58+
put_session(conn, "phoenix_flash", flash)
59+
end
60+
61+
defp maybe_forward_flash(conn), do: conn
5462
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule InertiaPhoenix.MixProject do
44
def project do
55
[
66
app: :inertia_phoenix,
7-
version: "0.2.4",
7+
version: "0.2.5",
88
elixir: "~> 1.6",
99
elixirc_paths: elixirc_paths(Mix.env()),
1010
start_permanent: Mix.env() == :prod,

test/inertia_phoenix/controllers/page_controller_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ defmodule InertiaPhoenix.PageControllerTest do
22
use InertiaPhoenix.ConnCase
33
alias Phoenix.HTML.Tag
44

5+
setup do
6+
Logger.disable(self())
7+
:ok
8+
end
9+
510
test "GET / non-inertia no props", %{conn: conn} do
611
conn =
712
conn
@@ -79,6 +84,19 @@ defmodule InertiaPhoenix.PageControllerTest do
7984
|> put_req_header("x-inertia-version", "123")
8085
|> get("/")
8186

87+
assert get_flash(conn) == %{}
88+
assert html = html_response(conn, 409)
89+
end
90+
91+
test "GET / x-inertia-version mismatch forwards flash", %{conn: conn} do
92+
conn =
93+
conn
94+
|> Plug.Test.init_test_session(%{"phoenix_flash" => %{error: "something went wrong"}})
95+
|> put_req_header("x-inertia", "true")
96+
|> put_req_header("x-inertia-version", "123")
97+
|> get("/")
98+
99+
assert get_flash(conn) == %{error: "something went wrong"}
82100
assert html = html_response(conn, 409)
83101
end
84102

test/support/conn_case.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule InertiaPhoenix.ConnCase do
88
import Plug.Conn
99
import Phoenix.ConnTest
1010
alias Router.Helpers, as: Routes
11+
1112
@endpoint Endpoint
1213
end
1314
end

test/support/endpoint.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ defmodule InertiaPhoenix.TestWeb.Endpoint do
1010
signing_salt: "yKZ6VvPl"
1111
]
1212

13-
plug(Plug.RequestId)
14-
plug(Plug.Telemetry, event_prefix: [:phoenix, :endpoint])
15-
1613
plug(Plug.Parsers,
1714
parsers: [:urlencoded, :multipart, :json],
1815
pass: ["*/*"],
1916
json_decoder: Phoenix.json_library()
2017
)
2118

19+
plug(Plug.RequestId)
2220
plug(Plug.MethodOverride)
2321
plug(Plug.Head)
2422
plug(Plug.Session, @session_options)
23+
2524
plug(InertiaPhoenix.TestWeb.Router)
2625
end

test/support/router.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule InertiaPhoenix.TestWeb.Router do
1111
end
1212

1313
scope "/", InertiaPhoenix.TestWeb do
14-
pipe_through([:browser])
14+
pipe_through(:browser)
1515

1616
get("/", PageController, :index)
1717
put("/", PageController, :index)

0 commit comments

Comments
 (0)