Skip to content

Commit

Permalink
Merge pull request #33 from microsoft/won
Browse files Browse the repository at this point in the history
Update game screen to handle navigation, continue api calls, etc
  • Loading branch information
WonSong committed Sep 18, 2024
2 parents 1947da9 + ab5571c commit 7ce64aa
Show file tree
Hide file tree
Showing 23 changed files with 5,395 additions and 10,752 deletions.
1 change: 1 addition & 0 deletions Client/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tabWidth: 2
15,749 changes: 5,141 additions & 10,608 deletions Client/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"devDependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-private-property-in-object": "^7.24.7",
"prettier": "^3.3.3",
"sass": "^1.78.0"
}
}
4 changes: 2 additions & 2 deletions Client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function App() {
<Layout>
<Routes>
<Route path="/" element={<StartScreen />} />
<Route path="/career-game/:conversation-id" element={<GameScreen />} />
<Route path="/career-game" element={<GameScreen />} />
<Route
path="/career-game/:conversation-id/results"
path="/career-game/results"
element={<ResultScreen />}
/>
<Route path="/admin" element={<AdminScreen />} />
Expand Down
14 changes: 11 additions & 3 deletions Client/src/components/Form/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ export type DropdownProps = {

export function Dropdown(props: DropdownProps): React.ReactElement {
const handleDropdownChanged = (
event: React.ChangeEvent<HTMLSelectElement>
event: React.ChangeEvent<HTMLSelectElement>,
) => {
props.onChange(props.name, event.target.value);
};

return (
<div className="dropdown-container">
{props.label && <label className="dropdown-label">{props.label}</label>}
{props.label && (
<label className="dropdown-label" htmlFor={props.name}>
{props.label}
</label>
)}

<select className="dropdown-root" onChange={handleDropdownChanged}>
<select
className="dropdown-root"
onChange={handleDropdownChanged}
name={props.name}
>
{props.options.map((option) => (
<option
key={option.value}
Expand Down
4 changes: 2 additions & 2 deletions Client/src/components/GameProgress/GameProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export function GameProgress(): React.ReactElement {
React.useContext<IAppContext>(AppContext);

const completedRounds = rounds.filter(
(round) => round.optionSeleted !== null
(round) => round.optionSelected !== null
).length;

const progressPercentage = (completedRounds / numberOfRounds) * 100;
const progressPercentage = ((completedRounds + 1) / numberOfRounds) * 100;

return (
<div className="progress-bar-container">
Expand Down
33 changes: 0 additions & 33 deletions Client/src/components/Layout/Header/Header.scss

This file was deleted.

19 changes: 0 additions & 19 deletions Client/src/components/Layout/Header/Header.tsx

This file was deleted.

1 change: 0 additions & 1 deletion Client/src/components/Layout/Header/index.ts

This file was deleted.

11 changes: 1 addition & 10 deletions Client/src/components/Layout/Layout.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
.header-container {
position: fixed;
top: 0;
left: 0;
height: 50px;
width: 100%;
}

.content-container {
padding-top: 50px;
display: flex;
flex-direction: row;
height: 100%;
Expand Down Expand Up @@ -40,4 +31,4 @@
max-width: 150px;
height: auto;
margin-bottom: 100px;
}
}
4 changes: 0 additions & 4 deletions Client/src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { LeftNav } from "./LeftNav";
import { SkipToMain } from "./SkipToMain";
import { RightAside } from "./RightAside";
import "./Layout.scss";
import { Header } from "./Header";

export type LayoutProps = {
children: React.ReactNode;
Expand All @@ -16,9 +15,6 @@ export function Layout(props: LayoutProps): React.ReactElement {
return (
<>
<SkipToMain />
<div className="header-container">
<Header />
</div>
<div className="content-container">
<LeftNav />
<div className="main-container">
Expand Down
11 changes: 7 additions & 4 deletions Client/src/components/Layout/LeftNav/LeftNav.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
}
}

.brand {
display: block;
margin-bottom: 30px;
}

.left-nav-items {
display: flex;
flex-direction: column;
align-items: left;
align-items: normal;
padding: 20px;
}

.nav-logo {
max-height: 30px;
width: auto;
margin-right: 10px;
margin-bottom: 30px;
}
}
11 changes: 7 additions & 4 deletions Client/src/components/Layout/LeftNav/LeftNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { Link } from "react-router-dom";
export function LeftNav(): React.ReactElement {
return (
<aside className="nav-root">
<nav>
<Link to="/" title="Go to CareerCraft Home page" className="brand">
<img
src="Flat_logo_linear.png"
alt="CareerCraft logo"
className="nav-logo"
src="Flat_logo_linear.png"
alt="CareerCraft logo"
className="nav-logo"
/>
</Link>

<nav>
<ul>
<li className="left-nav-items">
<Link to="/" title="Go to CareerCraft About Us page">
Expand Down
40 changes: 33 additions & 7 deletions Client/src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,62 @@ import { GameRound } from "../models/GameRound";
export const AppContext = React.createContext<IAppContext>({});

export function AppContextProvider(
props: React.PropsWithChildren<{}>
props: React.PropsWithChildren<{}>,
): React.ReactElement {
const [numberOfRounds, setNumberOfRounds] = React.useState<number>(5);
const [gameRounds, setGameRounds] = React.useState<GameRound[]>([]);
const [selectedRound, setSelectedRound] = React.useState<number>(0);
const [gameId, setGameId] = React.useState<string | null>(null);

const updateNumberOfRounds = React.useCallback((numberOfRounds: number) => {
setNumberOfRounds(numberOfRounds);
}, []);

const addRound = React.useCallback(
(round: GameRound) => {
setGameRounds([...gameRounds, round]);
},
[gameRounds]
);
const addRound = React.useCallback((round: GameRound) => {
setGameRounds((prev) => [...prev, round]);
}, []);

const selectRound = React.useCallback((selectedRound: number) => {
setSelectedRound(selectedRound);
}, []);

const completeRound = React.useCallback(
(roundIndex: number, selectedOptionIndex: number) => {
setGameRounds((prev) => {
const rounds: GameRound[] = [];
prev.forEach((p: GameRound, i: number) => {
rounds.push({
...p,
optionSelected:
i === roundIndex ? selectedOptionIndex : p.optionSelected,
});
});

return rounds;
});
},
[],
);

const resetGame = React.useCallback(() => {
setGameRounds([]);
setGameId(null);
selectRound(0);
}, [selectRound]);

return (
<AppContext.Provider
value={{
gameId,
setGameId,
numberOfRounds,
setNumberOfRounds: updateNumberOfRounds,
rounds: gameRounds,
addRound,
selectedRound,
selectRound,
completeRound,
resetGame,
}}
>
{props.children}
Expand Down
67 changes: 67 additions & 0 deletions Client/src/hooks/useCareerGame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from "react";
import { AppContext } from "../context/AppContext";
import { GameResponse } from "../models/GameResponse";
import { IAppContext } from "../models/IAppContext";
import { Http } from "../services/Http";

export function useCareerGame() {
const { addRound, setGameId } = React.useContext<IAppContext>(AppContext);
const [isLoading, setLoading] = React.useState<boolean>(false);

const startGame = async (career: string): Promise<void> => {
try {
setLoading(true);
const response = await Http.getInstance().post<GameResponse>(
"/api/career-game/start",
{
careerChoice: career,
},
);

if (response.ok && response.data) {
setGameId(response.data.conversationId)
addRound({
...response.data.round,
optionSelected: null,
});
}
} catch (error: any) {
console.log(error)
} finally {
setLoading(false);
}
};

const continueGame = async (gameId: string, userResponse: string): Promise<void> => {
try {
setLoading(true);
const response = await Http.getInstance().post<GameResponse>(
"/api/career-game/continue",
{
conversationId: gameId,
response: userResponse,
},
);

if (response.ok && response.data) {
addRound({
...response.data.round,
optionSelected: null,
});
}
} catch (error: any) {
console.log(error)
} finally {
setLoading(false);
}
};

const completeGame = async (response: string): Promise<void> => {};

return {
startGame,
continueGame,
completeGame,
isLoading,
}
}
3 changes: 2 additions & 1 deletion Client/src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ a,
button,
input,
select,
textarea {
textarea,
#main {
&:focus {
outline: 2px dashed $background-dark;

Expand Down
6 changes: 6 additions & 0 deletions Client/src/models/GameResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { GameRound } from "./GameRound";

export type GameResponse = {
conversationId: string;
round: GameRound;
};
2 changes: 1 addition & 1 deletion Client/src/models/GameRound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export type GameRound = {
options: string[];
scenario: string;
outcome: string;
optionSeleted: number | null;
optionSelected: number | null;
};
15 changes: 14 additions & 1 deletion Client/src/models/IAppContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { GameRound } from "./GameRound";

export interface IAppContext {
gameId: string | null;

setGameId(gameId: string | null): void;

selectedRound: number | 0;

selectRound(selectedRound: number): void;

numberOfRounds: number;

setNumberOfRounds(numberOfRounds: number): void;

rounds: GameRound[];
addRound: (round: GameRound) => void;

addRound(round: GameRound): void;

completeRound(roundIndex: number, selectedOptionIndex: number): void;

resetGame(): void;
}
Loading

0 comments on commit 7ce64aa

Please sign in to comment.