diff --git a/internal/bot/calendar.go b/internal/bot/calendar.go index fd097d6..24f4320 100644 --- a/internal/bot/calendar.go +++ b/internal/bot/calendar.go @@ -340,45 +340,53 @@ func (h *Handler) setEventWorkType(ctx context.Context, chatID int64, msgID int, h.sendDayView(ctx, chatID, msgID, user, t.Format("2006-01-02"), loc, false) } -// editEventTime shows an hour picker for changing an event's time. -func (h *Handler) editEventTime(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, loc *time.Location) { - text := "Select hour:" +// buildHourPickerKeyboard creates the 24-hour grid for time selection. +func buildHourPickerKeyboard(cb func(int) string, backData string) models.InlineKeyboardMarkup { kbRows := [][]models.InlineKeyboardButton{} hourRow := []models.InlineKeyboardButton{} for hh := 0; hh < 24; hh++ { hourRow = append(hourRow, models.InlineKeyboardButton{ - Text: fmt.Sprintf("%02d", hh), - CallbackData: fmt.Sprintf("edittm_%d_%02d", eventID, hh), + Text: fmt.Sprintf("%02d", hh), CallbackData: cb(hh), }) if len(hourRow) == 6 || hh == 23 { kbRows = append(kbRows, hourRow) hourRow = nil } } - kbRows = append(kbRows, []models.InlineKeyboardButton{ - {Text: "Back", CallbackData: fmt.Sprintf("back_day_%d", eventID)}, - }) - kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows} - h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb}) + kbRows = append(kbRows, []models.InlineKeyboardButton{{Text: "Back", CallbackData: backData}}) + return models.InlineKeyboardMarkup{InlineKeyboard: kbRows} } -// editEventTimeMin shows a minute picker (15-min intervals) after hour selection. -func (h *Handler) editEventTimeMin(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, hh int, loc *time.Location) { - text := fmt.Sprintf("Select minute for hour %02d:", hh) +// buildMinutePickerKeyboard creates the 15-min interval row for minute selection. +func buildMinutePickerKeyboard(cb func(int) string, backData string) models.InlineKeyboardMarkup { kbRows := [][]models.InlineKeyboardButton{} minRow := []models.InlineKeyboardButton{} for _, mm := range []int{0, 15, 30, 45} { minRow = append(minRow, models.InlineKeyboardButton{ - Text: fmt.Sprintf("%02d", mm), - CallbackData: fmt.Sprintf("edittm_%d_%02d_%02d", eventID, hh, mm), + Text: fmt.Sprintf("%02d", mm), CallbackData: cb(mm), }) } kbRows = append(kbRows, minRow) - kbRows = append(kbRows, []models.InlineKeyboardButton{ - {Text: "Back", CallbackData: fmt.Sprintf("edit_time_%d", eventID)}, - }) - kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows} - h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb}) + kbRows = append(kbRows, []models.InlineKeyboardButton{{Text: "Back", CallbackData: backData}}) + return models.InlineKeyboardMarkup{InlineKeyboard: kbRows} +} + +// editEventTime shows an hour picker for changing an event's time. +func (h *Handler) editEventTime(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, loc *time.Location) { + kb := buildHourPickerKeyboard( + func(hh int) string { return fmt.Sprintf("edittm_%d_%02d", eventID, hh) }, + fmt.Sprintf("back_day_%d", eventID), + ) + h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: "Select hour:", ReplyMarkup: &kb}) +} + +// editEventTimeMin shows a minute picker (15-min intervals) after hour selection. +func (h *Handler) editEventTimeMin(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, hh int, loc *time.Location) { + kb := buildMinutePickerKeyboard( + func(mm int) string { return fmt.Sprintf("edittm_%d_%02d_%02d", eventID, hh, mm) }, + fmt.Sprintf("edit_time_%d", eventID), + ) + h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: fmt.Sprintf("Select minute for hour %02d:", hh), ReplyMarkup: &kb}) } // editEventTimeSet applies a new time to an event, checking for overlaps. @@ -516,46 +524,23 @@ func (h *Handler) getEventByID(userID, eventID int64) (*db.Event, error) { // addEventTime shows an hour picker for adding a new event to a day. func (h *Handler) addEventTime(ctx context.Context, chatID int64, msgID int, user *db.User, date, eventType string, loc *time.Location) { - text := fmt.Sprintf("Select hour for %s event:", eventType) - kbRows := [][]models.InlineKeyboardButton{} - hourRow := []models.InlineKeyboardButton{} - for hh := 0; hh < 24; hh++ { - hourRow = append(hourRow, models.InlineKeyboardButton{ - Text: fmt.Sprintf("%02d", hh), - CallbackData: fmt.Sprintf("addtm_%s_%s_%02d", eventType, date, hh), - }) - if len(hourRow) == 6 || hh == 23 { - kbRows = append(kbRows, hourRow) - hourRow = nil - } - } - kbRows = append(kbRows, []models.InlineKeyboardButton{ - {Text: "Back", CallbackData: "cal_day_" + date}, - }) - kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows} + kb := buildHourPickerKeyboard( + func(hh int) string { return fmt.Sprintf("addtm_%s_%s_%02d", eventType, date, hh) }, + "cal_day_"+date, + ) h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ - ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb, + ChatID: chatID, MessageID: msgID, Text: fmt.Sprintf("Select hour for %s event:", eventType), ReplyMarkup: &kb, }) } // addEventTimeMin shows a minute picker (15-min intervals) after hour selection for adding an event. func (h *Handler) addEventTimeMin(ctx context.Context, chatID int64, msgID int, user *db.User, date, eventType string, hh int, loc *time.Location) { - text := fmt.Sprintf("Select minute for hour %02d:", hh) - kbRows := [][]models.InlineKeyboardButton{} - minRow := []models.InlineKeyboardButton{} - for _, mm := range []int{0, 15, 30, 45} { - minRow = append(minRow, models.InlineKeyboardButton{ - Text: fmt.Sprintf("%02d", mm), - CallbackData: fmt.Sprintf("addtm_%s_%s_%02d_%02d", eventType, date, hh, mm), - }) - } - kbRows = append(kbRows, minRow) - kbRows = append(kbRows, []models.InlineKeyboardButton{ - {Text: "Back", CallbackData: "add" + eventType + "_" + date}, - }) - kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows} + kb := buildMinutePickerKeyboard( + func(mm int) string { return fmt.Sprintf("addtm_%s_%s_%02d_%02d", eventType, date, hh, mm) }, + "add"+eventType+"_"+date, + ) h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ - ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb, + ChatID: chatID, MessageID: msgID, Text: fmt.Sprintf("Select minute for hour %02d:", hh), ReplyMarkup: &kb, }) }