refacto : global refacto
This commit is contained in:
@@ -1,17 +1,26 @@
|
||||
import { Button } from "@mui/material";
|
||||
import { useState } from "react";
|
||||
import NewGameDialog from "./loadGameDialog";
|
||||
import { Chess } from "chess.js";
|
||||
|
||||
export default function LoadGameButton() {
|
||||
interface Props {
|
||||
setGame?: (game: Chess) => void;
|
||||
}
|
||||
|
||||
export default function LoadGameButton({ setGame }: Props) {
|
||||
const [openDialog, setOpenDialog] = useState(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button variant="contained" onClick={() => setOpenDialog(true)}>
|
||||
Add a game
|
||||
Add game
|
||||
</Button>
|
||||
|
||||
<NewGameDialog open={openDialog} onClose={() => setOpenDialog(false)} />
|
||||
<NewGameDialog
|
||||
open={openDialog}
|
||||
onClose={() => setOpenDialog(false)}
|
||||
setGame={setGame}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,14 +16,16 @@ import {
|
||||
TextField,
|
||||
Typography,
|
||||
} from "@mui/material";
|
||||
import { Chess } from "chess.js";
|
||||
import { useState } from "react";
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
setGame?: (game: Chess) => void;
|
||||
}
|
||||
|
||||
export default function NewGameDialog({ open, onClose }: Props) {
|
||||
export default function NewGameDialog({ open, onClose, setGame }: Props) {
|
||||
const [pgn, setPgn] = useState("");
|
||||
const [parsingError, setParsingError] = useState("");
|
||||
const { addGame } = useGameDatabase();
|
||||
@@ -34,7 +36,13 @@ export default function NewGameDialog({ open, onClose }: Props) {
|
||||
|
||||
try {
|
||||
const gameToAdd = getGameFromPgn(pgn);
|
||||
addGame(gameToAdd);
|
||||
|
||||
if (setGame) {
|
||||
setGame(gameToAdd);
|
||||
} else {
|
||||
addGame(gameToAdd);
|
||||
}
|
||||
|
||||
handleClose();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
Reference in New Issue
Block a user