Skip to content

Commit 800c472

Browse files
authored
refactor: UX improvement
Merge pull request #23 from d-Rickyy-b/ux-improv
2 parents 779b114 + 2a3b79f commit 800c472

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

internal/bot/bot.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,12 @@ func mainMenuHandler(b *gotgbot.Bot, ctx *ext.Context) error {
179179
func showPriceagentDetail(b *gotgbot.Bot, ctx *ext.Context) error {
180180
cb := ctx.Update.CallbackQuery
181181

182-
priceagentID, parseErr := parseIDFromCallbackData(cb.Data, "m03_00_")
182+
menu, parseErr := models.NewMenu(cb.Data)
183183
if parseErr != nil {
184-
return fmt.Errorf("showPriceagentDetail: failed to parse priceagentID from callback data: %w", parseErr)
184+
return fmt.Errorf("showPriceagentDetail: failed to parse callback data: %w", parseErr)
185185
}
186186

187-
priceagent, dbErr := database.GetPriceagentForUserByID(ctx.EffectiveUser.Id, priceagentID)
187+
priceagent, dbErr := database.GetPriceagentForUserByID(ctx.EffectiveUser.Id, menu.PriceAgent)
188188
if dbErr != nil {
189189
return fmt.Errorf("showPriceagentDetail: failed to get priceagent from database: %w", dbErr)
190190
}
@@ -220,21 +220,25 @@ func showPriceagentDetail(b *gotgbot.Bot, ctx *ext.Context) error {
220220
},
221221
}
222222

223-
// Check if the initial message contained a photo, if yes, we're coming from the price history graph
224-
if len(cb.Message.Photo) > 0 {
223+
switch menu.SubMenu {
224+
case "00":
225+
_, err := cb.Message.EditText(b, editedText, &gotgbot.EditMessageTextOpts{ReplyMarkup: markup, ParseMode: "HTML"})
226+
if err != nil {
227+
return fmt.Errorf("showPriceagent: failed to edit message text: %w", err)
228+
}
229+
case "01":
225230
bot.DeleteMessage(ctx.EffectiveChat.Id, cb.Message.MessageId)
226231

227232
_, err := b.SendMessage(ctx.EffectiveChat.Id, editedText, &gotgbot.SendMessageOpts{ReplyMarkup: markup, ParseMode: "HTML"})
228233
if err != nil {
229234
return fmt.Errorf("showPriceagent: failed to send new message: %w", err)
230235
}
231-
} else {
232-
_, err := cb.Message.EditText(b, editedText, &gotgbot.EditMessageTextOpts{ReplyMarkup: markup, ParseMode: "HTML"})
236+
case "02":
237+
_, err := b.SendMessage(ctx.EffectiveChat.Id, editedText, &gotgbot.SendMessageOpts{ReplyMarkup: markup, ParseMode: "HTML"})
233238
if err != nil {
234-
return fmt.Errorf("showPriceagent: failed to edit message text: %w", err)
239+
return fmt.Errorf("showPriceagent: failed to send new message: %w", err)
235240
}
236241
}
237-
238242
return nil
239243
}
240244

@@ -410,7 +414,7 @@ func addMessageHandlers(dispatcher *ext.Dispatcher) {
410414
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("m04_02_"), setNotificationBelowHandler))
411415
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("m04_01_"), setNotificationAlwaysHandler))
412416
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("m04_00_"), changePriceagentSettingsHandler))
413-
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("m03_00_"), showPriceagentDetail))
417+
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Prefix("m03_"), showPriceagentDetail))
414418
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Equal("m02_00"), showWishlistPriceagents))
415419
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Equal("m02_01"), showProductPriceagents))
416420
dispatcher.AddHandler(handlers.NewCallback(callbackquery.Equal("m01_01"), viewPriceagentsHandler))

internal/bot/models/menu.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package models
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
type Menu struct {
10+
ID string
11+
SubMenu string
12+
PriceAgent int64
13+
Extra string
14+
}
15+
16+
// NewMenu returns a menu struct from a given menuData string. The menu follows the format <menuID>_<submenuID>_<priceagentID>[_<extraData>]
17+
func NewMenu(menuData string) (*Menu, error) {
18+
if len(menuData) > 64 {
19+
return nil, errors.New("length of menu data exceeding 64 bytes")
20+
}
21+
components := strings.Split(menuData, "_")
22+
if len(components) != 3 && len(components) != 4 {
23+
return nil, errors.New("unable to parse menu data")
24+
}
25+
26+
priceagentID, parseErr := strconv.Atoi(components[2])
27+
if parseErr != nil {
28+
return nil, parseErr
29+
}
30+
31+
menu := &Menu{
32+
ID: components[0],
33+
SubMenu: components[1],
34+
PriceAgent: int64(priceagentID),
35+
}
36+
37+
if len(components) == 4 {
38+
menu.Extra = components[3]
39+
}
40+
return menu, nil
41+
}

internal/bot/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func notifyUsers(priceAgent models.PriceAgent, oldEntity, updatedEntity geizhals
9090
markup := gotgbot.InlineKeyboardMarkup{
9191
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
9292
{
93-
{Text: "Zum Preisagenten!", CallbackData: fmt.Sprintf("m03_00_%d", priceAgent.ID)},
93+
{Text: "Zum Preisagenten!", CallbackData: fmt.Sprintf("m03_02_%d", priceAgent.ID)},
9494
},
9595
},
9696
}

internal/bot/pricehistory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func showPriceHistoryHandler(b *gotgbot.Bot, ctx *ext.Context) error {
3636
markup := gotgbot.InlineKeyboardMarkup{
3737
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
3838
dateRangeKeyboard,
39-
{{Text: "↩️ Zurück", CallbackData: fmt.Sprintf("m03_00_%d", priceagent.ID)}},
39+
{{Text: "↩️ Zurück", CallbackData: fmt.Sprintf("m03_01_%d", priceagent.ID)}},
4040
},
4141
}
4242

@@ -91,7 +91,7 @@ func updatePriceHistoryGraphHandler(b *gotgbot.Bot, ctx *ext.Context) error {
9191
markup := gotgbot.InlineKeyboardMarkup{
9292
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
9393
dateRangeKeyboard,
94-
{{Text: "↩️ Zurück", CallbackData: fmt.Sprintf("m03_00_%d", priceagent.ID)}},
94+
{{Text: "↩️ Zurück", CallbackData: fmt.Sprintf("m03_01_%d", priceagent.ID)}},
9595
},
9696
}
9797

0 commit comments

Comments
 (0)