Skip to content

Commit

Permalink
feat(CMS.Page.Event): better teaser started_status logic (#1866)
Browse files Browse the repository at this point in the history
* feat(Util): handle additional date types

* feat(CMS.Page.Event): better teaser started_status logic
  • Loading branch information
thecristen authored Jan 29, 2024
1 parent 4dad87a commit 6c8e906
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
37 changes: 17 additions & 20 deletions lib/cms/page/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule CMS.Page.Event do
]

import Util,
only: [time_is_greater_or_equal?: 2, date_to_naive_date: 1, to_local_time: 1, now: 0]
only: [time_is_greater_or_equal?: 2, date_to_naive_date: 1, now: 0]

alias CMS.Field.File
alias CMS.Field.Link
Expand Down Expand Up @@ -125,36 +125,33 @@ defmodule CMS.Page.Event do
# Events will always have a start time, but unsure if teasers will. Handle :nil
def started_status(nil, _), do: nil

def started_status(%NaiveDateTime{} = start, stop) do
started_status(
to_local_time(start),
if(is_nil(stop), do: nil, else: to_local_time(stop))
)
end

def started_status(%Date{} = start, stop) do
started_status(
date_to_naive_date(start),
if(is_nil(stop), do: nil, else: date_to_naive_date(stop))
)
end
def started_status(%NaiveDateTime{} = start, %NaiveDateTime{} = stop) do
now = now() |> DateTime.to_naive()

def started_status(start, nil) do
cond do
Date.compare(now(), start) === :gt -> :ended
time_is_greater_or_equal?(now(), start) -> :started
time_is_greater_or_equal?(now, stop) -> :ended
time_is_greater_or_equal?(now, start) -> :started
true -> :not_started
end
end

def started_status(start, stop) do
def started_status(%NaiveDateTime{} = start, nil) do
now_dt = now()

cond do
time_is_greater_or_equal?(now(), stop) -> :ended
time_is_greater_or_equal?(now(), start) -> :started
Date.compare(now_dt, start) === :gt -> :ended
time_is_greater_or_equal?(DateTime.to_naive(now_dt), start) -> :started
true -> :not_started
end
end

def started_status(start, stop) do
started_status(
date_to_naive_date(start),
if(is_nil(stop), do: nil, else: date_to_naive_date(stop))
)
end

@spec parse_optional_html(String.t() | nil) :: HTML.safe() | nil
defp parse_optional_html(nil), do: nil
defp parse_optional_html(value), do: handle_html(value)
Expand Down
22 changes: 18 additions & 4 deletions lib/util/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ defmodule Util do
now() |> Timex.to_date()
end

@spec time_is_greater_or_equal?(DateTime.t(), DateTime.t()) :: boolean
@spec time_is_greater_or_equal?(
DateTime.t() | NaiveDateTime.t(),
DateTime.t() | NaiveDateTime.t()
) :: boolean
def time_is_greater_or_equal?(time, ref_time) do
case DateTime.compare(time, ref_time) do
compare_fn =
case {time, ref_time} do
{%DateTime{}, %DateTime{}} ->
&DateTime.compare/2

{%NaiveDateTime{}, %NaiveDateTime{}} ->
&NaiveDateTime.compare/2
end

case compare_fn.(time, ref_time) do
:gt -> true
:eq -> true
:lt -> false
Expand Down Expand Up @@ -79,8 +91,10 @@ defmodule Util do
{:error, :invalid_date}
end

@spec date_to_naive_date(Date.t()) :: NaiveDateTime.t()
def date_to_naive_date(date), do: NaiveDateTime.new(date, ~T[00:00:00.00]) |> elem(1)
@spec date_to_naive_date(NaiveDateTime.t() | DateTime.t() | Date.t()) :: NaiveDateTime.t()
def date_to_naive_date(%Date{} = date), do: NaiveDateTime.new(date, ~T[00:00:00.00]) |> elem(1)
def date_to_naive_date(%DateTime{} = date), do: DateTime.to_naive(date)
def date_to_naive_date(%NaiveDateTime{} = date), do: date

def convert_to_iso_format(date) do
date
Expand Down

0 comments on commit 6c8e906

Please sign in to comment.