From fcc09e6bae56747d68f62720c19248234c6f88ba Mon Sep 17 00:00:00 2001 From: Nuno Miguel Date: Thu, 8 Aug 2024 02:58:18 +0100 Subject: [PATCH 01/23] refactor: improve calendar performance and style --- lib/atomic_web/components/calendar/month.ex | 68 +++++-- lib/atomic_web/live/calendar_live/show.ex | 95 +++++++++- .../live/calendar_live/show.html.heex | 176 +++++++++++++++++- 3 files changed, 317 insertions(+), 22 deletions(-) diff --git a/lib/atomic_web/components/calendar/month.ex b/lib/atomic_web/components/calendar/month.ex index 6717e6ba5..3cb452815 100644 --- a/lib/atomic_web/components/calendar/month.ex +++ b/lib/atomic_web/components/calendar/month.ex @@ -6,7 +6,7 @@ defmodule AtomicWeb.Components.CalendarMonth do import AtomicWeb.Components.Badge attr :id, :string, default: "calendar-month", required: false - attr :current_path, :string, required: true + attr :current_date, :string, required: true attr :activities, :list, required: true attr :timezone, :string, required: true attr :beginning_of_month, :string, required: true @@ -15,9 +15,9 @@ defmodule AtomicWeb.Components.CalendarMonth do def calendar_month(assigns) do ~H""" -
-
-
+
+
+
Mon
@@ -35,20 +35,21 @@ defmodule AtomicWeb.Components.CalendarMonth do
Sat
-
+
Sun
-
- <%= for i <- 0..@end_of_month.day - 1 do %> - <.day index={i} params={@params} current_path={@current_path} activities={@activities} date={Timex.shift(@beginning_of_month, days: i)} timezone={@timezone} /> +
+ <%= for date <- generate_days_list(@beginning_of_month, @end_of_month) do %> + <%!--

<%= @end_of_month %>

--%> + <.day index={date.day} params={@params} date={date} current_date={@current_date} activities={@activities} timezone={@timezone} /> <% end %>
-
    +
      <%= for activity <- get_date_activities(@activities, current_from_params(@timezone, @params)) do %> <.link patch={Routes.activity_show_path(AtomicWeb.Endpoint, :show, activity)}>
    1. @@ -74,16 +75,40 @@ defmodule AtomicWeb.Components.CalendarMonth do """ end + defp generate_days_list(beginning_of_month, end_of_month) do + days_from_last_month = Timex.weekday(beginning_of_month, :monday) + + past_month = + for i <- 1..(days_from_last_month - 1) do + Timex.shift(Timex.shift(beginning_of_month, days: -days_from_last_month), days: i) + end + + current_month = + for i <- 0..(end_of_month.day - 1) do + Timex.shift(beginning_of_month, days: i) + end + + next_month = + for i <- 1..(42 - Timex.days_in_month(beginning_of_month) - days_from_last_month + 1) do + Timex.shift(end_of_month, days: i) + end + + if days_from_last_month == 1 do + current_month ++ next_month + else + past_month ++ current_month ++ next_month + end + end + defp day(%{index: index, date: date, timezone: timezone} = assigns) do weekday = Timex.weekday(date, :monday) today? = Timex.compare(date, Timex.today(timezone)) class = class_list([ - {"relative py-2 px-3 lg:min-h-[110px] lg:flex hidden", true}, - {col_start(weekday), index == 0}, - {"bg-white", today? >= 0}, - {"bg-zinc-50 text-zinc-500", today? < 0} + {"relative py-2 px-3 lg:h-28 lg:flex flex-col hidden", true}, + {"bg-white", assigns.current_date.month == date.month}, + {"bg-zinc-50 text-zinc-500", assigns.current_date.month != date.month} ]) assigns = @@ -99,25 +124,30 @@ defmodule AtomicWeb.Components.CalendarMonth do
      -
        - <%= for activity <- get_date_activities(@activities, @date) do %> +
          + <%= for activity <- get_date_activities(@activities, @date) |> Enum.take(2) do %>
        1. <.link patch={Routes.activity_show_path(AtomicWeb.Endpoint, :show, activity)} class="group flex"> -

          +

          <%= activity.title %>

          - +
        2. <% end %> + <%= if Enum.count(get_date_activities(@activities, @date)) > 2 do %> +
        3. + + <%= Enum.count(get_date_activities(@activities, @date)) - 2 %> more +
        4. + <% end %>
      - <.link patch={build_path(@current_path, %{mode: "month", day: date_to_day(@date), month: date_to_month(@date), year: date_to_year(@date)})} class={"#{if @index == 0 do col_start(@weekday) end} min-h-[56px] flex w-full flex-col bg-white px-3 py-2 text-zinc-900 hover:bg-zinc-100 focus:z-10 lg:hidden"}> + <.link patch={build_path("", %{mode: "month", day: date_to_day(@date), month: date_to_month(@date), year: date_to_year(@date)})} class={"#{if @index == 0 do col_start(@weekday) end} min-h-[56px] flex w-full flex-col bg-white px-3 py-2 text-zinc-900 hover:bg-zinc-100 focus:z-10 lg:hidden"}>
@@ -100,7 +100,7 @@ defmodule AtomicWeb.Components.CalendarMonth do end end - defp day(%{index: index, date: date, timezone: timezone} = assigns) do + defp day(%{date: date, timezone: timezone} = assigns) do weekday = Timex.weekday(date, :monday) today? = Timex.compare(date, Timex.today(timezone)) @@ -147,7 +147,7 @@ defmodule AtomicWeb.Components.CalendarMonth do <% end %>
- <.link patch={build_path("", %{mode: "month", day: date_to_day(@date), month: date_to_month(@date), year: date_to_year(@date)})} class={"#{if @index == 0 do col_start(@weekday) end} min-h-[56px] flex w-full flex-col bg-white px-3 py-2 text-zinc-900 hover:bg-zinc-100 focus:z-10 lg:hidden"}> + <.link patch={build_path("", %{mode: "month", day: date_to_day(@date), month: date_to_month(@date), year: date_to_year(@date)})} class="min-h-[56px] flex w-full flex-col bg-white px-3 py-2 text-zinc-900 hover:bg-zinc-100 focus:z-10 lg:hidden">