diff --git a/app/javascript/controllers/mood_controller.js b/app/javascript/controllers/mood_controller.js index 0ec087c..fca07af 100644 --- a/app/javascript/controllers/mood_controller.js +++ b/app/javascript/controllers/mood_controller.js @@ -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) { diff --git a/app/models/user.rb b/app/models/user.rb index 6fc0754..048226a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/services/mood_calendar_service.rb b/app/services/mood_calendar_service.rb index 8625067..fae249e 100644 --- a/app/services/mood_calendar_service.rb +++ b/app/services/mood_calendar_service.rb @@ -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) } diff --git a/app/views/moods/index.html.erb b/app/views/moods/index.html.erb index 0fc1612..26b8d84 100644 --- a/app/views/moods/index.html.erb +++ b/app/views/moods/index.html.erb @@ -27,6 +27,7 @@ <% end %> +
<%= @user.current_day_log&.info %>