Skip to content

Commit

Permalink
feat: select/toggle update menu afterwards is now explicit
Browse files Browse the repository at this point in the history
this enables for more customization like #98 requires
  • Loading branch information
EdJoPaTo committed Jun 24, 2020
1 parent 3445c12 commit 5e09689
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 6 additions & 0 deletions examples/main-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ let mainMenuToggle = false
menu.toggle('toggle me', 'toggle me', {
set: (_, newState) => {
mainMenuToggle = newState
// Update the menu afterwards
return true
},
isSet: () => mainMenuToggle
})
Expand Down Expand Up @@ -44,6 +46,7 @@ menu.select('select', ['A', 'B', 'C'], {
set: async (ctx, key) => {
selectedKey = key
await ctx.answerCbQuery(`you selected ${key}`)
return true
},
isSet: (_, key) => key === selectedKey
})
Expand Down Expand Up @@ -82,6 +85,7 @@ foodSelectSubmenu.toggle('Prefer tea', 'tea', {
set: (ctx, choice) => {
const person = ctx.match![1]
people[person].tee = choice
return true
},
isSet: ctx => {
const person = ctx.match![1]
Expand All @@ -92,6 +96,7 @@ foodSelectSubmenu.select('food', food, {
set: (ctx, key) => {
const person = ctx.match![1]
people[person].food = key
return true
},
isSet: (ctx, key) => {
const person = ctx.match![1]
Expand Down Expand Up @@ -183,6 +188,7 @@ mediaMenu.select('type', ['animation', 'document', 'photo1', 'photo2', 'video',
isSet: (_, key) => mediaOption === key,
set: (_, key) => {
mediaOption = key
return true
}
})
mediaMenu.manualRow(createBackMainMenuButtons())
Expand Down
2 changes: 1 addition & 1 deletion source/buttons/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {prefixEmoji} from '../prefix'
import {getButtonsOfPage, getButtonsAsRows} from './align'

export type IsSetFunction<Context> = (context: Context, key: string) => ConstOrPromise<boolean>
export type SetFunction<Context> = (context: Context, key: string, newState: boolean) => ConstOrPromise<void>
export type SetFunction<Context> = (context: Context, key: string, newState: boolean) => ConstOrPromise<string | boolean>
export type FormatStateFunction<Context> = (context: Context, textResult: string, state: boolean, key: string) => ConstOrPromise<string>

export interface SelectOptions<Context> extends ManyChoicesOptions<Context> {
Expand Down
2 changes: 1 addition & 1 deletion source/buttons/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {SingleButtonOptions} from './basic'
export type FormatStateFunction<Context> = (context: Context, text: string, state: boolean, path: string) => ConstOrPromise<string>

export interface ToggleOptions<Context> extends SingleButtonOptions<Context> {
readonly set: (context: Context, newState: boolean, path: string) => ConstOrPromise<void>;
readonly set: (context: Context, newState: boolean, path: string) => ConstOrPromise<string | boolean>;
readonly isSet: ContextPathFunc<Context, boolean>;
readonly formatState?: FormatStateFunction<Context>;
}
Expand Down
22 changes: 10 additions & 12 deletions source/menu-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class MenuTemplate<Context> {
* menuTemplate.interact('Knock Knock', 'unique', {
* do: async context => {
* await context.answerCbQuery('Who is there?')
* return false // Do not update the menu afterwards
* }
* })
* @example
Expand Down Expand Up @@ -252,6 +253,7 @@ export class MenuTemplate<Context> {
* do: async ctx => {
* console.log('Take a look at ctx.match. It contains the chosen city', ctx.match)
* await ctx.answerCbQuery('You hit a button in a submenu')
* return false
* }
* })
* submenu.manualRow(createBackMainMenuButtons())
Expand Down Expand Up @@ -290,6 +292,7 @@ export class MenuTemplate<Context> {
* isSet: (context, key) => context.session.currentLocation === key,
* set: (context, key) => {
* context.session.currentLocation = key
* return true
* }
* })
* @example
Expand All @@ -299,6 +302,7 @@ export class MenuTemplate<Context> {
* isSet: (context, key) => Boolean(context.session.bodyparts[key]),
* set: (context, key, newState) => {
* context.session.bodyparts[key] = newState
* return true
* }
* })
*/
Expand All @@ -316,8 +320,7 @@ export class MenuTemplate<Context> {
trueTrigger,
async (context, path) => {
const key = getKeyFromPath(trueTrigger, path)
await options.set(context, key, true)
return '.'
return options.set(context, key, true)
},
combineHideAndChoices(actionPrefix + 'T', choices, options.hide)
)
Expand All @@ -327,8 +330,7 @@ export class MenuTemplate<Context> {
falseTrigger,
async (context, path) => {
const key = getKeyFromPath(falseTrigger, path)
await options.set(context, key, false)
return '.'
return options.set(context, key, false)
},
combineHideAndChoices(actionPrefix + 'F', choices, options.hide)
)
Expand Down Expand Up @@ -378,6 +380,7 @@ export class MenuTemplate<Context> {
* isSet: context => Boolean(context.session.isFunny),
* set: (context, newState) => {
* context.session.isFunny = newState
* return true
* }
* })
* @example
Expand All @@ -387,6 +390,7 @@ export class MenuTemplate<Context> {
* isSet: context => Boolean(context.session.lamp),
* set: (context, newState) => {
* context.session.lamp = newState
* return true
* }
* })
*/
Expand All @@ -401,19 +405,13 @@ export class MenuTemplate<Context> {

this._actions.add(
new RegExp(actionPrefix + ':true$'),
async (context, path) => {
await options.set(context, true, path)
return '.'
},
async (context, path) => options.set(context, true, path),
options.hide
)

this._actions.add(
new RegExp(actionPrefix + ':false$'),
async (context, path) => {
await options.set(context, false, path)
return '.'
},
async (context, path) => options.set(context, false, path),
options.hide
)

Expand Down
6 changes: 4 additions & 2 deletions test/menu-template/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ test('action true', async t => {
t.is(context, 'foo')
t.is(key, 'Button')
t.is(newState, true)
return 'wow'
}
})

const actions = [...menu.renderActionHandlers(/^\//)]
const action = actions.find(o => o.trigger.source.includes('uniqueT'))!
const result = await action.doFunction('foo', '/uniqueT:Button')
t.is(result, '.')
t.is(result, 'wow')
})

test('action false', async t => {
Expand All @@ -150,13 +151,14 @@ test('action false', async t => {
t.is(context, 'foo')
t.is(key, 'Button')
t.is(newState, false)
return 'wow'
}
})

const actions = [...menu.renderActionHandlers(/^\//)]
const action = actions.find(o => o.trigger.source.includes('uniqueF'))!
const result = await action.doFunction('foo', '/uniqueF:Button')
t.is(result, '.')
t.is(result, 'wow')
})

test('with pagnination buttons', async t => {
Expand Down
6 changes: 4 additions & 2 deletions test/menu-template/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ test('action true', async t => {
t.is(context, 'foo')
t.is(newState, true)
t.is(path, '/unique:true')
return 'wow'
}
})

const actions = [...menu.renderActionHandlers(/^\//)]
const action = actions.find(o => o.trigger.source.includes('true'))!
const result = await action.doFunction('foo', '/unique:true')
t.is(result, '.')
t.is(result, 'wow')
})

test('action false', async t => {
Expand All @@ -126,11 +127,12 @@ test('action false', async t => {
t.is(context, 'foo')
t.is(newState, false)
t.is(path, '/unique:false')
return 'wow'
}
})

const actions = [...menu.renderActionHandlers(/^\//)]
const action = actions.find(o => o.trigger.source.includes('false'))!
const result = await action.doFunction('foo', '/unique:false')
t.is(result, '.')
t.is(result, 'wow')
})

0 comments on commit 5e09689

Please sign in to comment.