Skip to content

Commit 3695a29

Browse files
committed
Push and pray
1 parent a0765b2 commit 3695a29

2 files changed

Lines changed: 42 additions & 8 deletions

File tree

lib/peek_app_sdk/ui/odyssey/activity_picker.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPicker do
1313
use Phoenix.LiveComponent
1414
use Phoenix.Component
1515

16-
@required_assigns ~w(field install_id)a
17-
1816
@impl true
1917
def mount(socket) do
20-
socket =
21-
socket
22-
|> assign(selection: nil)
23-
2418
{:ok, socket}
2519
end
2620

@@ -59,7 +53,7 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPicker do
5953
~H"""
6054
<div phx-hook="OdysseyActivityPicker" id={"#{@id}_hook"}>
6155
<!-- Hidden input for form value -->
62-
<input type="hidden" name={@field.name} value={@selection} />
56+
<input type="hidden" name={@field.name} value={process_ids_for_multi_select(@field.value)} />
6357
6458
<!-- Odyssey product picker will be rendered here -->
6559
<div>
@@ -68,7 +62,8 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPicker do
6862
phx-update="ignore"
6963
title="Activity Picker"
7064
phx-target={@myself}
71-
ids={@selection}
65+
multiple={if(@multiple, do: "true", else: "false")}
66+
ids={process_ids_for_multi_select(@field.value)}
7267
products={Jason.encode!(@products)}
7368
>
7469
</odyssey-product-picker>
@@ -77,6 +72,10 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPicker do
7772
"""
7873
end
7974

75+
defp process_ids_for_multi_select(ids) do
76+
ids |> List.wrap() |> Enum.join(",")
77+
end
78+
8079
@doc """
8180
Renders an activity picker component.
8281
@@ -85,6 +84,7 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPicker do
8584
"""
8685
attr(:field, :any, required: true, doc: "a Phoenix.HTML.FormField struct")
8786
attr(:id, :string, doc: "component id, defaults to form_field_activity_picker")
87+
attr(:multiple, :boolean, default: false, doc: "whether to allow multiple selections")
8888
attr(:install_id, :string, required: true, doc: "the install id for the current partner")
8989

9090
def odyssey_activity_picker(assigns) do

test/peek_app_sdk/ui/odyssey/activity_picker_test.exs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,40 @@ defmodule PeekAppSDK.UI.Odyssey.OdysseyActivityPickerTest do
9090
assert html =~ ~r/<input[^>]*type="hidden"[^>]*name="my_form\[activity_id\]"[^>]*>/
9191
end
9292

93+
test "renders with correct form when a multi-select" do
94+
# Mock the GraphQL query response
95+
Tesla.Adapter.Finch
96+
|> Mimic.stub(:call, fn _env, _opts ->
97+
response_data = %{
98+
activities: [
99+
%{id: "activity_1", name: "Test Activity", colorHex: "#000000"},
100+
%{id: "activity_2", name: "Test Activity 2", colorHex: "#000000"}
101+
]
102+
}
103+
104+
{:ok, %Tesla.Env{status: 200, body: %{data: response_data}}}
105+
end)
106+
107+
# Create a form with a pre-selected activity
108+
form_data = %{"activity_ids" => ["activity_1", "activity_2"]}
109+
form = to_form(form_data, as: :my_form)
110+
111+
html =
112+
render_component(
113+
fn assigns ->
114+
~H"""
115+
<.form for={@form}>
116+
<.odyssey_activity_picker field={@form[:activity_ids]} install_id="test_install_id" />
117+
</.form>
118+
"""
119+
end,
120+
%{form: form}
121+
)
122+
123+
# Verify the hidden input has the correct name (value starts as nil and gets updated by JS)
124+
assert html =~ "ids=\"activity_1,activity_2\""
125+
end
126+
93127
test "generates unique component IDs based on form field" do
94128
# Mock the GraphQL query response
95129
Tesla.Adapter.Finch

0 commit comments

Comments
 (0)