2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.Admin.Providers.Form do
|
|
|
|
|
use BerrypodWeb, :live_view
|
2026-01-31 22:08:34 +00:00
|
|
|
|
2026-03-04 12:17:56 +00:00
|
|
|
alias Berrypod.{KeyValidation, Products}
|
2026-02-18 21:23:15 +00:00
|
|
|
alias Berrypod.Products.ProviderConnection
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
alias Berrypod.Providers.Provider
|
2026-01-31 22:08:34 +00:00
|
|
|
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
@supported_types Enum.map(Provider.available(), & &1.type)
|
2026-02-15 10:53:15 +00:00
|
|
|
|
2026-01-31 22:08:34 +00:00
|
|
|
@impl true
|
|
|
|
|
def mount(params, _session, socket) do
|
|
|
|
|
{:ok, apply_action(socket, socket.assigns.live_action, params)}
|
|
|
|
|
end
|
|
|
|
|
|
2026-02-15 10:53:15 +00:00
|
|
|
defp apply_action(socket, :new, params) do
|
|
|
|
|
provider_type = validated_type(params["type"])
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
provider = Provider.get(provider_type)
|
2026-02-15 10:53:15 +00:00
|
|
|
|
2026-01-31 22:08:34 +00:00
|
|
|
socket
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
|> assign(:page_title, "Connect to #{provider.name}")
|
2026-02-15 10:53:15 +00:00
|
|
|
|> assign(:provider_type, provider_type)
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
|> assign(:provider, provider)
|
2026-02-15 10:53:15 +00:00
|
|
|
|> assign(:connection, %ProviderConnection{provider_type: provider_type})
|
2026-01-31 22:08:34 +00:00
|
|
|
|> assign(:form, to_form(ProviderConnection.changeset(%ProviderConnection{}, %{})))
|
|
|
|
|
|> assign(:pending_api_key, nil)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp apply_action(socket, :edit, %{"id" => id}) do
|
|
|
|
|
connection = Products.get_provider_connection!(id)
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
provider = Provider.get(connection.provider_type)
|
2026-01-31 22:08:34 +00:00
|
|
|
|
|
|
|
|
socket
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
|> assign(:page_title, "#{provider.name} settings")
|
2026-02-15 10:53:15 +00:00
|
|
|
|> assign(:provider_type, connection.provider_type)
|
add setup onboarding page, dashboard launch checklist, provider registry
- new /setup page with three-section onboarding (account, provider, payments)
- dashboard launch checklist with progress bar, go-live, dismiss
- provider registry on Provider module (single source of truth for metadata)
- payments registry for Stripe
- setup context made provider-agnostic (provider_connected, theme_customised, etc.)
- admin provider pages now fully registry-driven (no hardcoded provider names)
- auth flow: fresh installs redirect to /setup, signed_in_path respects setup state
- removed old /admin/setup wizard
- 840 tests, 0 failures
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 00:34:06 +00:00
|
|
|
|> assign(:provider, provider)
|
2026-01-31 22:08:34 +00:00
|
|
|
|> assign(:connection, connection)
|
|
|
|
|
|> assign(:form, to_form(ProviderConnection.changeset(connection, %{})))
|
|
|
|
|
|> assign(:pending_api_key, nil)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_event("validate", %{"provider_connection" => params}, socket) do
|
complete onboarding UX v2
Tasks C, H, I from the plan:
- Forgiving API key validation: add Printify UUID format and Printful
length validation, validate on blur for fast feedback, helpful error
messages with specific guidance
- External links UX: verified all external links use <.external_link>
component with target="_blank", rel="noopener noreferrer", icon, and
screen reader text
- Input styling WCAG compliance: increase input border contrast from
~3.3:1 to ~4.5-5:1 across all theme moods (neutral, warm, cool, dark)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 07:56:54 +00:00
|
|
|
api_key = params["api_key"] || ""
|
|
|
|
|
provider_type = socket.assigns.provider_type
|
|
|
|
|
|
|
|
|
|
# Build base changeset
|
|
|
|
|
changeset =
|
2026-01-31 22:08:34 +00:00
|
|
|
socket.assigns.connection
|
|
|
|
|
|> ProviderConnection.changeset(params)
|
|
|
|
|
|> Map.put(:action, :validate)
|
complete onboarding UX v2
Tasks C, H, I from the plan:
- Forgiving API key validation: add Printify UUID format and Printful
length validation, validate on blur for fast feedback, helpful error
messages with specific guidance
- External links UX: verified all external links use <.external_link>
component with target="_blank", rel="noopener noreferrer", icon, and
screen reader text
- Input styling WCAG compliance: increase input border contrast from
~3.3:1 to ~4.5-5:1 across all theme moods (neutral, warm, cool, dark)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 07:56:54 +00:00
|
|
|
|
|
|
|
|
# Add key format validation error if key is present
|
|
|
|
|
form =
|
|
|
|
|
if api_key != "" do
|
|
|
|
|
case KeyValidation.validate_provider_key(api_key, provider_type) do
|
|
|
|
|
{:ok, _} ->
|
|
|
|
|
to_form(changeset)
|
|
|
|
|
|
|
|
|
|
{:error, message} ->
|
|
|
|
|
changeset
|
|
|
|
|
|> Ecto.Changeset.add_error(:api_key, message)
|
|
|
|
|
|> to_form()
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
to_form(changeset)
|
|
|
|
|
end
|
2026-01-31 22:08:34 +00:00
|
|
|
|
|
|
|
|
# Store api_key separately since changeset encrypts it immediately
|
complete onboarding UX v2
Tasks C, H, I from the plan:
- Forgiving API key validation: add Printify UUID format and Printful
length validation, validate on blur for fast feedback, helpful error
messages with specific guidance
- External links UX: verified all external links use <.external_link>
component with target="_blank", rel="noopener noreferrer", icon, and
screen reader text
- Input styling WCAG compliance: increase input border contrast from
~3.3:1 to ~4.5-5:1 across all theme moods (neutral, warm, cool, dark)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-10 07:56:54 +00:00
|
|
|
{:noreply, assign(socket, form: form, pending_api_key: api_key)}
|
2026-01-31 22:08:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
|
def handle_event("save", %{"provider_connection" => params}, socket) do
|
|
|
|
|
save_connection(socket, socket.assigns.live_action, params)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp save_connection(socket, :new, params) do
|
2026-03-03 15:19:17 +00:00
|
|
|
api_key = params["api_key"] || socket.assigns[:pending_api_key]
|
2026-02-15 10:53:15 +00:00
|
|
|
provider_type = socket.assigns.provider_type
|
|
|
|
|
|
2026-03-04 12:17:56 +00:00
|
|
|
case KeyValidation.validate_provider_key(api_key, provider_type) do
|
|
|
|
|
{:error, message} ->
|
|
|
|
|
form = form_with_api_key_error(socket, api_key, message)
|
|
|
|
|
{:noreply, assign(socket, :form, form)}
|
|
|
|
|
|
|
|
|
|
{:ok, api_key} ->
|
|
|
|
|
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, _reason} ->
|
|
|
|
|
form =
|
|
|
|
|
form_with_api_key_error(
|
|
|
|
|
socket,
|
|
|
|
|
api_key,
|
|
|
|
|
"Could not connect. Check your API key and try again"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
{:noreply, assign(socket, :form, form)}
|
|
|
|
|
end
|
2026-01-31 22:08:34 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp save_connection(socket, :edit, params) do
|
|
|
|
|
case Products.update_provider_connection(socket.assigns.connection, params) do
|
|
|
|
|
{:ok, _connection} ->
|
|
|
|
|
{:noreply,
|
|
|
|
|
socket
|
|
|
|
|
|> put_flash(:info, "Settings saved")
|
2026-02-12 09:04:51 +00:00
|
|
|
|> push_navigate(to: ~p"/admin/settings")}
|
2026-01-31 22:08:34 +00:00
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
|
{:noreply, assign(socket, form: to_form(changeset))}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-03-04 12:17:56 +00:00
|
|
|
defp form_with_api_key_error(socket, api_key, message) do
|
|
|
|
|
socket.assigns.connection
|
|
|
|
|
|> ProviderConnection.changeset(%{"api_key" => api_key})
|
|
|
|
|
|> Ecto.Changeset.add_error(:api_key, message)
|
|
|
|
|
|> Map.put(:action, :validate)
|
|
|
|
|
|> to_form()
|
2026-01-31 22:08:34 +00:00
|
|
|
end
|
|
|
|
|
|
2026-02-15 10:53:15 +00:00
|
|
|
defp validated_type(type) when type in @supported_types, do: type
|
|
|
|
|
defp validated_type(_), do: "printify"
|
2026-01-31 22:08:34 +00:00
|
|
|
end
|