Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions lib/arrow/disruptions/limit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule Arrow.Disruptions.Limit do
])
|> put_change(:check_for_overlap, true)
|> validate_required([:start_date, :end_date, :route_id, :start_stop_id, :end_stop_id])
|> validate_start_date_before_end_date()
|> Arrow.Util.Validation.validate_start_date_before_end_date()
|> cast_assoc_day_of_weeks()
|> exclusion_constraint(:end_date,
name: :no_overlap,
Expand Down Expand Up @@ -73,23 +73,6 @@ defmodule Arrow.Disruptions.Limit do
|> struct!(attrs)
end

@spec validate_start_date_before_end_date(Ecto.Changeset.t(t())) :: Ecto.Changeset.t(t())
defp validate_start_date_before_end_date(changeset) do
start_date = get_field(changeset, :start_date)
end_date = get_field(changeset, :end_date)

cond do
is_nil(start_date) or is_nil(end_date) ->
changeset

Date.compare(start_date, end_date) == :gt ->
add_error(changeset, :start_date, "start date should not be after end date")

true ->
changeset
end
end

@spec cast_assoc_day_of_weeks(Ecto.Changeset.t(t())) :: Ecto.Changeset.t(t())
defp cast_assoc_day_of_weeks(changeset) do
start_date = get_field(changeset, :start_date)
Expand Down
19 changes: 1 addition & 18 deletions lib/arrow/disruptions/replacement_service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,11 @@ defmodule Arrow.Disruptions.ReplacementService do
:disruption_id,
:shuttle_id
])
|> validate_start_date_before_end_date()
|> Arrow.Util.Validation.validate_start_date_before_end_date()
|> assoc_constraint(:shuttle)
|> assoc_constraint(:disruption)
end

@spec validate_start_date_before_end_date(Ecto.Changeset.t(t())) :: Ecto.Changeset.t(t())
defp validate_start_date_before_end_date(changeset) do
start_date = get_field(changeset, :start_date)
end_date = get_field(changeset, :end_date)

cond do
is_nil(start_date) or is_nil(end_date) ->
changeset

Date.compare(start_date, end_date) == :gt ->
add_error(changeset, :start_date, "start date should not be after end date")

true ->
changeset
end
end

def add_timetable(%__MODULE__{} = replacement_service) do
timetable =
schedule_service_types()
Expand Down
13 changes: 1 addition & 12 deletions lib/arrow/gtfs/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,7 @@ defmodule Arrow.Gtfs.Calendar do
~w[service_id monday tuesday wednesday thursday friday saturday sunday start_date end_date]a
)
|> assoc_constraint(:service)
|> validate_start_date_not_after_end_date()
end

defp validate_start_date_not_after_end_date(changeset) do
start_date = fetch_field!(changeset, :start_date)
end_date = fetch_field!(changeset, :end_date)

if Date.compare(start_date, end_date) in [:lt, :eq] do
changeset
else
add_error(changeset, :dates, "start date should not be after end date")
end
|> Arrow.Util.Validation.validate_start_date_before_end_date()
end

@impl Arrow.Gtfs.Importable
Expand Down
13 changes: 1 addition & 12 deletions lib/arrow/gtfs/feed_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ defmodule Arrow.Gtfs.FeedInfo do
|> validate_required(
~w[id publisher_name publisher_url lang start_date end_date version contact_email]a
)
|> validate_start_date_before_end_date()
end

defp validate_start_date_before_end_date(changeset) do
start_date = fetch_field!(changeset, :start_date)
end_date = fetch_field!(changeset, :end_date)

if Date.compare(start_date, end_date) == :lt do
changeset
else
add_error(changeset, :dates, "start date should be before end date")
end
|> Arrow.Util.Validation.validate_start_date_before_end_date()
end

@impl Arrow.Gtfs.Importable
Expand Down
19 changes: 1 addition & 18 deletions lib/arrow/hastus/service_date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Arrow.Hastus.ServiceDate do
service_date
|> cast(attrs, [:start_date, :end_date, :service_id])
|> validate_required([:start_date, :end_date])
|> validate_start_date_before_end_date()
|> Arrow.Util.Validation.validate_start_date_before_end_date()
|> assoc_constraint(:service)
end

Expand All @@ -31,21 +31,4 @@ defmodule Arrow.Hastus.ServiceDate do
|> Stream.take(7)
|> MapSet.new(&Date.day_of_week/1)
end

@spec validate_start_date_before_end_date(Ecto.Changeset.t(t())) :: Ecto.Changeset.t(t())
defp validate_start_date_before_end_date(changeset) do
start_date = get_field(changeset, :start_date)
end_date = get_field(changeset, :end_date)

cond do
is_nil(start_date) or is_nil(end_date) ->
changeset

