Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for matching x-inertia-partial-component #18

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions lib/inertia_phoenix/controller.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
defmodule InertiaPhoenix.Controller do
@moduledoc false
import InertiaPhoenix

import Plug.Conn,
only: [
get_req_header: 2,
put_resp_header: 3,
put_resp_cookie: 4
]

alias Phoenix.Controller

def render_inertia(conn, component, assigns \\ [props: %{}])
Expand All @@ -33,7 +35,7 @@ defmodule InertiaPhoenix.Controller do

defp build_assigns(conn, assigns, component) do
assigns
|> filter_partial_data(conn)
|> filter_partial_data(conn, component)
|> merge_shared_props(conn)
|> lazy_load()
|> assign_component(component)
Expand Down Expand Up @@ -61,21 +63,21 @@ defmodule InertiaPhoenix.Controller do
put_in(assigns, [:props, :flash], flash)
end

defp filter_partial_data(assigns, conn) do
case get_req_header(conn, "x-inertia-partial-data") do
[] ->
assigns

[partial_data] ->
requested_props = String.split(partial_data, ",")

Keyword.put(
assigns,
:props,
assigns[:props]
|> Enum.filter(fn {k, _} -> Atom.to_string(k) in requested_props end)
|> Enum.into(%{})
)
defp filter_partial_data(assigns, conn, component) do
with [component_request] when component_request == component <-
get_req_header(conn, "x-inertia-partial-component"),
[partial_data] <- get_req_header(conn, "x-inertia-partial-data") do
requested_props = String.split(partial_data, ",")

Keyword.put(
assigns,
:props,
assigns[:props]
|> Enum.filter(fn {k, _} -> Atom.to_string(k) in requested_props end)
|> Enum.into(%{})
)
else
_ -> assigns
end
end

Expand Down
30 changes: 29 additions & 1 deletion test/inertia_phoenix/controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ defmodule InertiaPhoenix.ControllerTest do
|> fetch_session
|> fetch_flash
|> InertiaPhoenix.Plug.call([])
# |> InertiaPhoenix.Controller.render_inertia("Home", props: %{hello: "world"})

# |> InertiaPhoenix.Controller.render_inertia("Home", props: %{hello: "world"})

assert html = html_response(conn, 409)
end
Expand All @@ -111,6 +112,7 @@ defmodule InertiaPhoenix.ControllerTest do
conn
|> Conn.put_req_header("x-inertia", "true")
|> Conn.put_req_header("x-inertia-version", "1")
|> Conn.put_req_header("x-inertia-partial-component", "Home")
|> Conn.put_req_header("x-inertia-partial-data", "hello,foo")
|> fetch_session
|> fetch_flash
Expand All @@ -130,6 +132,32 @@ defmodule InertiaPhoenix.ControllerTest do
assert json == page_map
end

test "render_inertia/3 with x-inertia-partial-data and mismatched x-inertia-partial-component",
%{conn: conn} do
conn =
conn
|> Conn.put_req_header("x-inertia", "true")
|> Conn.put_req_header("x-inertia-version", "1")
|> Conn.put_req_header("x-inertia-partial-component", "Dashboard")
|> Conn.put_req_header("x-inertia-partial-data", "hello,foo")
|> fetch_session
|> fetch_flash
|> InertiaPhoenix.Plug.call([])
|> InertiaPhoenix.Controller.render_inertia("Home",
props: %{hello: "world", world: "hello", foo: "bar"}
)

page_map = %{
"component" => "Home",
"props" => %{"hello" => "world", "world" => "hello", "foo" => "bar"},
"url" => "/",
"version" => "1"
}

assert json = json_response(conn, 200)
assert json == page_map
end

test "render_inertia/3 with lazy loaded prop", %{conn: conn} do
conn =
conn
Expand Down