From 55cc78760e4d04b57320a9d32eb7df83e1503697 Mon Sep 17 00:00:00 2001 From: Pascal Schmid Date: Mon, 21 Oct 2024 22:44:52 +0200 Subject: [PATCH] Show interaction data in calendar --- assets/css/app.css | 1 - lib/mihainator/extractor.ex | 27 ++++++++++++- .../live/components/result_component.ex | 38 +++++++++++++++---- .../components/result_component.html.heex | 11 ++---- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/assets/css/app.css b/assets/css/app.css index 93d0346..428ccf5 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -62,7 +62,6 @@ cursor: pointer; outline: 0; border: 0; - background: transparent; font-size: 16px; justify-self: center; align-self: center; diff --git a/lib/mihainator/extractor.ex b/lib/mihainator/extractor.ex index 9bf2923..253c0e2 100644 --- a/lib/mihainator/extractor.ex +++ b/lib/mihainator/extractor.ex @@ -3,11 +3,27 @@ defmodule Mihainator.Extractor do This module will take Threema's CSV history file and parse dates / first interaction of day """ + # TODO: unit test + # TODO: refactor this method + @spec extract( + binary() + | maybe_improper_list( + binary() | maybe_improper_list(any(), binary() | []) | char(), + binary() | [] + ) + ) :: %{first_date: Date.t(), interaction_data: map(), last_date: Date.t()} def extract(file) do raw_data = get_raw_data(file) - {last_date, _} = Enum.at(raw_data, -1) - first_date = Date.shift(last_date, year: -1) + last_date = + Enum.at(raw_data, -1) + |> elem(0) + |> Date.end_of_month() + + first_date = + Date.shift(last_date, year: -1) + |> Date.beginning_of_month() + date_range = Date.range(first_date, last_date) interaction_data = @@ -15,6 +31,13 @@ defmodule Mihainator.Extractor do |> Enum.filter(fn {date, _} -> Date.after?(date, first_date) end) |> Map.new() |> get_normalized_interaction_data(date_range) + |> Enum.group_by(fn {date, _} -> + month = + Integer.to_string(date.month) + |> String.pad_leading(2, "0") + + "#{date.year}-#{month}" + end) %{ first_date: first_date, diff --git a/lib/mihainator_web/live/components/result_component.ex b/lib/mihainator_web/live/components/result_component.ex index ff4751a..7227953 100644 --- a/lib/mihainator_web/live/components/result_component.ex +++ b/lib/mihainator_web/live/components/result_component.ex @@ -7,10 +7,6 @@ defmodule MihainatorWeb.ResultComponent do def update(assigns, socket) do socket = assign(socket, assigns) - months = get_months(assigns.calendar_dates) - - socket = assign(socket, months: months) - {:ok, socket} end @@ -20,16 +16,42 @@ defmodule MihainatorWeb.ResultComponent do end def time_button(assigns) do + {day, outgoing} = assigns.day + + state = + case outgoing do + true -> "bg-green-300" + false -> "bg-red-300" + _ -> "" + end + + assigns = assign(assigns, state: state, day: day.day) + ~H""" - """ end - defp get_months(%{first_date: first_date}) do - start = Date.beginning_of_month(first_date) + def month(assigns) do + days_of_month = assigns.days_of_month - for month_difference <- 0..11, do: Date.shift(start, month: month_difference) + day = + Enum.at(days_of_month, 0) + |> elem(0) + + formatted_month = Calendar.strftime(day, "%B") + + assigns = assign(assigns, formatted_month: formatted_month, year: day.year) + + ~H""" +
+
+ <%= @formatted_month %> + <%= @year %> +
+
+ """ end end diff --git a/lib/mihainator_web/live/components/result_component.html.heex b/lib/mihainator_web/live/components/result_component.html.heex index f0ebe05..5f285a8 100644 --- a/lib/mihainator_web/live/components/result_component.html.heex +++ b/lib/mihainator_web/live/components/result_component.html.heex @@ -2,13 +2,10 @@

This is the result of your history:

- <%= for month <- @months do %> + <%= for {_, days_of_month} <- @calendar_dates.interaction_data do %>
-
-
- <%= Calendar.strftime(month, "%B") %> <%= month.year %> -
-
+ <.month days_of_month={days_of_month}> +
Mon Tue @@ -19,7 +16,7 @@ Sun
- <%= for day <- 1..31 do %> + <%= for day <- days_of_month do %> <.time_button day={day}> <% end %>