Skip to content

Commit

Permalink
fix(pagination): bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ephrimlawrence committed Mar 15, 2024
1 parent b293a82 commit 40a44f4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 81 deletions.
3 changes: 3 additions & 0 deletions src/cli/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class Simulator {

reply(data?: any, input?: string): string | { url: string, body: any } {
if (this.provider == SupportedGateway.wigal) {
// If '#' in input, encode i
input = input?.replace(/#/g, '%23');

// Wigal reply
data ??= {};
data.userdata = input != null ? input : data.userdata;
Expand Down
35 changes: 11 additions & 24 deletions src/core/pagination_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { State } from "@src/models";
import { MenuRouter, DynamicMenu, Menu, Menus } from "@src/menus";
import { Config, ConfigOptions } from "@src/config";
import { BaseMenu, MenuAction } from "@src/menus";
import { buildUserResponse, menuType, validateInput } from "@src/helpers";
import { buildUserResponse, getMenuActions, menuType, validateInput } from "@src/helpers";
import { PaginationItem } from "@src/types/pagination.type";
import { MAXIMUM_CHARACTERS } from "@src/helpers/constants";

Expand All @@ -29,13 +29,7 @@ export class PaginationHandler {
return
}

let actions: MenuAction[] = []

if (menuType(this.menu!) == "class") {
actions = (await (this.menu as unknown as BaseMenu).actions()) || [];
} else {
actions = await (this.menu as DynamicMenu).getActions()
}
let actions: MenuAction[] = await getMenuActions(this.menu)

const pages = await this.generatePaginationItems({
actions: actions,
Expand Down Expand Up @@ -145,7 +139,6 @@ export class PaginationHandler {
menu: opts.menu,
actions: temp,
unpaginatedActions: opts.unpaginatedActions,
// item: paginationItem,
page: opts.page,
pages: opts.pages
})
Expand All @@ -161,14 +154,6 @@ export class PaginationHandler {
return `\n${conf.previousPage.display}\n${conf.nextPage.display}`
}

// goToFirstPage(item: PaginationItem): PaginationItem {
// if (item.previousPage != null && item.page != 1) {
// return this.goToFirstPage(item.previousPage)
// }

// return item;
// }

static get paginationConfig() {
return Config.getInstance().options.pagination ?? new PaginationOption();
}
Expand All @@ -186,13 +171,9 @@ export class PaginationHandler {
return options.includes(input?.trim())
}

// static shouldGoToNextPage(input?: string): boolean {
// if (input == null) {
// return false
// }

// return input.trim() == this.paginationConfig.nextPage?.choice?.trim()
// }
static isPageActionSelected(state: State) {
return !this.isNavActionSelected(state.userData?.trim())
}

private static shouldGoToPreviousPage(input?: string): boolean {
if (input == null) {
Expand All @@ -211,6 +192,12 @@ export class PaginationHandler {
currentPage = pages.find(i => i.page == 1);
} else if (PaginationHandler.shouldGoToPreviousPage(input)) {
currentPage = pages.find(i => i.page == currentPage?.previousPage);

// No more previous pages, might be on the first page but user input prev page;
// navigate to page 1
if (currentPage == null) {
currentPage = pages.find(i => i.page == 1);
}
} else {
currentPage = pages.find(i => i.page == currentPage?.nextPage);
}
Expand Down
50 changes: 13 additions & 37 deletions src/core/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MENU_CACHE, State, StateMode } from "@src/models";
import { MenuRouter, DynamicMenu, Menu, Menus } from "@src/menus";
import { Config, ConfigOptions } from "@src/config";
import { BaseMenu, MenuAction } from "@src/menus";
import { buildUserResponse, menuType, validateInput } from "@src/helpers";
import { buildUserResponse, getMenuActions, menuType, validateInput } from "@src/helpers";
import { FormMenuHandler } from "./form_handler";
import { PaginationHandler } from "./pagination_handler";

Expand Down Expand Up @@ -113,12 +113,9 @@ export class RequestHandler {

// Resolve menu options
if (await this.isPaginatedMenu(currentMenu)) {
const paginationState = state.pagination == null ? null : state.pagination[state.menu.nextMenu!]

// First visited paginated menu, lookup menu options and generate pagination items
// TODO: consider when paginated action is selected (not nav action)
if (paginationState == null) {
if (PaginationHandler.isPageActionSelected(state)) {
await this.lookupMenuOptions(state, currentMenu);
} else {
await new PaginationHandler(
this.request,
this.response,
Expand Down Expand Up @@ -182,10 +179,8 @@ export class RequestHandler {
): Promise<string | undefined> {

// If its a paginated menu, and the user data matches nav choices, skip input validation
if (await this.isPaginatedMenu(menu)) {
if (PaginationHandler.isNavActionSelected(state.userData?.trim())) {
return undefined
}
if ((await this.isPaginatedMenu(menu)) && PaginationHandler.isNavActionSelected(state.userData?.trim())) {
return undefined
}

let result = await validateInput({
Expand All @@ -211,19 +206,6 @@ export class RequestHandler {
) {
// If its a paginated menu, and the user data matches nav choices, skip input validation
if (menu != null && await this.isPaginatedMenu(menu)) {
// const input = state.userData?.trim();
// let actions: MenuAction[] = [],
// paginationState = state.pagination[state.menu?.nextMenu!]

// if (PaginationHandler.shouldGoToPreviousPage(input)) {
// actions = [...paginationState?.data];
// paginationState = { ...paginationState.previousPage! };
// } else {
// actions = [...paginationState?.data];
// paginationState = { ...paginationState.nextPage! };
// }

// state.pagination[state.menu?.nextMenu!] = paginationState
return buildUserResponse({
menu,
actions: PaginationHandler.navigateToPage(state),
Expand All @@ -244,20 +226,20 @@ export class RequestHandler {
}

private async resolveNextMenu(state: State, currentMenu: Menu) {
// If paginated menu, and the user data matches nav choices, we stay on the same page
if (await this.isPaginatedMenu(currentMenu)) {
if (PaginationHandler.isNavActionSelected(state.userData?.trim())) {
return currentMenu
}
}

const action = state.action;

// Execute the action handler
if (action?.handler != null) {
await action.handler(this.request);
}

// If paginated menu, and the user data matches nav choices, we stay on the same page
if (await this.isPaginatedMenu(currentMenu)) {
if (PaginationHandler.isNavActionSelected(state.userData?.trim())) {
return (await this.navigateToNextMenu(state, state.menu?.nextMenu))?.menu;
}
}

// Resolve next menu and make it the current menu
const resp = await this.navigateToNextMenu(state, action?.next_menu);
if (resp.status) {
Expand Down Expand Up @@ -333,13 +315,7 @@ export class RequestHandler {
}

private async lookupMenuOptions(state: State, menu: Menu) {
let actions: MenuAction[] = [];

if (menuType(menu) == "class") {
actions = await (menu as unknown as BaseMenu).actions();
} else {
actions = (menu as DynamicMenu).getActions();
}
const actions: MenuAction[] = await getMenuActions(menu)

// TODO: cache actions for pagination

Expand Down
22 changes: 2 additions & 20 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,8 @@ import { State } from "@src/models";
import { FormInput } from "@src/types";
import { Request, Response } from "@src/types/request";
import { SupportedGateway } from "./constants";
import { menuType } from "./menu.helper";

export function menuType(val: Menu): "class" | "dynamic" {
// TODO: document why this special case is needed
if (/^DynamicMenu$/i.test(val.constructor.name)) {
return "dynamic";
}
return "class";
}

export function instantiateMenu(menu: Menu) {
if (menuType(menu) == "class") {
if (menu instanceof BaseMenu) {
return menu;
}

// @ts-ignore
return new menu(this.request, this.response);
}

return menu;
}

export async function validateInput(opts: {
state: State;
Expand Down Expand Up @@ -132,3 +113,4 @@ export async function buildUserResponse(
}

export { SupportedGateway }
export { getMenuActions, menuType } from './menu.helper'
30 changes: 30 additions & 0 deletions src/helpers/menu.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BaseMenu, DynamicMenu, Menu, MenuAction } from "../menus";

// export function instantiateMenu(menu: Menu) {
// if (menuType(menu) == "class") {
// if (menu instanceof BaseMenu) {
// return menu;
// }

// // @ts-ignore
// return new menu(this.request, this.response);
// }

// return menu;
// }

export function menuType(val: Menu): "class" | "dynamic" {
// TODO: document why this special case is needed
if (/^DynamicMenu$/i.test(val.constructor.name)) {
return "dynamic";
}
return "class";
}

export async function getMenuActions(menu: Menu): Promise<MenuAction[]> {
if (menuType(menu!) == "class") {
return (await (menu as unknown as BaseMenu).actions()) || [];
} else {
return await (menu as DynamicMenu).getActions()
}
}

0 comments on commit 40a44f4

Please sign in to comment.