Skip to content

Commit

Permalink
feat: Implement displaying user scope depending on game manifest :)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnslig committed Dec 30, 2021
1 parent 0592632 commit 415d5a1
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 18 deletions.
19 changes: 19 additions & 0 deletions app/src/common/hooks/api/useGame.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { renderHook } from "@testing-library/react-hooks";

import useGame from "./useGame";

it("loads the game", async () => {
const { result, waitForValueToChange } = renderHook(() => useGame());

await waitForValueToChange(() => result.current.data);
expect(result.current.data).toEqual(require("../../../mocks/data/game.json"));
});

it("returns an error when the game is not found", async () => {
const { result, waitForValueToChange } = renderHook(() => useGame());

await waitForValueToChange(() => result.current.error);
expect(result.current.error).toEqual(
require("../../../mocks/data/game-not-found.json")
);
});
26 changes: 26 additions & 0 deletions app/src/common/hooks/api/useGame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import useSWR from "swr";

import { Game } from "../../types/Game";
import ApiError from "./helper/ApiError";
import config from "../../../config";
import errorAwareFetcher from "./helper/errorAwareFetcher";
import useApiUrl from "./useApiUrl";

interface GameApiResponse {
data: Game;
}

/**
* A React hook that retrieves the game manifest
*
* @returns The game manifest. May be null during initial load
*/
const useGame = () => {
const url = useApiUrl((gameId) => config.apiEndpoints.game(gameId));
return useSWR<GameApiResponse, ApiError>(
() => url,
(url) => errorAwareFetcher(() => fetch(url))
);
};

export default useGame;
8 changes: 8 additions & 0 deletions app/src/common/types/Game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Game {
type: "game";
id: string;
attributes: {
hasMessages: boolean;
hasUserParameterScope: boolean;
};
}
2 changes: 2 additions & 0 deletions app/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface Config {
apiEndpoints: {
clock: (gameId: string) => string;
code: (codeId: string, gameId: string) => string;
game: (gameId: string) => string;
parameters: (gameId: string) => string;
};

Expand All @@ -19,6 +20,7 @@ const config: Config = {
clock: (gameId: string) => `${process.env.API_ROOT}/games/${gameId}/clock`,
code: (codeId: string, gameId: string) =>
`${process.env.API_ROOT}/games/${gameId}/codes/${codeId}`,
game: (gameId: string) => `${process.env.API_ROOT}/games/${gameId}`,
parameters: (gameId: string) =>
`${process.env.API_ROOT}/games/${gameId}/parameters`,
},
Expand Down
39 changes: 22 additions & 17 deletions app/src/features/parameters/Parameters.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
import { FormattedMessage } from "react-intl";
import * as React from "react";

import ParameterCard from "./ParameterCard";
import {
useParameters,
useScopedParameterResponse,
} from "../../common/hooks/api/useParameters";
import ParameterCard from "./ParameterCard";
import useGame from "../../common/hooks/api/useGame";

const Parameters = () => {
const game = useGame();

const { data } = useParameters();
const userParams = useScopedParameterResponse("user", data);
const globalParams = useScopedParameterResponse("global", data);

return (
<>
<ParameterCard
title={
<FormattedMessage
defaultMessage="Persönliches"
description="personal parameter card title"
/>
}
description={
<FormattedMessage
defaultMessage="Diese Parameter betreffen nur dich selber. Tausche sie gegen Aktionskarten ein!"
description="personal parameter card description"
/>
}
params={userParams}
numSkeletonParams={1}
/>
{game.data?.data.attributes.hasUserParameterScope && (
<ParameterCard
title={
<FormattedMessage
defaultMessage="Persönliches"
description="personal parameter card title"
/>
}
description={
<FormattedMessage
defaultMessage="Diese Parameter betreffen nur dich selber. Tausche sie gegen Aktionskarten ein!"
description="personal parameter card description"
/>
}
params={userParams}
numSkeletonParams={1}
/>
)}
<ParameterCard
title={
<FormattedMessage
Expand Down
9 changes: 9 additions & 0 deletions app/src/mocks/data/game-not-found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"errors": [
{
"id": "not-found",
"status": 404,
"title": "Unknown game"
}
]
}
6 changes: 6 additions & 0 deletions app/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export const handlers = [
}),

rest.get("/games/:gameId", (req, res, ctx) => {
if (req.params.gameId === "not-found") {
return res(
ctx.status(404),
ctx.json(require("./data/game-not-found.json"))
);
}
return res(ctx.json(require("./data/game.json")));
}),
];
3 changes: 2 additions & 1 deletion docs/ABC-DPT.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ paths:
required: true
get:
summary: Your GET endpoint
tags: []
tags:
- App
responses:
"200":
description: OK
Expand Down

0 comments on commit 415d5a1

Please sign in to comment.