DRY hour/minute picker: extract shared buildHourPickerKeyboard/buildMinutePickerKeyboard

This commit is contained in:
2026-06-24 15:04:22 +03:30
parent 75da4648aa
commit 353056349f

View File

@@ -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) h.sendDayView(ctx, chatID, msgID, user, t.Format("2006-01-02"), loc, false)
} }
// editEventTime shows an hour picker for changing an event's time. // buildHourPickerKeyboard creates the 24-hour grid for time selection.
func (h *Handler) editEventTime(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, loc *time.Location) { func buildHourPickerKeyboard(cb func(int) string, backData string) models.InlineKeyboardMarkup {
text := "Select hour:"
kbRows := [][]models.InlineKeyboardButton{} kbRows := [][]models.InlineKeyboardButton{}
hourRow := []models.InlineKeyboardButton{} hourRow := []models.InlineKeyboardButton{}
for hh := 0; hh < 24; hh++ { for hh := 0; hh < 24; hh++ {
hourRow = append(hourRow, models.InlineKeyboardButton{ hourRow = append(hourRow, models.InlineKeyboardButton{
Text: fmt.Sprintf("%02d", hh), Text: fmt.Sprintf("%02d", hh), CallbackData: cb(hh),
CallbackData: fmt.Sprintf("edittm_%d_%02d", eventID, hh),
}) })
if len(hourRow) == 6 || hh == 23 { if len(hourRow) == 6 || hh == 23 {
kbRows = append(kbRows, hourRow) kbRows = append(kbRows, hourRow)
hourRow = nil hourRow = nil
} }
} }
kbRows = append(kbRows, []models.InlineKeyboardButton{ kbRows = append(kbRows, []models.InlineKeyboardButton{{Text: "Back", CallbackData: backData}})
{Text: "Back", CallbackData: fmt.Sprintf("back_day_%d", eventID)}, return models.InlineKeyboardMarkup{InlineKeyboard: kbRows}
})
kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows}
h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb})
} }
// editEventTimeMin shows a minute picker (15-min intervals) after hour selection. // buildMinutePickerKeyboard creates the 15-min interval row for minute selection.
func (h *Handler) editEventTimeMin(ctx context.Context, chatID int64, msgID int, user *db.User, eventID int64, hh int, loc *time.Location) { func buildMinutePickerKeyboard(cb func(int) string, backData string) models.InlineKeyboardMarkup {
text := fmt.Sprintf("Select minute for hour %02d:", hh)
kbRows := [][]models.InlineKeyboardButton{} kbRows := [][]models.InlineKeyboardButton{}
minRow := []models.InlineKeyboardButton{} minRow := []models.InlineKeyboardButton{}
for _, mm := range []int{0, 15, 30, 45} { for _, mm := range []int{0, 15, 30, 45} {
minRow = append(minRow, models.InlineKeyboardButton{ minRow = append(minRow, models.InlineKeyboardButton{
Text: fmt.Sprintf("%02d", mm), Text: fmt.Sprintf("%02d", mm), CallbackData: cb(mm),
CallbackData: fmt.Sprintf("edittm_%d_%02d_%02d", eventID, hh, mm),
}) })
} }
kbRows = append(kbRows, minRow) kbRows = append(kbRows, minRow)
kbRows = append(kbRows, []models.InlineKeyboardButton{ kbRows = append(kbRows, []models.InlineKeyboardButton{{Text: "Back", CallbackData: backData}})
{Text: "Back", CallbackData: fmt.Sprintf("edit_time_%d", eventID)}, return models.InlineKeyboardMarkup{InlineKeyboard: kbRows}
}) }
kb := models.InlineKeyboardMarkup{InlineKeyboard: kbRows}
h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ChatID: chatID, MessageID: msgID, Text: text, ReplyMarkup: &kb}) // 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. // 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. // 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) { 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) kb := buildHourPickerKeyboard(
kbRows := [][]models.InlineKeyboardButton{} func(hh int) string { return fmt.Sprintf("addtm_%s_%s_%02d", eventType, date, hh) },
hourRow := []models.InlineKeyboardButton{} "cal_day_"+date,
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}
h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ 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. // 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) { 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) kb := buildMinutePickerKeyboard(
kbRows := [][]models.InlineKeyboardButton{} func(mm int) string { return fmt.Sprintf("addtm_%s_%s_%02d_%02d", eventType, date, hh, mm) },
minRow := []models.InlineKeyboardButton{} "add"+eventType+"_"+date,
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}
h.Bot.EditMessageText(ctx, &bot.EditMessageTextParams{ 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,
}) })
} }