spazio-solazzo/lib/spazio_solazzo_web/live/booking/booking_cancellation_live.ex
Víctor Martínez 69f992f8f6
feat: new booking system + admin dashboard (#12)
feat: implement a new booking system and admin dashboard
2026-02-07 19:08:39 +01:00

63 lines
1.9 KiB
Elixir

defmodule SpazioSolazzoWeb.BookingCancellationLive do
use SpazioSolazzoWeb, :live_view
alias SpazioSolazzo.BookingSystem
alias SpazioSolazzo.BookingSystem.Booking.Token
def mount(%{"token" => token}, _session, socket) do
case Token.verify(token) do
{:ok, %{booking_id: booking_id, action: :cancel}} ->
handle_booking(booking_id, socket)
{:error, _} ->
{:ok,
socket
|> put_flash(:error, "Invalid or expired cancellation link")
|> push_navigate(to: "/")}
end
end
defp handle_booking(booking_id, socket) do
case Ash.get(SpazioSolazzo.BookingSystem.Booking, booking_id, load: [:space]) do
{:ok, %{state: state} = booking} when state in [:requested, :accepted] ->
{:ok,
assign(socket,
booking: booking,
cancellation_reason: "",
show_success: false
)}
{:ok, _} ->
{:ok,
socket
|> put_flash(:error, "This booking has already been cancelled or completed")
|> push_navigate(to: "/")}
{:error, _} ->
{:ok,
socket
|> put_flash(:error, "Booking not found")
|> push_navigate(to: "/")}
end
end
def handle_event("validate", %{"reason" => reason}, socket) do
{:noreply, assign(socket, cancellation_reason: reason)}
end
def handle_event("cancel_booking", %{"reason" => reason}, socket) do
if String.trim(reason) == "" do
{:noreply, put_flash(socket, :error, "Please provide a reason for cancellation")}
else
booking = socket.assigns.booking
case BookingSystem.cancel_booking(booking, reason) do
{:ok, _cancelled_booking} ->
{:noreply, assign(socket, show_success: true)}
{:error, _error} ->
{:noreply, put_flash(socket, :error, "Failed to cancel booking. Please try again.")}
end
end
end
end