refactor booking form to have server side validation

This commit is contained in:
JasterV 2026-02-03 00:56:43 +01:00
parent fa575ceb41
commit cf2191eca9
3 changed files with 71 additions and 64 deletions

View file

@ -65,6 +65,18 @@ defmodule SpazioSolazzoWeb.Admin.WalkInLive do
end
end
def handle_info({:multi_day_mode_changed, multi_day}, socket) do
{:noreply, assign(socket, multi_day_mode: multi_day)}
end
def handle_info({:date_selected, start_date, end_date}, socket) do
{:noreply, assign(socket, start_date: start_date, end_date: end_date)}
end
def handle_info(_msg, socket) do
{:noreply, socket}
end
defp create_walk_in(
form,
%{
@ -127,18 +139,6 @@ defmodule SpazioSolazzoWeb.Admin.WalkInLive do
end
end
def handle_info({:multi_day_mode_changed, multi_day}, socket) do
{:noreply, assign(socket, multi_day_mode: multi_day)}
end
def handle_info({:date_selected, start_date, end_date}, socket) do
{:noreply, assign(socket, start_date: start_date, end_date: end_date)}
end
def handle_info(_msg, socket) do
{:noreply, socket}
end
defp days_selected(nil, nil), do: 0
defp days_selected(start_date, nil) when not is_nil(start_date), do: 1

View file

@ -12,7 +12,6 @@ defmodule SpazioSolazzoWeb.BookingFormLiveComponent do
initial_data = %{
"customer_name" => current_user.name,
"customer_email" => current_user.email,
"customer_phone" => current_user.phone_number || "",
"customer_comment" => ""
}
@ -31,10 +30,9 @@ defmodule SpazioSolazzoWeb.BookingFormLiveComponent do
def handle_event("submit_booking", params, socket) do
booking_data = %{
customer_name: params["customer_name"] || "",
customer_email: params["customer_email"],
customer_phone: params["customer_phone"] || "",
customer_comment: params["customer_comment"] || ""
customer_name: String.trim(params["customer_name"] || ""),
customer_phone: String.trim(params["customer_phone"] || ""),
customer_comment: String.trim(params["customer_comment"] || "")
}
send(self(), {:create_booking, booking_data})
@ -92,31 +90,19 @@ defmodule SpazioSolazzoWeb.BookingFormLiveComponent do
placeholder="Your full name"
/>
<%= if @current_user do %>
<div>
<label class="block text-sm font-medium text-base-content mb-2">
Email
</label>
<div class="flex items-center gap-3 p-4 bg-secondary/5 rounded-xl border border-base-200">
<div class="flex-shrink-0">
<.icon name="hero-envelope" class="size-5 text-secondary" />
</div>
<span class="text-sm font-medium text-base-content truncate">
{@current_user.email}
</span>
<div>
<label class="block text-sm font-medium text-base-content mb-2">
Email
</label>
<div class="flex items-center gap-3 p-4 bg-secondary/5 rounded-xl border border-base-200">
<div class="flex-shrink-0">
<.icon name="hero-envelope" class="size-5 text-secondary" />
</div>
<span class="text-sm font-medium text-base-content truncate">
{@current_user.email}
</span>
</div>
<% else %>
<.input
name="customer_email"
id="customer_email"
type="email"
label="Email *"
value={@form[:customer_email].value}
required
placeholder="your@email.com"
/>
<% end %>
</div>
<.input
name="customer_phone"

View file

@ -52,6 +52,41 @@ defmodule SpazioSolazzoWeb.SpaceBookingLive do
end
def handle_info({:create_booking, booking_data}, socket) do
case parse_booking_data(booking_data) do
{:error, error} -> {:noreply, put_flash(socket, :error, error)}
{:ok, booking_data} -> create_booking(booking_data, socket)
end
end
def handle_info({:date_selected, date}, socket) do
time_slots =
load_time_slots_with_stats(socket.assigns.space, date, socket.assigns.current_user)
{:noreply,
socket
|> assign(
selected_date: date,
time_slots: time_slots
)}
end
def handle_info(
%{topic: "booking:" <> _event, payload: %{data: %{space_id: space_id, date: date}}},
%{assigns: %{space: %{id: space_id}, selected_date: date}} = socket
) do
time_slots =
load_time_slots_with_stats(socket.assigns.space, date, socket.assigns.current_user)
{:noreply,
socket
|> assign(time_slots: time_slots)}
end
def handle_info(_msg, socket) do
{:noreply, socket}
end
defp create_booking(booking_data, socket) do
current_user = socket.assigns.current_user
result =
@ -84,32 +119,18 @@ defmodule SpazioSolazzoWeb.SpaceBookingLive do
end
end
def handle_info({:date_selected, date}, socket) do
time_slots =
load_time_slots_with_stats(socket.assigns.space, date, socket.assigns.current_user)
{:noreply,
socket
|> assign(
selected_date: date,
time_slots: time_slots
)}
defp parse_booking_data(%{customer_name: ""}) do
{:error, "Please fill all the required fields to create a booking request."}
end
def handle_info(
%{topic: "booking:" <> _event, payload: %{data: %{space_id: space_id, date: date}}},
%{assigns: %{space: %{id: space_id}, selected_date: date}} = socket
) do
time_slots =
load_time_slots_with_stats(socket.assigns.space, date, socket.assigns.current_user)
{:noreply,
socket
|> assign(time_slots: time_slots)}
end
def handle_info(_msg, socket) do
{:noreply, socket}
defp parse_booking_data(
%{
customer_name: _,
customer_phone: _,
customer_comment: _
} = form
) do
{:ok, form}
end
defp load_time_slots_with_stats(space, date, current_user) do