From f0324c2c46424fedc5c528ac2741deb3141f8667 Mon Sep 17 00:00:00 2001 From: Peter Marsh <118171430+pmarsh-scottlogic@users.noreply.github.com> Date: Wed, 28 Feb 2024 09:19:33 +0000 Subject: [PATCH] convert the level enum (#842) --- backend/src/document.ts | 4 +-- backend/src/models/level.ts | 36 +++++++++---------- frontend/src/App.tsx | 9 ++++- .../Pages/HandbookSystemRole.tsx | 4 +-- .../MainComponent/MainComponent.tsx | 2 +- frontend/src/models/level.ts | 14 ++++---- 6 files changed, 38 insertions(+), 31 deletions(-) diff --git a/backend/src/document.ts b/backend/src/document.ts index 4ac0b04d7..f3502b8a3 100644 --- a/backend/src/document.ts +++ b/backend/src/document.ts @@ -93,9 +93,7 @@ async function initDocumentVectors() { const docVectors: DocumentsVector[] = []; const commonDocuments = await getDocuments(getFilepath('common')); - const levelValues = Object.values(LEVEL_NAMES) - .filter((value) => !isNaN(Number(value))) - .map((value) => Number(value)); + const levelValues = Object.values(LEVEL_NAMES); for (const level of levelValues) { const commonAndLevelDocuments = commonDocuments.concat( diff --git a/backend/src/models/level.ts b/backend/src/models/level.ts index e3dcce78e..96e3956c6 100644 --- a/backend/src/models/level.ts +++ b/backend/src/models/level.ts @@ -4,12 +4,14 @@ import { ChatMessage } from './chatMessage'; import { Defence } from './defence'; import { EmailInfo } from './email'; -enum LEVEL_NAMES { - LEVEL_1, - LEVEL_2, - LEVEL_3, - SANDBOX, -} +const LEVEL_NAMES = { + LEVEL_1: 0, + LEVEL_2: 1, + LEVEL_3: 2, + SANDBOX: 3, +} as const; + +type LEVEL_NAMES = (typeof LEVEL_NAMES)[keyof typeof LEVEL_NAMES]; interface LevelState { level: LEVEL_NAMES; @@ -19,18 +21,16 @@ interface LevelState { } function getInitialLevelStates() { - return Object.values(LEVEL_NAMES) - .filter((value) => Number.isNaN(Number(value))) - .map( - (value) => - ({ - level: value as LEVEL_NAMES, - chatHistory: [], - defences: defaultDefences, - sentEmails: [], - } as LevelState) - ); + return Object.values(LEVEL_NAMES).map( + (level) => + ({ + level, + chatHistory: [], + defences: defaultDefences, + sentEmails: [], + } as LevelState) + ); } -export { LEVEL_NAMES, getInitialLevelStates }; +export { getInitialLevelStates, LEVEL_NAMES }; export type { LevelState }; diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index eade00131..1b1ef56ed 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -45,7 +45,14 @@ function App() { const currentLevelStr = localStorage.getItem('currentLevel'); if (currentLevelStr && !isNewUser) { // start the user from where they last left off - return parseInt(currentLevelStr); + const level = parseInt(currentLevelStr); + if (level < LEVEL_NAMES.LEVEL_1 || level > LEVEL_NAMES.SANDBOX) { + console.error( + `Invalid level ${level} in local storage, defaulting to level 1` + ); + return LEVEL_NAMES.LEVEL_1; + } + return parseInt(currentLevelStr) as LEVEL_NAMES; } else { // by default, start on level 1 return LEVEL_NAMES.LEVEL_1; diff --git a/frontend/src/components/HandbookOverlay/Pages/HandbookSystemRole.tsx b/frontend/src/components/HandbookOverlay/Pages/HandbookSystemRole.tsx index 8652b19be..1b1b270db 100644 --- a/frontend/src/components/HandbookOverlay/Pages/HandbookSystemRole.tsx +++ b/frontend/src/components/HandbookOverlay/Pages/HandbookSystemRole.tsx @@ -1,4 +1,4 @@ -import { LEVEL_NAMES, LevelSystemRole } from '@src/models/level'; +import { LevelSystemRole } from '@src/models/level'; import './HandbookPage.css'; @@ -6,7 +6,7 @@ function HandbookSystemRole({ numCompletedLevels, systemRoles, }: { - numCompletedLevels: LEVEL_NAMES; + numCompletedLevels: number; systemRoles: LevelSystemRole[]; }) { return ( diff --git a/frontend/src/components/MainComponent/MainComponent.tsx b/frontend/src/components/MainComponent/MainComponent.tsx index 3390ae6aa..4be21c8b7 100644 --- a/frontend/src/components/MainComponent/MainComponent.tsx +++ b/frontend/src/components/MainComponent/MainComponent.tsx @@ -37,7 +37,7 @@ function MainComponent({ currentLevel: LEVEL_NAMES; numCompletedLevels: number; closeOverlay: () => void; - updateNumCompletedLevels: (level: number) => void; + updateNumCompletedLevels: (level: LEVEL_NAMES) => void; openDocumentViewer: () => void; openInformationOverlay: () => void; openLevelsCompleteOverlay: () => void; diff --git a/frontend/src/models/level.ts b/frontend/src/models/level.ts index 6ec38900c..d458f8f20 100644 --- a/frontend/src/models/level.ts +++ b/frontend/src/models/level.ts @@ -1,9 +1,11 @@ -enum LEVEL_NAMES { - LEVEL_1 = 0, - LEVEL_2, - LEVEL_3, - SANDBOX, -} +const LEVEL_NAMES = { + LEVEL_1: 0, + LEVEL_2: 1, + LEVEL_3: 2, + SANDBOX: 3, +} as const; + +type LEVEL_NAMES = (typeof LEVEL_NAMES)[keyof typeof LEVEL_NAMES]; interface Level { id: LEVEL_NAMES;