Date.compare(start_date, end_date) == :gt ->
add_error(changeset, :start_date, "start date must be less than or equal to end date")

true ->
changeset
end
end
end
6 changes: 5 additions & 1 deletion lib/arrow/trainsformer/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ defmodule Arrow.Trainsformer.Service do
def changeset(service, attrs) do
service
|> cast(attrs, [:name, :export_id])
|> cast_assoc(:service_dates,
with: &ServiceDate.changeset/2,
sort_param: :service_dates_sort,
drop_param: :service_dates_drop
)
|> validate_required([:name])
|> cast_assoc(:service_dates, with: &ServiceDate.changeset/2)
|> assoc_constraint(:export)
end
end
5 changes: 3 additions & 2 deletions lib/arrow/trainsformer/service_date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ defmodule Arrow.Trainsformer.ServiceDate do
alias Arrow.Trainsformer.ServiceDateDayOfWeek

typed_schema "trainsformer_service_dates" do
field :start_date, :date
field :end_date, :date
field :start_date, :date, default: Date.utc_today()
field :end_date, :date, default: Date.utc_today()
belongs_to :service, Arrow.Trainsformer.Service, on_replace: :delete

has_many :service_date_days_of_week, ServiceDateDayOfWeek,
Expand Down Expand Up @@ -39,6 +39,7 @@ defmodule Arrow.Trainsformer.ServiceDate do
|> cast(transformed_attrs, [:start_date, :end_date, :service_id])
|> cast_assoc(:service_date_days_of_week, with: &ServiceDateDayOfWeek.changeset/2)
|> validate_required([:start_date, :end_date])
|> Arrow.Util.Validation.validate_start_date_before_end_date()
|> assoc_constraint(:service)
end
end
26 changes: 26 additions & 0 deletions lib/arrow/util/validation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Arrow.Util.Validation do
@moduledoc """
Utilities for validating changesets in Arrow. Note that many these functions make assumptions
about the field names / error messages in your changeset that are specific to this project.
"""
@spec validate_start_date_before_end_date(Ecto.Changeset.t(any())) :: Ecto.Changeset.t(any())
def validate_start_date_before_end_date(changeset) do
start_date = Ecto.Changeset.get_field(changeset, :start_date)
end_date = Ecto.Changeset.get_field(changeset, :end_date)

cond do
is_nil(start_date) or is_nil(end_date) ->
changeset

Date.compare(start_date, end_date) == :gt ->
Ecto.Changeset.add_error(
changeset,
:start_date,
"start date must be less than or equal to end date"
)

true ->
changeset
end
end
end
2 changes: 1 addition & 1 deletion lib/arrow_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule ArrowWeb.CoreComponents do
<button
phx-click={JS.exec("data-cancel", to: "##{@id}")}
type="button"
class="-m-3 flex-none p-3 opacity-20 hover:opacity-40"
class="-m-3 flex-none p-3 opacity-20 hover:opacity-140"
aria-label={gettext("close")}
>
<.icon name="hero-x-mark-solid" class="h-5 w-5" />
Expand Down
16 changes: 8 additions & 8 deletions lib/arrow_web/components/disruption_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -329,18 +329,18 @@ defmodule ArrowWeb.DisruptionComponents do
<div class="text-right">
<.link
:if={!@editing}
id={"edit-export-button-#{export.id}"}
id={"edit-hastus-export-button-#{export.id}"}
class="btn-sm p-0"
patch={~p"/disruptions/#{@disruption.id}/hastus_export/#{export.id}/edit"}
>
<.icon name="hero-pencil-solid" class="bg-primary" />
</.link>
<.button
:if={!@editing}
id={"delete-export-button-#{export.id}"}
id={"delete-hastus-export-button-#{export.id}"}
class="btn-sm p-0"
type="button"
phx-click="delete_export"
phx-click="delete_hastus_export"
phx-value-export={export.id}
data-confirm="Are you sure you want to delete this export?"
>
Expand Down Expand Up @@ -493,22 +493,22 @@ defmodule ArrowWeb.DisruptionComponents do
<div class="col-12 text-right">
<.link
:if={!@editing}
id={"edit-export-button-#{export.id}"}
id={"edit-trainsformer-export-button-#{export.id}"}
class="btn-sm p-0"
patch={~p"/disruptions/#{@disruption.id}/trainsformer_export/#{export.id}/edit"}
>
<.icon name="hero-pencil-solid" class="bg-primary" />
<.icon name="hero-pencil-solid" class="bg-primary hover:opacity-140" />
</.link>
<.button
:if={!@editing}
id={"delete-export-button-#{export.id}"}
id={"delete-trainsformer-export-button-#{export.id}"}
class="btn-sm p-0"
type="button"
phx-click="delete_export"
phx-click="delete_trainsformer_export"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion:
Good catch.

