Skip to content

add credentials with single click #3127

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

Merged
merged 3 commits into from
Apr 18, 2025
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ and this project adheres to
- `petal_components` from 2.9.0 to 3.0.1
- Added a "BETA" label to the Kafka trigger type
[#3098](https://github.com/OpenFn/lightning/pull/3098)
- Simplified adding credentials to projects
[#3034](https://github.com/OpenFn/lightning/issues/3034)

### Fixed

Expand Down
59 changes: 19 additions & 40 deletions lib/lightning_web/live/components/credentials.ex
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,13 @@ defmodule LightningWeb.Components.Credentials do
%{
select_id: "project-credentials-list-#{assigns.id}",
prompt: "Grant projects access to this credential",
add_project_id: "add-project-credential-button-#{assigns.id}",
remove_project_id: "remove-project-credential-button-#{assigns.id}"
}

:oauth_client ->
%{
select_id: "project-oauth-clients-list-#{assigns.id}",
prompt: "Grant projects access to this OAuth client",
add_project_id: "add-project-oauth-client-button-#{assigns.id}",
remove_project_id: "remove-project-oauth-client-button-#{assigns.id}"
}
end
Expand All @@ -208,40 +206,25 @@ defmodule LightningWeb.Components.Credentials do
>
Project
</label>

<div class="flex w-full items-center gap-2 pb-3 mt-1">
<div class="grow">
<select
id={@select_id}
name={:project_id}
class={[
"block w-full rounded-lg border border-secondary-300 bg-white",
"sm:text-sm shadow-xs",
"focus:border-primary-300 focus:ring focus:ring-primary-200 focus:ring-primary-200/50",
"disabled:cursor-not-allowed "
]}
phx-change="select_project"
phx-target={@phx_target}
>
<option value="">{@prompt}</option>
{Phoenix.HTML.Form.options_for_select(
map_projects_for_select(@available_projects),
@selected
)}
</select>
</div>

<div class="grow-0 items-right">
<.button
id={@add_project_id}
disabled={disable_add_button(@available_projects, @selected_projects)}
phx-target={@phx_target}
phx-value-project_id={@selected}
phx-click="add_selected_project"
>
Add
</.button>
</div>
<div class="mt-1 pb-3">
<select
id={@select_id}
name="project_id"
class={[
"block w-full rounded-lg border border-secondary-300 bg-white",
"sm:text-sm shadow-xs",
"focus:border-primary-300 focus:ring focus:ring-primary-200 focus:ring-primary-200/50",
"disabled:cursor-not-allowed "
]}
phx-change="add_selected_project"
phx-target={@phx_target}
>
<option value="">{@prompt}</option>
{Phoenix.HTML.Form.options_for_select(
map_projects_for_select(@available_projects),
""
)}
</select>
</div>

<div class="overflow-auto max-h-32">
Expand Down Expand Up @@ -275,10 +258,6 @@ defmodule LightningWeb.Components.Credentials do
end)
end

defp disable_add_button(available_projects, selected) do
selected == "" or selected == nil or available_projects == []
end

attr :users, :list, required: true
attr :form, :map, required: true

Expand Down
64 changes: 30 additions & 34 deletions test/lightning_web/live/credential_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,16 @@ defmodule LightningWeb.CredentialLiveTest do
"#credential-form-new textarea[name='credential[body]']"
)

# Select first project
index_live
|> element("#project-credentials-list-new")
|> render_change(%{"project_id" => project1.id})

index_live
|> element("#add-project-credential-button-new", "Add")
|> render_click()
# Verify project is added
assert index_live
|> has_element?(
"#remove-project-credential-button-new-#{project1.id}"
)

assert index_live
|> form("#credential-form-new", credential: %{name: ""})
Expand All @@ -384,13 +387,16 @@ defmodule LightningWeb.CredentialLiveTest do
|> form("#credential-form-new", credential: %{name: "MailChimp'24"})
|> render_change() =~ "credential name has invalid format"

# Select second project
index_live
|> element("#project-credentials-list-new")
|> render_change(%{"project_id" => project2.id})

index_live
|> element("#add-project-credential-button-new", "Add")
|> render_click()
# Verify second project is added
assert index_live
|> has_element?(
"#remove-project-credential-button-new-#{project2.id}"
)

{:ok, _index_live, html} =
index_live
Expand Down Expand Up @@ -446,10 +452,6 @@ defmodule LightningWeb.CredentialLiveTest do
|> element("#project-credentials-list-new")
|> render_change(%{"project_id" => project1.id})

index_live
|> element("#add-project-credential-button-new", "Add")
|> render_click()

assert index_live
|> form("#credential-form-new", credential: %{name: ""})
|> render_change() =~ "can&#39;t be blank"
Expand All @@ -462,10 +464,6 @@ defmodule LightningWeb.CredentialLiveTest do
|> element("#project-credentials-list-new")
|> render_change(%{"project_id" => project2.id})

index_live
|> element("#add-project-credential-button-new", "Add")
|> render_click()

{:ok, _index_live, html} =
index_live
|> form("#credential-form-new", credential: @create_attrs)
Expand Down Expand Up @@ -771,7 +769,7 @@ defmodule LightningWeb.CredentialLiveTest do
assert html =~ "some updated name"
end

test "adds new project with access", %{
test "Edit adds new project with access", %{
conn: conn,
user: user
} do
Expand Down Expand Up @@ -800,9 +798,11 @@ defmodule LightningWeb.CredentialLiveTest do
|> element("#project-credentials-list-#{credential.id}")
|> render_change(%{"project_id" => project.id})

view
|> element("#add-project-credential-button-#{credential.id}")
|> render_click()
# Verify project is added
assert view
|> has_element?(
"#remove-project-credential-button-#{credential.id}-#{project.id}"
)

view |> form("#credential-form-#{credential.id}") |> render_submit()

Expand Down Expand Up @@ -928,18 +928,19 @@ defmodule LightningWeb.CredentialLiveTest do
|> element("#project-credentials-list-#{credential.id}")
|> render_change(%{"project_id" => project.id})

html =
view
|> element("#add-project-credential-button-#{credential.id}")
|> render_click()
html = view |> render()

assert html =~ project.name,
"adding an existing project doesn't break anything"

assert view |> delete_credential_button(project.id) |> has_element?()
# Verify the project is added to the credential's projects list
assert view
|> has_element?(
"#remove-project-credential-button-#{credential.id}-#{project.id}"
),
"project should be added to credential's projects list"

# Let's remove the project and add it back again

view
|> delete_credential_button(project.id)
|> render_click()
Expand All @@ -952,12 +953,11 @@ defmodule LightningWeb.CredentialLiveTest do
|> element("#project-credentials-list-#{credential.id}")
|> render_change(%{"project_id" => project.id})

view
|> element("#add-project-credential-button-#{credential.id}")
|> render_click()

assert view |> delete_credential_button(project.id) |> has_element?(),
"project is added back"
assert view
|> has_element?(
"#remove-project-credential-button-#{credential.id}-#{project.id}"
),
"project should be added back to credential's projects list"

view |> form("#credential-form-#{credential.id}") |> render_submit()

Expand Down Expand Up @@ -1362,10 +1362,6 @@ defmodule LightningWeb.CredentialLiveTest do
view
|> element("#project-credentials-list-new")
|> render_change(%{"project_id" => project.id})

view
|> element("#add-project-credential-button-new", "Add")
|> render_click()
end)

view
Expand Down