feat: display due date better

This commit is contained in:
Moritz Böhme 2025-04-07 15:28:11 +02:00
parent af1e2eb06f
commit a59ad17743

View file

@ -20,7 +20,7 @@ defmodule PutzplanWeb.TaskLive.Index do
>
<:col :let={{_id, task}} label="Description">{task.description}</:col>
<:col :let={{_id, task}} label="Due">{task.due}</:col>
<:col :let={{_id, task}} label="Due">{format_date(task.due)}</:col>
<:action :let={{_id, task}}>
<div class="sr-only">
@ -115,4 +115,37 @@ defmodule PutzplanWeb.TaskLive.Index do
{:noreply, stream_insert(socket, :tasks, Ash.get!(Putzplan.Tasks.Task, id, load: [:due]))}
end
defp format_date(date) do
string =
case Date.diff(date, Date.utc_today()) do
0 ->
"today"
1 ->
"tomorrow"
days when days > 0 and days < 7 ->
"next " <> get_weekday(Date.day_of_week(date))
_ ->
Date.to_string(date)
end
String.capitalize(string)
end
defp get_weekday(index) do
days = %{
1 => "monday",
2 => "tuesday",
3 => "wednesday",
4 => "thursday",
5 => "friday",
6 => "saturday",
7 => "sunday"
}
Map.get(days, index)
end
end