If it wouldn't be too much of a pain, it would also help to rename the delete_export event and its corresponding handle_event clause to delete_hastus_export.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can do!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phx-value-export={export.id}
data-confirm="Are you sure you want to delete this export?"
>
<.icon name="hero-trash-solid" class="bg-primary" />
<.icon name="hero-trash-solid" class="bg-primary hover:opacity-140" />
</.button>
</div>
</div>
Expand Down
92 changes: 21 additions & 71 deletions lib/arrow_web/components/edit_trainsformer_export_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule ArrowWeb.EditTrainsformerExportForm do
alias Arrow.Trainsformer
alias Arrow.Trainsformer.Export
alias Arrow.Trainsformer.ExportUpload
alias Arrow.Trainsformer.ServiceDate
alias Arrow.Trainsformer.ServiceDateDayOfWeek
alias Phoenix.LiveView.UploadEntry

Expand Down Expand Up @@ -152,6 +151,11 @@ defmodule ArrowWeb.EditTrainsformerExportForm do
</div>
<div class="col-lg-9 text-md">
<.inputs_for :let={f_date} field={f_service[:service_dates]}>
<input
type="hidden"
name={Phoenix.HTML.Form.input_name(f_service, :service_dates_sort) <> "[]"}
value={f_date.index}
/>
{f_service[:start_date].value}
<div class="row">
<div class="col">
Expand Down Expand Up @@ -214,15 +218,15 @@ defmodule ArrowWeb.EditTrainsformerExportForm do
</div>
</div>
<div class="col">
<.button
type="button"
phx-click="delete_service_date"
phx-value-service_index={f_service.index}
phx-value-date_index={f_date.index}
phx-target={@myself}
>
<label class="cursor-pointer hover:opacity-140">
<.icon name="hero-trash-solid" class="bg-primary" />
</.button>
<input
type="checkbox"
name={Phoenix.HTML.Form.input_name(f_service, :service_dates_drop) <> "[]"}
class="hidden"
value={f_date.index}
/>
</label>
</div>
</div>
<div class="row">
Expand All @@ -239,15 +243,15 @@ defmodule ArrowWeb.EditTrainsformerExportForm do

<div class="row mt-3">
<div class="col-9" />
<.button
type="button"
class="btn h-15 w-15 btn-primary btn-sm "
value={f_service.index}
phx-click="add_service_date"
phx-target={@myself}
>
<label class="btn h-15 w-15 btn-primary btn-sm">
Add Another Timeframe
</.button>
<input
type="checkbox"
name={Phoenix.HTML.Form.input_name(f_service, :service_dates_sort) <> "[]"}
class="hidden"
value="new"
/>
</label>
</div>
</.inputs_for>
</div>
Expand Down Expand Up @@ -426,60 +430,6 @@ defmodule ArrowWeb.EditTrainsformerExportForm do
{:noreply, socket}
end

def handle_event("add_service_date", %{"value" => index}, socket) do
{index, _} = Integer.parse(index)

socket =
update(socket, :form, fn %{source: changeset} ->
updated_services =
changeset
|> Ecto.Changeset.get_assoc(:services)
|> update_in([Access.at(index)], fn service ->
existing_dates = Ecto.Changeset.get_assoc(service, :service_dates)

Ecto.Changeset.put_change(
service,
:service_dates,
existing_dates ++ [%ServiceDate{service_date_days_of_week: []}]
)
end)

changeset
|> Ecto.Changeset.put_assoc(:services, updated_services)
|> to_form()
end)

{:noreply, socket}
end

def handle_event(
"delete_service_date",
%{"service_index" => service_index, "date_index" => date_index},
socket
) do
{service_index, _} = Integer.parse(service_index)
{date_index, _} = Integer.parse(date_index)

socket =
update(socket, :form, fn %{source: changeset} ->
updated_services =
changeset
|> Ecto.Changeset.get_assoc(:services)
|> update_in([Access.at(service_index)], fn service ->
dates =
service |> Ecto.Changeset.get_assoc(:service_dates) |> List.delete_at(date_index)

Ecto.Changeset.put_change(service, :service_dates, dates)
end)

changeset
|> Ecto.Changeset.put_assoc(:services, updated_services)
|> to_form()
end)

{:noreply, socket}
end

defp handle_progress(:trainsformer_export, %UploadEntry{done?: false}, socket) do
{:noreply, socket}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ defmodule ArrowWeb.DisruptionV2ViewLive do
end
end

def handle_event("delete_export", %{"export" => export_id}, socket) do
def handle_event("delete_hastus_export", %{"export" => export_id}, socket) do
{parsed_id, _} = Integer.parse(export_id)
export = Hastus.get_export!(parsed_id)

Expand Down
Loading