rename project from SimpleshopTheme to Berrypod

All modules, configs, paths, and references updated.
836 tests pass, zero warnings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jamey
2026-02-18 21:23:15 +00:00
parent c65e777832
commit 9528700862
300 changed files with 23932 additions and 1349 deletions

View File

@@ -0,0 +1,163 @@
defmodule BerrypodWeb.Admin.Providers.Form do
use BerrypodWeb, :live_view
alias Berrypod.Products
alias Berrypod.Products.ProviderConnection
alias Berrypod.Providers
@supported_types ~w(printify printful)
@impl true
def mount(params, _session, socket) do
{:ok, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :new, params) do
provider_type = validated_type(params["type"])
socket
|> assign(:page_title, "Connect to #{provider_label(provider_type)}")
|> assign(:provider_type, provider_type)
|> assign(:connection, %ProviderConnection{provider_type: provider_type})
|> assign(:form, to_form(ProviderConnection.changeset(%ProviderConnection{}, %{})))
|> assign(:testing, false)
|> assign(:test_result, nil)
|> assign(:pending_api_key, nil)
end
defp apply_action(socket, :edit, %{"id" => id}) do
connection = Products.get_provider_connection!(id)
socket
|> assign(:page_title, "#{provider_label(connection.provider_type)} settings")
|> assign(:provider_type, connection.provider_type)
|> assign(:connection, connection)
|> assign(:form, to_form(ProviderConnection.changeset(connection, %{})))
|> assign(:testing, false)
|> assign(:test_result, nil)
|> assign(:pending_api_key, nil)
end
@impl true
def handle_event("validate", %{"provider_connection" => params}, socket) do
form =
socket.assigns.connection
|> ProviderConnection.changeset(params)
|> Map.put(:action, :validate)
|> to_form()
# Store api_key separately since changeset encrypts it immediately
{:noreply, assign(socket, form: form, pending_api_key: params["api_key"])}
end
@impl true
def handle_event("test_connection", _params, socket) do
socket = assign(socket, testing: true, test_result: nil)
api_key =
socket.assigns[:pending_api_key] ||
ProviderConnection.get_api_key(socket.assigns.connection)
if api_key && api_key != "" do
temp_conn = %ProviderConnection{
provider_type: socket.assigns.provider_type,
api_key_encrypted: encrypt_api_key(api_key)
}
result = Providers.test_connection(temp_conn)
{:noreply, assign(socket, testing: false, test_result: result)}
else
{:noreply, assign(socket, testing: false, test_result: {:error, :no_api_key})}
end
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
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
{:ok, _connection} ->
{:noreply,
socket
|> put_flash(:info, "Connected to #{provider_label(provider_type)}!")
|> push_navigate(to: ~p"/admin/settings")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
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")
|> push_navigate(to: ~p"/admin/settings")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
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
Map.put_new(params, "name", provider_label(type))
end
defp encrypt_api_key(api_key) do
case Berrypod.Vault.encrypt(api_key) do
{:ok, encrypted} -> encrypted
_ -> nil
end
end
defp validated_type(type) when type in @supported_types, do: type
defp validated_type(_), do: "printify"
# Shared helpers used by the template
defp provider_label("printful"), do: "Printful"
defp provider_label(_), do: "Printify"
defp connection_name({:ok, %{shop_name: name}}), do: name
defp connection_name({:ok, %{store_name: name}}), do: name
defp connection_name(_), do: nil
defp format_error(:no_api_key), do: "Please enter your API key"
defp format_error(:unauthorized), do: "That key doesn't seem to be valid"
defp format_error(:timeout), do: "Couldn't reach the provider - try again"
defp format_error({:http_error, _code}), do: "Something went wrong - try again"
defp format_error(error) when is_binary(error), do: error
defp format_error(_), do: "Connection failed - check your key and try again"
end

View File

@@ -0,0 +1,135 @@
<.header>
{if @live_action == :new,
do: "Connect to #{provider_label(@provider_type)}",
else: "#{provider_label(@provider_type)} settings"}
</.header>
<div class="max-w-xl mt-6">
<%= if @live_action == :new do %>
<div class="prose prose-sm mb-6">
<p>
{provider_label(@provider_type)} is a print-on-demand service that prints and ships products for you.
Connect your account to automatically import your products into your shop.
</p>
</div>
<%= if @provider_type == "printify" do %>
<div class="rounded-lg bg-base-200 p-4 mb-6 text-sm">
<p class="font-medium mb-2">Get your API key from Printify:</p>
<ol class="list-decimal list-inside space-y-1 text-base-content/80">
<li>
<a
href="https://printify.com/app/auth/login"
target="_blank"
rel="noopener"
class="admin-link"
>
Log in to Printify
</a>
(or <a
href="https://printify.com/app/auth/register"
target="_blank"
rel="noopener"
class="admin-link"
>create a free account</a>)
</li>
<li>Click <strong>Account</strong> (top right)</li>
<li>Select <strong>Connections</strong> from the dropdown</li>
<li>Find <strong>API tokens</strong> and click <strong>Generate</strong></li>
<li>
Enter a name (e.g. "My Shop"), keep <strong>all scopes</strong>
selected, and click <strong>Generate token</strong>
</li>
<li>Click <strong>Copy to clipboard</strong> and paste it below</li>
</ol>
</div>
<% else %>
<div class="rounded-lg bg-base-200 p-4 mb-6 text-sm">
<p class="font-medium mb-2">Get your API key from Printful:</p>
<ol class="list-decimal list-inside space-y-1 text-base-content/80">
<li>
<a
href="https://www.printful.com/auth/login"
target="_blank"
rel="noopener"
class="admin-link"
>
Log in to Printful
</a>
(or <a
href="https://www.printful.com/auth/signup"
target="_blank"
rel="noopener"
class="admin-link"
>create a free account</a>)
</li>
<li>Go to <strong>Settings</strong> &rarr; <strong>API access</strong></li>
<li>Click <strong>Create API key</strong></li>
<li>Give it a name and select <strong>all scopes</strong></li>
<li>Copy the token and paste it below</li>
</ol>
</div>
<% end %>
<% end %>
<.form for={@form} id="provider-form" phx-change="validate" phx-submit="save">
<input type="hidden" name="provider_connection[provider_type]" value={@provider_type} />
<.input
field={@form[:api_key]}
type="password"
label={"#{provider_label(@provider_type)} API key"}
placeholder={
if @live_action == :edit,
do: "Leave blank to keep current key",
else: "Paste your key here"
}
autocomplete="off"
/>
<div class="flex items-center gap-3 mb-6">
<button
type="button"
class="admin-btn admin-btn-outline admin-btn-sm"
phx-click="test_connection"
disabled={@testing}
>
<.icon
name={if @testing, do: "hero-arrow-path", else: "hero-signal"}
class={if @testing, do: "size-4 animate-spin", else: "size-4"}
/>
{if @testing, do: "Checking...", else: "Check connection"}
</button>
<div :if={@test_result} class="text-sm">
<%= case @test_result do %>
<% {:ok, _info} -> %>
<span class="text-success flex items-center gap-1">
<.icon name="hero-check-circle" class="size-4" />
Connected to {connection_name(@test_result) || provider_label(@provider_type)}
</span>
<% {:error, reason} -> %>
<span class="text-error flex items-center gap-1">
<.icon name="hero-x-circle" class="size-4" />
{format_error(reason)}
</span>
<% end %>
</div>
</div>
<%= if @live_action == :edit do %>
<.input field={@form[:enabled]} type="checkbox" label="Connection enabled" />
<% end %>
<div class="flex gap-2 mt-6">
<.button type="submit" disabled={@testing}>
{if @live_action == :new,
do: "Connect to #{provider_label(@provider_type)}",
else: "Save changes"}
</.button>
<.link navigate={~p"/admin/providers"} class="admin-btn admin-btn-ghost">
Cancel
</.link>
</div>
</.form>
</div>

View File

@@ -0,0 +1,98 @@
defmodule BerrypodWeb.Admin.Providers.Index do
use BerrypodWeb, :live_view
alias Berrypod.Products
alias Berrypod.Products.ProviderConnection
@impl true
def mount(_params, _session, socket) do
connections = Products.list_provider_connections()
{:ok,
socket
|> assign(:page_title, "Provider connections")
|> stream(:connections, connections)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
connection = Products.get_provider_connection!(id)
{:ok, _} = Products.delete_provider_connection(connection)
{:noreply,
socket
|> stream_delete(:connections, connection)
|> put_flash(:info, "Provider connection deleted")}
end
@impl true
def handle_event("sync", %{"id" => id}, socket) do
connection = Products.get_provider_connection!(id)
case Products.enqueue_sync(connection) do
{:ok, _job} ->
# Update the connection status in the stream
updated = %{connection | sync_status: "syncing"}
{:noreply,
socket
|> stream_insert(:connections, updated)
|> put_flash(:info, "Sync started for #{connection.name}")}
{:error, _reason} ->
{:noreply, put_flash(socket, :error, "Failed to start sync")}
end
end
# Function components for the template
attr :status, :string, required: true
attr :enabled, :boolean, required: true
defp status_indicator(assigns) do
~H"""
<span class={[
"inline-flex size-3 rounded-full",
cond do
not @enabled -> "bg-base-content/30"
@status == "syncing" -> "bg-warning animate-pulse"
@status == "completed" -> "bg-success"
@status == "failed" -> "bg-error"
true -> "bg-base-content/30"
end
]} />
"""
end
attr :connection, ProviderConnection, required: true
defp connection_info(assigns) do
product_count = Products.count_products_for_connection(assigns.connection.id)
assigns = assign(assigns, :product_count, product_count)
~H"""
<span>
<.icon name="hero-cube" class="size-4 inline" />
{@product_count} {if @product_count == 1, do: "product", else: "products"}
</span>
<span :if={@connection.last_synced_at}>
<.icon name="hero-clock" class="size-4 inline" />
Last synced {format_relative_time(@connection.last_synced_at)}
</span>
<span :if={!@connection.last_synced_at} class="text-warning">
<.icon name="hero-exclamation-triangle" class="size-4 inline" /> Never synced
</span>
"""
end
defp format_relative_time(datetime) do
diff = DateTime.diff(DateTime.utc_now(), datetime, :second)
cond do
diff < 60 -> "just now"
diff < 3600 -> "#{div(diff, 60)} min ago"
diff < 86400 -> "#{div(diff, 3600)} hours ago"
true -> "#{div(diff, 86400)} days ago"
end
end
end

View File

@@ -0,0 +1,94 @@
<.header>
Providers
<:actions>
<div class="admin-dropdown">
<div tabindex="0" role="button" class="admin-btn admin-btn-primary">
<.icon name="hero-plus" class="size-4 mr-1" /> Connect provider
</div>
<ul tabindex="0" class="admin-dropdown-content">
<li>
<.link navigate={~p"/admin/providers/new?type=printify"}>Printify</.link>
</li>
<li>
<.link navigate={~p"/admin/providers/new?type=printful"}>Printful</.link>
</li>
</ul>
</div>
</:actions>
</.header>
<div id="connections" phx-update="stream" class="mt-6 space-y-4">
<div id="connections-empty" class="hidden only:block text-center py-12">
<.icon name="hero-cube" class="size-16 mx-auto mb-4 text-base-content/30" />
<h2 class="text-xl font-medium">Connect a print-on-demand provider</h2>
<p class="mt-2 text-base-content/60 max-w-md mx-auto">
Connect your Printify or Printful account to import products
and start selling.
</p>
<div class="flex justify-center gap-3 mt-6">
<.button navigate={~p"/admin/providers/new?type=printify"}>
Connect Printify
</.button>
<.button navigate={~p"/admin/providers/new?type=printful"} class="admin-btn-outline">
Connect Printful
</.button>
</div>
</div>
<div
:for={{dom_id, connection} <- @streams.connections}
id={dom_id}
class="admin-card"
>
<div class="admin-card-body">
<div class="flex items-start justify-between gap-4">
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2">
<.status_indicator status={connection.sync_status} enabled={connection.enabled} />
<h3 class="font-semibold text-lg">
{String.capitalize(connection.provider_type)}
</h3>
</div>
<p class="text-base-content/70 mt-1">{connection.name}</p>
<div class="flex flex-wrap gap-x-4 gap-y-1 mt-2 text-sm text-base-content/60">
<.connection_info connection={connection} />
</div>
</div>
<div class="flex items-center gap-2">
<.link
navigate={~p"/admin/providers/#{connection.id}/edit"}
class="admin-btn admin-btn-ghost admin-btn-sm"
>
Settings
</.link>
<button
phx-click="delete"
phx-value-id={connection.id}
data-confirm={"Disconnect from #{String.capitalize(connection.provider_type)}? Your synced products will remain in your shop."}
class="admin-btn admin-btn-ghost admin-btn-sm text-error"
>
Disconnect
</button>
</div>
</div>
<div class="admin-card-actions">
<button
phx-click="sync"
phx-value-id={connection.id}
disabled={connection.sync_status == "syncing"}
class="admin-btn admin-btn-outline admin-btn-sm"
>
<.icon
name="hero-arrow-path"
class={
if connection.sync_status == "syncing", do: "size-4 animate-spin", else: "size-4"
}
/>
{if connection.sync_status == "syncing", do: "Syncing...", else: "Sync products"}
</button>
</div>
</div>
</div>
</div>