display day log of the day

This commit is contained in:
Christophe Robillard 2026-03-22 16:32:01 +01:00
parent 9918584a91
commit 3b46a4de5a
5 changed files with 61 additions and 7 deletions

View file

@ -1,7 +1,7 @@
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ "image", "modeDay", "dayLogLink" ];
static targets = [ "image", "modeDay", "dayLogLink", "dayLogInfo" ];
updateDayInfo(event) {
const image = this.imageTarget;
@ -12,6 +12,7 @@ export default class extends Controller {
modeDay.textContent = modeDayContent;
event.target.className = "selected-day " + event.target.dataset.mode;
this.dayLogInfoTarget.textContent = event.target.dataset.info || '';
this.dayLogLinkTarget.href = `/day_logs/edit?day=${day}`
if (this.lastTarget) {

View file

@ -39,4 +39,8 @@ class User < ApplicationRecord
def current_mode
self.moods.last&.mode || "croisiere"
end
def current_day_log
self.day_logs.find_by(day: Date.today)
end
end

View file

@ -1,16 +1,17 @@
class MoodCalendarService
def self.generate_calendar(user, start_date: nil, end_date: Date.current)
def self.generate_calendar(user, start_date: nil, end_date: nil)
data = user.moods.includes(:mode)
.order(:recorded_at)
.map { |mood| { mode: mood.mode, recorded_at: mood.recorded_at } }
day_logs_by_date = user.day_logs.index_by(&:day)
if data.empty?
start_date = Date.current
else
start_date ||= data.first[:recorded_at].to_date
end
start_date = start_date.to_date
end_date = end_date ? end_date.to_date : [ Date.current, start_date + 5.months ].max
=begin
@ -29,16 +30,17 @@ class MoodCalendarService
&.[](:mode)
complete_data = (start_date..end_date).map do |date|
day_log = day_logs_by_date[date]
if data_by_date[date]
last_mode = data_by_date[date][:mode]
data_by_date[date]
data_by_date[date].merge(day_log: day_log)
else
{ mode: nil, recorded_at: date.to_datetime, guess: last_mode }
{ mode: nil, recorded_at: date.to_datetime, guess: last_mode, day_log: day_log }
end
end
complete_data.group_by { |d| d[:recorded_at].to_date.beginning_of_month }
.map do |month_start, month_data|
.map do |month_start, _|
first_monday = month_start
first_monday = first_monday.next_occurring(:monday) unless first_monday.monday?
@ -51,7 +53,7 @@ class MoodCalendarService
data_hash = complete_data.index_by { |d| d[:recorded_at].to_date }
all_days = (first_monday..month_end).map do |date|
data_hash[date] || { mode: nil, recorded_at: date.to_datetime, guess: nil }
data_hash[date] || { mode: nil, recorded_at: date.to_datetime, guess: nil, day_log: nil }
end
weeks = all_days.group_by { |d| d[:recorded_at].to_date.beginning_of_week(:monday) }

View file

@ -27,6 +27,7 @@
<span class="icon ml-2"><i class="has-text-grey fa-solid fa-pencil fa-xs"></i></span>
<% end %>
</div>
<p data-mood-target="dayLogInfo"><%= @user.current_day_log&.info %></p>
</div>
<div class="logs">
@ -43,6 +44,7 @@
<div data-image="<%= mode.image_url %>"
data-mode="<%= mode.label %>"
data-day="<%= l mood[:recorded_at].to_date %>"
data-info="<%= mood[:day_log]&.info %>"
data-action="click->mood#updateDayInfo"
title="<%= l(mood[:recorded_at].to_date) %> : <%= mode.label %>"
class="<%= css_class_for_day(status) %>"

View file

@ -310,5 +310,50 @@ RSpec.describe MoodCalendarService do
expect(mood_16[:guess]).to eq(mode)
end
end
context 'avec day_log' do
it 'inclut le day_log dans les données du jour correspondant' do
create(:mood, user: user, mode: mode, recorded_at: Time.zone.parse("2025-02-15 10:00:00"))
day_log = create(:day_log, user: user, day: Date.parse("2025-02-15"), info: "Une belle journée")
result = MoodCalendarService.generate_calendar(user, end_date: Date.parse("2025-02-28"))
all_days = result.find { |m| m[:month] == Date.parse("2025-02-01") }[:weeks].flatten
day_15 = all_days.find { |m| m[:recorded_at].to_date == Date.parse("2025-02-15") }
expect(day_15[:day_log]).to eq(day_log)
end
it 'retourne nil pour day_log quand il n\'en existe pas pour ce jour' do
create(:mood, user: user, mode: mode, recorded_at: Time.zone.parse("2025-02-15 10:00:00"))
result = MoodCalendarService.generate_calendar(user, end_date: Date.parse("2025-02-28"))
all_days = result.find { |m| m[:month] == Date.parse("2025-02-01") }[:weeks].flatten
day_15 = all_days.find { |m| m[:recorded_at].to_date == Date.parse("2025-02-15") }
expect(day_15[:day_log]).to be_nil
end
it 'inclut le day_log pour un jour sans mood' do
create(:mood, user: user, mode: mode, recorded_at: Time.zone.parse("2025-02-15 10:00:00"))
day_log = create(:day_log, user: user, day: Date.parse("2025-02-16"), info: "Jour sans mood")
result = MoodCalendarService.generate_calendar(user, end_date: Date.parse("2025-02-28"))
all_days = result.find { |m| m[:month] == Date.parse("2025-02-01") }[:weeks].flatten
day_16 = all_days.find { |m| m[:recorded_at].to_date == Date.parse("2025-02-16") }
expect(day_16[:mode]).to be_nil
expect(day_16[:day_log]).to eq(day_log)
end
it 'inclut le day_log pour un jour avec mood' do
create(:mood, user: user, mode: mode, recorded_at: Time.zone.parse("2025-02-15 10:00:00"))
day_log = create(:day_log, user: user, day: Date.parse("2025-02-15"), info: "Jour avec mood")
result = MoodCalendarService.generate_calendar(user, end_date: Date.parse("2025-02-28"))
all_days = result.find { |m| m[:month] == Date.parse("2025-02-01") }[:weeks].flatten
day_15 = all_days.find { |m| m[:recorded_at].to_date == Date.parse("2025-02-15") }
expect(day_15[:mode]).to eq(mode)
expect(day_15[:day_log]).to eq(day_log)
end
end
end
end