Skip to content

Commit

Permalink
feat: Implement leave game menu option
Browse files Browse the repository at this point in the history
Closes #16
  • Loading branch information
barnslig committed Jul 16, 2021
1 parent 2439a05 commit 22b0eb5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
12 changes: 12 additions & 0 deletions app/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@
"defaultMessage": "Ausführen",
"description": "confirm qr code button"
},
"KqZNMU": {
"defaultMessage": "Spiel verlassen",
"description": "appbar menu item leave game"
},
"OgWAX/": {
"defaultMessage": "QR-Code Aktionen",
"description": "title of the confirm qr code actions page"
},
"OzAuhp": {
"defaultMessage": "Spiel wirklich verlassen?",
"description": "leave game confirm message"
},
"P5jGEu": {
"defaultMessage": "Lebensmittelvorrat",
"description": "parameter label"
Expand Down Expand Up @@ -79,6 +87,10 @@
"defaultMessage": "Status",
"description": "main nav main page item label"
},
"ZelVIn": {
"defaultMessage": "Spiel erfolgreich verlassen!",
"description": "success snackbar on leave game"
},
"eEE+z4": {
"defaultMessage": "Diversity Puzzle Trails",
"description": "main page title"
Expand Down
22 changes: 22 additions & 0 deletions app/src/app/pages/IndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import PausedHeroMessage from "../../common/components/PausedHeroMessage";
import useCharacter from "../../common/hooks/useCharacter";
import useClock from "../../common/hooks/api/useClock";
import useUpdateClock from "../../common/hooks/api/useUpdateClock";
import useLeaveGame from "../../common/hooks/useLeaveGame";

const bg = require("./bg.svg");

Expand Down Expand Up @@ -58,6 +59,21 @@ const IndexPage = (props: IndexPageProps) => {
const [appBarMenuIsOpen, setAppBarMenuIsOpen] = React.useState(false);
const [updateClockIsLoading, updateClock] = useUpdateClock();

const leaveGame = useLeaveGame();
const onLeaveGame = () => {
if (
window.confirm(
intl.formatMessage({
defaultMessage: "Spiel wirklich verlassen?",
description: "leave game confirm message",
})
)
) {
leaveGame();
}
setAppBarMenuIsOpen(false);
};

return (
<div className={classes.root}>
<AppBar position="fixed" color="inherit">
Expand Down Expand Up @@ -132,6 +148,12 @@ const IndexPage = (props: IndexPageProps) => {
{updateClockIsLoading && <ButtonProgressIndicator />}
</MenuItem>
)}
<MenuItem onClick={onLeaveGame}>
<FormattedMessage
defaultMessage="Spiel verlassen"
description="appbar menu item leave game"
/>
</MenuItem>
</Menu>
</Toolbar>
</AppBar>
Expand Down
33 changes: 33 additions & 0 deletions app/src/common/hooks/useLeaveGame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useIntl } from "react-intl";
import { useLocation } from "wouter";
import { useSnackbar } from "notistack";
import useCharacter from "./useCharacter";
import useGameId from "./useGameId";

/**
* A React hook to leave the game
*
* @returns A function to call in order to leave the game
*/
const useLeaveGame = () => {
const [, , deleteCharacter] = useCharacter();
const [, , deleteGameId] = useGameId();
const [, setLocation] = useLocation();
const { enqueueSnackbar } = useSnackbar();
const intl = useIntl();

return () => {
deleteCharacter();
deleteGameId();
enqueueSnackbar(
intl.formatMessage({
defaultMessage: "Spiel erfolgreich verlassen!",
description: "success snackbar on leave game",
}),
{ variant: "success" }
);
setLocation("/");
};
};

export default useLeaveGame;

0 comments on commit 22b0eb5

Please sign in to comment.