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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user