Skip to content

Commit

Permalink
fix: class based menu lookup bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ephrimlawrence committed Mar 10, 2024
1 parent 21e0601 commit 4881765
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/core/request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export class RequestHandler {
menu = this.router.getMenu(id);
} else if (state.isStart) {
// If it is a start request, lookup for the start menu
const value = this.router.getStartMenu(this.request, this.response);
const value = await this.router.getStartMenu(this.request, this.response);
id = value.id;
menu = value.obj;
}
Expand Down
4 changes: 2 additions & 2 deletions src/menus/base.menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export abstract class BaseMenu {
constructor(
protected readonly request: Request,
protected readonly response: Response,
) {}
) { }

async validate(data?: string): Promise<ValidationResponse> {
return true;
Expand All @@ -30,7 +30,7 @@ export abstract class BaseMenu {
return this.request.query?.sessionid!;
}

get isStart(): Promise<boolean> {
async isStart(): Promise<boolean> {
return Promise.resolve(false);
}

Expand Down
41 changes: 27 additions & 14 deletions src/menus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ export { BaseMenu } from "./base.menu";
export { DynamicMenu } from "./dynamic_menu.menu";
export { ValidationResponse } from "@src/types";

// TODO: Keep list of menus cached in a map, globally
import { Request, Response } from "@src/types/request";
import { Type } from "@src/types";
import { BaseMenu } from "@src/menus/base.menu";
import { DynamicMenu } from "@src/menus";
import { MENU_CACHE } from "@src/core/state.core";
import { menuType } from "..";

export class Menus {
private static instance: Menus;

// private items: { [menuId: string]: Type<BaseMenu> | DynamicMenu } = {};

private constructor() {}
private constructor() { }

public static getInstance(): Menus {
if (!Menus.instance) {
Expand All @@ -34,8 +34,6 @@ export class Menus {
add(cls: Type<BaseMenu>, name: string): void {
// const _menu = new cls(cls.name, cls);
MENU_CACHE[name] = cls;

// return _menu;
}

menu(id: string): DynamicMenu {
Expand All @@ -49,25 +47,40 @@ export class Menus {
return MENU_CACHE;
}

getStartMenu(
async getStartMenu(
req: Request,
res: Response,
): { id: string; obj: DynamicMenu | Type<BaseMenu> } {
const start = Object.keys(MENU_CACHE).find((id) => {
): Promise<{ id: string; obj: DynamicMenu | Type<BaseMenu>; }> {
let startId: string | undefined;

for (const id in MENU_CACHE) {
let isStart = false;
const menu = MENU_CACHE[id];
if (menu instanceof BaseMenu) {
// @ts-ignore
return new menu(req, res).isStart;

if (menuType(menu) == "class") {
if (menu instanceof BaseMenu) {
isStart = await menu.isStart()
} else {
// @ts-ignore
isStart = (await new menu(req, res).isStart)
}
} else {

isStart = (menu as DynamicMenu).isStart
}

return (menu as DynamicMenu).isStart;
});
if (isStart) {
startId = id;
break;
}
}

if (start == undefined) {
// console.log(start, MENU_CACHE)
if (startId == undefined) {
throw new Error("No start menu defined. Please define a start menu");
}

return { id: start, obj: MENU_CACHE[start] };
return { id: startId, obj: MENU_CACHE[startId] };
}

getMenu(id: string): DynamicMenu | Type<BaseMenu> {
Expand Down
2 changes: 1 addition & 1 deletion thesis.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"path": "."
},
{
"path": "../instant-settlement"
"path": "../direct-debit-ussd"
}
],
"settings": {
Expand Down

0 comments on commit 4881765

Please sign in to comment.