@@ -13,27 +13,31 @@ import (
1313 "github.com/ignavan39/mood-diary/internal/domain/entity"
1414 "github.com/ignavan39/mood-diary/internal/infrastructure/i18n"
1515 "github.com/ignavan39/mood-diary/internal/presentation/styles"
16+ "github.com/ignavan39/mood-diary/internal/presentation/tui/components"
1617 "github.com/ignavan39/mood-diary/internal/presentation/tui/formatters"
1718 "github.com/ignavan39/mood-diary/internal/presentation/tui/state"
1819)
1920
2021type CalendarScreen struct {
2122 state.BaseState
2223
24+ ctx context.Context
2325 service * usecase.MoodService
2426 translator i18n.Translator
2527
2628 currentMonth time.Time
2729 selectedDate time.Time
2830 moodData map [time.Time ]* entity.MoodEntry
2931
30- cursorRow int
31- cursorCol int
32+ cursorRow int
33+ cursorCol int
34+ confirmDlg * components.ConfirmationDialog
3235}
3336
34- func NewCalendarScreen (service * usecase.MoodService , translator i18n.Translator ) * CalendarScreen {
37+ func NewCalendarScreen (ctx context. Context , service * usecase.MoodService , translator i18n.Translator ) * CalendarScreen {
3538 now := time .Now ()
3639 return & CalendarScreen {
40+ ctx : ctx ,
3741 service : service ,
3842 translator : translator ,
3943 currentMonth : time .Date (now .Year (), now .Month (), 1 , 0 , 0 , 0 , 0 , time .UTC ),
@@ -55,6 +59,14 @@ func (s *CalendarScreen) Init() tea.Cmd {
5559}
5660
5761func (s * CalendarScreen ) Update (msg tea.Msg ) (state.Screen , tea.Cmd ) {
62+ if s .confirmDlg != nil && ! s .confirmDlg .IsDone () {
63+ cmd := s .confirmDlg .Update (msg )
64+ if s .confirmDlg .IsDone () {
65+ s .confirmDlg = nil
66+ }
67+ return s , cmd
68+ }
69+
5870 switch msg := msg .(type ) {
5971 case tea.WindowSizeMsg :
6072 s .SetSize (msg .Width , msg .Height )
@@ -106,11 +118,13 @@ func (s *CalendarScreen) handleKeyMsg(msg tea.KeyMsg) (state.Screen, tea.Cmd) {
106118
107119 entry := s .moodData [s .selectedDate ]
108120 if entry != nil {
109- return s , s .deleteMood (entry )
121+ msg := fmt .Sprintf (s .t (i18n .EditDeleteWarningKey ), formatters .FormatDate (entry .Date ))
122+ s .confirmDlg = components .NewConfirmation (msg , s .translator , s .deleteMood (entry ), nil )
123+ return s , nil
110124 }
111125
112126 case "esc" , "q" :
113- return s , state .NavigateToMenu ()
127+ return s , state .NavigateBack ()
114128 }
115129
116130 return s , nil
@@ -163,16 +177,17 @@ func (s *CalendarScreen) updateCursorPosition() {
163177
164178func (s * CalendarScreen ) loadMonthData () tea.Cmd {
165179 return func () tea.Msg {
166- ctx := context .Background ()
167- entries , err := s .service .GetAllMoods (ctx )
180+ start := time .Date (s .currentMonth .Year (), s .currentMonth .Month (), 1 , 0 , 0 , 0 , 0 , time .UTC )
181+ end := start .AddDate (0 , 1 , - 1 )
182+ end = time .Date (end .Year (), end .Month (), end .Day (), 23 , 59 , 59 , 0 , time .UTC )
183+
184+ entries , err := s .service .GetMoodsByDateRange (s .ctx , start , end )
168185
169186 data := make (map [time.Time ]* entity.MoodEntry )
170187 if err == nil {
171188 for _ , e := range entries {
172189 day := time .Date (e .Date .Year (), e .Date .Month (), e .Date .Day (), 0 , 0 , 0 , 0 , time .UTC )
173- if day .Year () == s .currentMonth .Year () && day .Month () == s .currentMonth .Month () {
174- data [day ] = e
175- }
190+ data [day ] = e
176191 }
177192 } else {
178193 return state.ErrorMsg {Error : err }
@@ -184,8 +199,7 @@ func (s *CalendarScreen) loadMonthData() tea.Cmd {
184199
185200func (s * CalendarScreen ) deleteMood (entry * entity.MoodEntry ) tea.Cmd {
186201 return func () tea.Msg {
187- ctx := context .Background ()
188- err := s .service .DeleteMood (ctx , entry .Date )
202+ err := s .service .DeleteMood (s .ctx , entry .Date )
189203 if err != nil {
190204 return state.ErrorMsg {Error : err }
191205 }
@@ -201,6 +215,11 @@ func (s *CalendarScreen) View() string {
201215 b .WriteString (header )
202216 b .WriteString ("\n \n " )
203217
218+ if s .confirmDlg != nil {
219+ b .WriteString (s .confirmDlg .View ())
220+ return lipgloss .NewStyle ().Padding (2 , 4 ).Render (b .String ())
221+ }
222+
204223 if s .Error != nil {
205224 b .WriteString (styles .ErrorStyle .Render (s .t (i18n .CommonErrorPrefixKey ) + s .Error .Error ()))
206225 b .WriteString ("\n \n " )
0 commit comments