Skip to content

Commit

Permalink
store form state in server side assigns
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Apr 26, 2024
1 parent c458162 commit a369571
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ defmodule HamsterTravelWeb.Planning.Trips.FormComponent do
<div>
<.form_container>
<.form
id="trip-form"
for={@form}
as={:trip}
phx-submit="form_submit"
Expand Down Expand Up @@ -155,52 +156,53 @@ defmodule HamsterTravelWeb.Planning.Trips.FormComponent do
# convert dates_unknown to boolean
dates_unknown = dates_unknown == "true"

new_changeset =
trip_params =
trip_params
|> Map.merge(%{
"start_date" => assigns.start_date,
"end_date" => assigns.end_date,
"duration" => Dates.duration(assigns.start_date, assigns.end_date)
})
|> Planning.trip_changeset()

{:noreply,
socket
|> assign(:dates_unknown, dates_unknown)
|> assign_form(new_changeset)}
|> assign_form(trip_params)}
end

def handle_event(
"form_changed",
%{
"_target" => ["trip", "start_date"],
"trip" => %{"start_date" => start_date}
"trip" => %{"start_date" => start_date} = trip_params
},
socket
) do
{:noreply,
socket
|> assign(start_date: start_date)}
|> assign(start_date: start_date)
|> assign_form(trip_params)}
end

def handle_event(
"form_changed",
%{
"_target" => ["trip", "end_date"],
"trip" => %{"end_date" => end_date}
"trip" => %{"end_date" => end_date} = trip_params
},
socket
) do
{:noreply,
socket
|> assign(end_date: end_date)}
|> assign(end_date: end_date)
|> assign_form(trip_params)}
end

def handle_event(
"form_changed",
%{
"_target" => ["trip", "status"],
"trip" => %{"status" => status}
"trip" => %{"status" => status} = trip_params
},
socket
) do
Expand All @@ -212,18 +214,29 @@ defmodule HamsterTravelWeb.Planning.Trips.FormComponent do
socket
end

trip_params =
if status == Trip.finished() do
trip_params
|> Map.merge(%{"dates_unknown" => false})
else
trip_params
end

{:noreply,
socket
|> assign(status: status)}
|> assign(status: status)
|> assign_form(trip_params)}
end

@impl true
def handle_event(
"form_changed",
_,
%{
"trip" => trip_params
},
socket
) do
{:noreply, socket}
{:noreply, socket |> assign_form(trip_params)}
end

@impl true
Expand Down Expand Up @@ -270,4 +283,8 @@ defmodule HamsterTravelWeb.Planning.Trips.FormComponent do
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end

defp assign_form(socket, trip_params) when is_map(trip_params) do
assign_form(socket, Planning.trip_changeset(trip_params))
end
end
4 changes: 2 additions & 2 deletions lib/hamster_travel_web/live/planning_live/show_trip.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<.button link_type="live_redirect" to={trip_url(@trip.slug, :edit)} color="secondary">
<.icon_text icon={:pencil} label={gettext("Edit")} />
</.button>
<.link href={trip_url(@trip.slug, :copy)}>
<%!-- <.link href={trip_url(@trip.slug, :copy)}>
<%= gettext("Make a copy") %>
</.link>
<.link href={trip_url(@trip.slug, :pdf)}>
<%= gettext("Export as PDF") %>
</.link>
<.link href={trip_url(@trip.slug, :delete)}>
<%= gettext("Delete") %>
</.link>
</.link> --%>
</.inline>
<.status_row trip={@trip} />
</div>
Expand Down

0 comments on commit a369571

Please sign in to comment.