fix tables unable to sort on nil dates
This commit is contained in:
		
							
								
								
									
										12
									
								
								lib/cannery/comparable_date.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								lib/cannery/comparable_date.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| defmodule Cannery.ComparableDate do | ||||
|   @moduledoc """ | ||||
|   A custom `Date` module that provides a `compare/2` function that is comparable | ||||
|   with nil values | ||||
|   """ | ||||
|  | ||||
|   @spec compare(Date.t() | any(), Date.t() | any()) :: :lt | :gt | :eq | ||||
|   def compare(%Date{} = date_1, %Date{} = date_2), do: Date.compare(date_1, date_2) | ||||
|   def compare(%Date{}, _date_2), do: :lt | ||||
|   def compare(_date_1, %Date{}), do: :gt | ||||
|   def compare(_date_1, _date_2), do: :eq | ||||
| end | ||||
							
								
								
									
										15
									
								
								lib/cannery/comparable_datetime.ex
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								lib/cannery/comparable_datetime.ex
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| defmodule Cannery.ComparableDateTime do | ||||
|   @moduledoc """ | ||||
|   A custom `DateTime` module that provides a `compare/2` function that is | ||||
|   comparable with nil values | ||||
|   """ | ||||
|  | ||||
|   @spec compare(DateTime.t() | any(), DateTime.t() | any()) :: :lt | :gt | :eq | ||||
|   def compare(%DateTime{} = datetime_1, %DateTime{} = datetime_2) do | ||||
|     DateTime.compare(datetime_1, datetime_2) | ||||
|   end | ||||
|  | ||||
|   def compare(%DateTime{}, _datetime_2), do: :lt | ||||
|   def compare(_datetime_1, %DateTime{}), do: :gt | ||||
|   def compare(_datetime_1, _datetime_2), do: :eq | ||||
| end | ||||
| @@ -3,7 +3,8 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|   A component that displays a list of ammo groups | ||||
|   """ | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, ActivityLog, Ammo, Ammo.AmmoGroup, Containers} | ||||
|   alias Cannery.{Accounts.User, Ammo.AmmoGroup, ComparableDate} | ||||
|   alias Cannery.{ActivityLog, Ammo, Containers} | ||||
|   alias Ecto.UUID | ||||
|   alias Phoenix.LiveView.{Rendered, Socket} | ||||
|  | ||||
| @@ -54,8 +55,8 @@ defmodule CanneryWeb.Components.AmmoGroupTableComponent do | ||||
|       end | ||||
|  | ||||
|     columns = [ | ||||
|       %{label: gettext("Purchased on"), key: :purchased_on, type: Date}, | ||||
|       %{label: gettext("Last used on"), key: :used_up_on, type: Date} | columns | ||||
|       %{label: gettext("Purchased on"), key: :purchased_on, type: ComparableDate}, | ||||
|       %{label: gettext("Last used on"), key: :used_up_on, type: ComparableDate} | columns | ||||
|     ] | ||||
|  | ||||
|     columns = | ||||
|   | ||||
| @@ -3,7 +3,7 @@ defmodule CanneryWeb.Components.ShotGroupTableComponent do | ||||
|   A component that displays a list of shot groups | ||||
|   """ | ||||
|   use CanneryWeb, :live_component | ||||
|   alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo} | ||||
|   alias Cannery.{Accounts.User, ActivityLog.ShotGroup, Ammo, ComparableDate} | ||||
|   alias Ecto.UUID | ||||
|   alias Phoenix.LiveView.{Rendered, Socket} | ||||
|  | ||||
| @@ -41,7 +41,7 @@ defmodule CanneryWeb.Components.ShotGroupTableComponent do | ||||
|       %{label: gettext("Ammo"), key: :name}, | ||||
|       %{label: gettext("Rounds shot"), key: :count}, | ||||
|       %{label: gettext("Notes"), key: :notes}, | ||||
|       %{label: gettext("Date"), key: :date, type: Date}, | ||||
|       %{label: gettext("Date"), key: :date, type: ComparableDate}, | ||||
|       %{label: nil, key: :actions, sortable: false} | ||||
|     ] | ||||
|  | ||||
|   | ||||
| @@ -6,7 +6,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|   use CanneryWeb, :live_view | ||||
|   alias Cannery.{ActivityLog, ActivityLog.ShotGroup} | ||||
|   alias Cannery.{Ammo, Ammo.AmmoGroup} | ||||
|   alias Cannery.Containers | ||||
|   alias Cannery.{ComparableDate, Containers} | ||||
|   alias CanneryWeb.Endpoint | ||||
|   alias Phoenix.LiveView.Socket | ||||
|  | ||||
| @@ -90,7 +90,7 @@ defmodule CanneryWeb.AmmoGroupLive.Show do | ||||
|     columns = [ | ||||
|       %{label: gettext("Rounds shot"), key: :count}, | ||||
|       %{label: gettext("Notes"), key: :notes}, | ||||
|       %{label: gettext("Date"), key: :date, type: Date}, | ||||
|       %{label: gettext("Date"), key: :date, type: ComparableDate}, | ||||
|       %{label: nil, key: :actions, sortable: false} | ||||
|     ] | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user