Skip to content

Commit

Permalink
convert the level enum (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic authored and chriswilty committed Apr 8, 2024
1 parent f060b01 commit f0324c2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
4 changes: 1 addition & 3 deletions backend/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 18 additions & 18 deletions backend/src/models/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
9 changes: 8 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { LEVEL_NAMES, LevelSystemRole } from '@src/models/level';
import { LevelSystemRole } from '@src/models/level';

import './HandbookPage.css';

function HandbookSystemRole({
numCompletedLevels,
systemRoles,
}: {
numCompletedLevels: LEVEL_NAMES;
numCompletedLevels: number;
systemRoles: LevelSystemRole[];
}) {
return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/MainComponent/MainComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/models/level.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit f0324c2

Please sign in to comment.