share provider connection logic between setup wizard and providers form

Extract Products.connect_provider/2 that tests the connection, fetches
shop_id, creates the record, and enqueues sync. Both the setup wizard
and the providers form now use this shared function instead of
duplicating the flow. Also makes the products empty state context-aware
(distinguishes "no provider" from "provider connected but no products").

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey
2026-03-03 15:19:17 +00:00
parent 5b41f3fedf
commit 0853b6f528
6 changed files with 92 additions and 50 deletions

View File

@@ -184,12 +184,19 @@ defmodule BerrypodWeb.Admin.Products do
<div :if={@product_count == 0} class="admin-empty-state">
<.icon name="hero-cube" class="admin-empty-state-icon" />
<p class="admin-empty-state-title">No products yet</p>
<p class="admin-empty-state-text">
<p :if={@connections == []} class="admin-empty-state-text">
<.link navigate={~p"/admin/providers"} class="admin-link">
Connect a provider
</.link>
to sync your products.
</p>
<p :if={@connections != []} class="admin-empty-state-text">
Head to the
<.link navigate={~p"/admin/providers"} class="admin-link">
providers page
</.link>
to sync your products.
</p>
</div>
"""
end

View File

@@ -82,23 +82,18 @@ defmodule BerrypodWeb.Admin.Providers.Form do
end
defp save_connection(socket, :new, params) do
api_key = params["api_key"] || socket.assigns[:pending_api_key]
provider_type = socket.assigns.provider_type
params =
params
|> Map.put("provider_type", provider_type)
|> maybe_add_config(provider_type, socket.assigns.test_result)
|> maybe_add_name(provider_type, socket.assigns.test_result)
case Products.create_provider_connection(params) do
case Products.connect_provider(api_key, provider_type) do
{:ok, _connection} ->
{:noreply,
socket
|> put_flash(:info, "Connected to #{socket.assigns.provider.name}!")
|> push_navigate(to: ~p"/admin/settings")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
{:error, _reason} ->
{:noreply, put_flash(socket, :error, "Could not connect — check your API key")}
end
end
@@ -115,32 +110,6 @@ defmodule BerrypodWeb.Admin.Providers.Form do
end
end
# Printify returns shop_id, Printful returns store_id
defp maybe_add_config(params, "printify", {:ok, %{shop_id: shop_id}}) do
config = Map.get(params, "config", %{}) |> Map.put("shop_id", to_string(shop_id))
Map.put(params, "config", config)
end
defp maybe_add_config(params, "printful", {:ok, %{store_id: store_id}}) do
config = Map.get(params, "config", %{}) |> Map.put("store_id", to_string(store_id))
Map.put(params, "config", config)
end
defp maybe_add_config(params, _type, _result), do: params
defp maybe_add_name(params, "printify", {:ok, %{shop_name: name}}) when is_binary(name) do
Map.put_new(params, "name", name)
end
defp maybe_add_name(params, "printful", {:ok, %{store_name: name}}) when is_binary(name) do
Map.put_new(params, "name", name)
end
defp maybe_add_name(params, type, _result) do
provider = Provider.get(type)
Map.put_new(params, "name", (provider && provider.name) || type)
end
defp encrypt_api_key(api_key) do
case Berrypod.Vault.encrypt(api_key) do
{:ok, encrypted} -> encrypted

View File

@@ -127,17 +127,8 @@ defmodule BerrypodWeb.Setup.Onboarding do
else
socket = assign(socket, provider_connecting: true)
name =
case Provider.get(type) do
nil -> type
info -> info.name
end
params = %{"api_key" => api_key, "provider_type" => type, "name" => name}
case Products.create_provider_connection(params) do
case Products.connect_provider(api_key, type) do
{:ok, connection} ->
Products.enqueue_sync(connection)
setup = Setup.setup_status()
if setup.setup_complete do
@@ -154,11 +145,17 @@ defmodule BerrypodWeb.Setup.Onboarding do
|> put_flash(:info, "Connected! Product sync started in the background.")}
end
{:error, _changeset} ->
{:error, :no_api_key} ->
{:noreply,
socket
|> assign(:provider_connecting, false)
|> put_flash(:error, "Failed to save connection")}
|> put_flash(:error, "Please enter your API token")}
{:error, _reason} ->
{:noreply,
socket
|> assign(:provider_connecting, false)
|> put_flash(:error, "Could not connect — check your API key and try again")}
end
end
end
@@ -448,7 +445,13 @@ defmodule BerrypodWeb.Setup.Onboarding do
# ── Helpers ──
defp account_summary(%{current_scope: %{user: user}}) when not is_nil(user) do
user.email
site_name = Settings.site_name()
if site_name != "Store Name" do
"#{site_name} · #{user.email}"
else
user.email
end
end
defp account_summary(_), do: "Account created"