2026-03-09 14:47:50 +00:00
|
|
|
defmodule BerrypodWeb.Shop.Pages.Contact do
|
|
|
|
|
@moduledoc """
|
|
|
|
|
Contact page handler for the unified Shop.Page LiveView.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import Phoenix.Component, only: [assign: 3]
|
|
|
|
|
import Phoenix.LiveView, only: [push_navigate: 2, put_flash: 3]
|
2026-01-17 21:52:11 +00:00
|
|
|
|
2026-02-28 18:57:51 +00:00
|
|
|
alias Berrypod.{ContactNotifier, Orders}
|
2026-02-24 08:40:08 +00:00
|
|
|
alias Berrypod.Orders.OrderNotifier
|
2026-02-26 18:29:20 +00:00
|
|
|
alias Berrypod.Pages
|
2026-02-24 08:40:08 +00:00
|
|
|
alias BerrypodWeb.OrderLookupController
|
|
|
|
|
|
2026-03-09 14:47:50 +00:00
|
|
|
def init(socket, _params, _uri) do
|
2026-02-26 18:29:20 +00:00
|
|
|
page = Pages.get_page("contact")
|
|
|
|
|
|
2026-03-09 14:47:50 +00:00
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:page_title, "Contact")
|
|
|
|
|
|> assign(
|
|
|
|
|
:page_description,
|
|
|
|
|
"Get in touch with us for any questions or help with your order."
|
|
|
|
|
)
|
|
|
|
|
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/contact")
|
|
|
|
|
|> assign(:tracking_state, :idle)
|
|
|
|
|
|> assign(:page, page)
|
|
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_params(_params, _uri, socket) do
|
|
|
|
|
{:noreply, socket}
|
2026-02-24 08:40:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_event("lookup_orders", %{"email" => email}, socket) do
|
|
|
|
|
orders = Orders.list_orders_by_email(email)
|
|
|
|
|
|
|
|
|
|
state =
|
|
|
|
|
if orders == [] do
|
|
|
|
|
:not_found
|
|
|
|
|
else
|
|
|
|
|
token = OrderLookupController.generate_token(email)
|
2026-03-09 14:47:50 +00:00
|
|
|
link = BerrypodWeb.Endpoint.url() <> "/orders/verify/#{token}"
|
2026-02-24 08:40:08 +00:00
|
|
|
OrderNotifier.deliver_order_lookup(email, link)
|
|
|
|
|
:sent
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
{:noreply, assign(socket, :tracking_state, state)}
|
|
|
|
|
end
|
|
|
|
|
|
2026-02-28 18:57:51 +00:00
|
|
|
def handle_event("send_contact", params, socket) do
|
|
|
|
|
case ContactNotifier.deliver_contact_message(params) do
|
|
|
|
|
{:ok, _} ->
|
|
|
|
|
{:noreply,
|
|
|
|
|
socket
|
|
|
|
|
|> put_flash(:info, "Message sent! We'll get back to you soon.")
|
2026-03-09 14:47:50 +00:00
|
|
|
|> push_navigate(to: "/contact")}
|
2026-02-28 18:57:51 +00:00
|
|
|
|
|
|
|
|
{:error, :invalid_params} ->
|
|
|
|
|
{:noreply, put_flash(socket, :error, "Please fill in all required fields.")}
|
|
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
|
{:noreply, put_flash(socket, :error, "Sorry, something went wrong. Please try again.")}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-02-24 08:40:08 +00:00
|
|
|
def handle_event("reset_tracking", _params, socket) do
|
|
|
|
|
{:noreply, assign(socket, :tracking_state, :idle)}
|
2026-01-17 21:52:11 +00:00
|
|
|
end
|
2026-01-17 22:17:59 +00:00
|
|
|
|
2026-03-09 14:47:50 +00:00
|
|
|
def handle_event(_event, _params, _socket), do: :cont
|
2026-01-17 21:52:11 +00:00
|
|
|
end
|