Skip to content

Commit

Permalink
feat: update menu when user presses a hidden button
Browse files Browse the repository at this point in the history
As the button is hidden the user must have an old menu.
Update it in order to help the user understand.
  • Loading branch information
EdJoPaTo committed Sep 18, 2018
1 parent 5403781 commit 5ba3471
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ class TelegrafInlineMenu {
if (handler.submenu) {
middleware = handler.submenu.middleware(childActionCode.get(), subOptions)
} else {
// Run the setMenuFunc even when action is hidden.
// As the button should be hidden already the user must have an old menu
// Update the menu to show the user why this will not work
middlewareOptions.runAfterFuncEvenWhenHidden = true
middlewareOptions.only = async ctx => {
if (ctx.updateType !== 'callback_query') {
return false
Expand Down
12 changes: 8 additions & 4 deletions middleware-helper.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
function createHandlerMiddleware(middleware, {
hide,
afterFunc,
runAfterFuncEvenWhenHidden,
only
} = {}) {
return async (ctx, next) => {
if (only && !(await only(ctx))) {
return next(ctx)
}
if (hide && (await hide(ctx))) {
return next(ctx)
const isHidden = hide && (await hide(ctx))
if (isHidden) {
await next(ctx)
} else {
await middleware(ctx, next)
}
await middleware(ctx, next)
if (afterFunc) {

if (afterFunc && (!isHidden || runAfterFuncEvenWhenHidden)) {
await afterFunc(ctx)
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ test('hides key in keyboard', async t => {
})

test('hidden key can not be set', async t => {
t.plan(1)
t.plan(2)
const menu = new TelegrafInlineMenu('foo')
menu.select('c', ['a', 'b'], {
setFunc: t.fail,
Expand All @@ -208,7 +208,7 @@ test('hidden key can not be set', async t => {
const bot = new Telegraf()
bot.use(menu.init({actionCode: 'a:b'}))

bot.context.editMessageText = () => Promise.resolve(t.fail())
bot.context.editMessageText = () => Promise.resolve(t.pass())
bot.use(() => t.pass())

await bot.handleUpdate({callback_query: {data: 'a:b:c:a'}})
Expand Down

0 comments on commit 5ba3471

Please sign in to comment.