Skip to content

Commit 5a870fd

Browse files
committed
Fix the calendar
Shows the current date correctly now
1 parent 85fbe58 commit 5a870fd

File tree

2 files changed

+10
-51
lines changed

2 files changed

+10
-51
lines changed

lib/components_guide_web/components/calendar_component.ex

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,19 @@ defmodule ComponentsGuideWeb.CalendarComponent do
22
use ComponentsGuideWeb, :component
33

44
def calendar_grid(assigns) do
5-
%{year: year, month: month} = assigns
6-
7-
day =
8-
case assigns[:current_date] do
9-
nil ->
10-
nil
11-
12-
%{day: day} ->
13-
day
14-
end
15-
16-
# %{year: year, month: month, day: day} = today = assigns[:date]
17-
date = Date.new!(year, month, 1)
18-
start_date = Date.beginning_of_month(date)
19-
end_date = Date.end_of_month(date)
20-
date_range = Date.range(start_date, end_date)
21-
22-
day_of_week = Date.day_of_week(start_date)
23-
day_offset = 1 - day_of_week
24-
max_week = div(end_date.day + day_of_week + 5, 7)
5+
%{current_date: current_date} = assigns
6+
current_row_start_date = Date.beginning_of_week(current_date)
7+
current_day_of_week = Date.day_of_week(current_date)
258

269
assigns =
2710
assigns
2811
|> Map.merge(%{
29-
date_range: date_range,
30-
year: year,
31-
month: month,
32-
day: day,
33-
current: Date.new!(year, month, day),
34-
day_of_week: day_of_week,
35-
day_offset: day_offset
12+
current_row_start_date: current_row_start_date,
13+
current_day_of_week: current_day_of_week
3614
})
3715
|> Map.put_new(:extra, fn _ -> "" end)
3816

3917
~H"""
40-
<%= if false do %>
41-
<h2 class="text-center"><%= Calendar.strftime(@date_range.first, "%B %Y") %></h2>
42-
<% end %>
4318
<table class="text-center">
4419
<thead class="border-0">
4520
<tr>
@@ -53,27 +28,11 @@ defmodule ComponentsGuideWeb.CalendarComponent do
5328
</tr>
5429
</thead>
5530
<tbody>
56-
<%= for week_n <- 1..max_week, false do %>
57-
<tr class="min-h-16">
58-
<%= for weekday <- 0..6 do %>
59-
<% day = weekday + 1 + day_offset + ((week_n - 1) * 7) %>
60-
<%= if day in @date_range.first.day..@date_range.last.day do %>
61-
<% current? = day == @day %>
62-
<td aria-current={if current?, do: "date", else: "false"} class={td_class(%{current?: current?, weekday: weekday})}>
63-
<div class="text-sm"><%= day %></div>
64-
<%= @extra.(Date.new!(year, month, day)) %>
65-
</td>
66-
<% else %>
67-
<td role="presentation" class=""></td>
68-
<% end %>
69-
<% end %>
70-
</tr>
71-
<% end %>
7231
<%= for week_offset <- -4..4 do %>
7332
<tr class="min-h-16 group">
74-
<%= for weekday <- 0..6 do %>
75-
<% date = Date.add(@current, (week_offset - 1) * 7 + weekday - day_offset) %>
76-
<% current_day? = week_offset == 0 && day_of_week == weekday %>
33+
<%= for weekday <- 1..7 do %>
34+
<% date = Date.add(@current_row_start_date, week_offset * 7 + (weekday - 1)) %>
35+
<% current_day? = week_offset == 0 && @current_day_of_week == weekday %>
7736
<td aria-current={if current_day?, do: "date", else: "false"} class={td_class(%{current_day?: current_day?, weekday: weekday, week_offset: week_offset})}>
7837
<div class={td_text_class(week_offset)}><%= Calendar.strftime(date, "%d %b") %></div>
7938
<%= @extra.(date) %>
@@ -87,7 +46,7 @@ defmodule ComponentsGuideWeb.CalendarComponent do
8746
end
8847

8948
defp td_class(%{current_day?: true}), do: "bg-green-900/90 text-green-100"
90-
defp td_class(%{weekday: weekday}) when weekday in [5, 6], do: "bg-black/40"
49+
defp td_class(%{weekday: weekday}) when weekday in [6, 7], do: "bg-black/40"
9150
defp td_class(%{week_offset: 0}), do: "bg-green-900/25"
9251
defp td_class(_), do: "bg-black"
9352

lib/components_guide_web/templates/calendar/index.html.heex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="prose md:prose-xl prose-invert max-w-6xl mx-auto pt-8 px-3">
22
<h1 class="text-center">Developer Calendar</h1>
3-
<CalendarComponent.calendar_grid year={@today.year} month={@today.month} current_date={@today} extra={@calendar_extra} />
3+
<CalendarComponent.calendar_grid current_date={@today} extra={@calendar_extra} />
44
</div>
55

66
<div class="prose md:prose-xl prose-invert max-w-2xl mx-auto pt-8 pb-16 px-3">

0 commit comments

Comments
 (0)