Adding services for backend communication

This commit is contained in:
2020-11-07 15:54:56 -03:00
parent c724c2c0cf
commit cd73c39b23
8 changed files with 47 additions and 9 deletions

1
.env
View File

@@ -1 +1,2 @@
SKIP_PREFLIGHT_CHECK=true SKIP_PREFLIGHT_CHECK=true
REACT_APP_API_URL=http://127.0.0.1:8000

View File

@@ -32,7 +32,7 @@
2, 2,
2 2
], ],
"react/jsx-max-props-per-line": 1, "react/jsx-max-props-per-line": 0,
"react/jsx-no-duplicate-props": 1, "react/jsx-no-duplicate-props": 1,
"react/jsx-no-literals": 0, "react/jsx-no-literals": 0,
"react/jsx-no-undef": 1, "react/jsx-no-undef": 1,

View File

@@ -1,6 +1,9 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Board, Header } from 'components'; import { Board, Header } from 'components';
import { GameService } from 'services';
const gameClient = new GameService();
function App() { function App() {
const [rows, setRows] = useState(10); const [rows, setRows] = useState(10);
@@ -85,6 +88,12 @@ function App() {
} }
}; };
const handleCreateNewGame = event => {
gameClient
.createNewGame(rows, cols, mines)
.then(response => console.log(response));
};
if (!mounted) { if (!mounted) {
return null; return null;
} }
@@ -95,6 +104,7 @@ function App() {
<Header <Header
cols={cols} cols={cols}
flags={flags} flags={flags}
handleCreateNewGame={handleCreateNewGame}
handleSetMines={handleSetMines} handleSetMines={handleSetMines}
mines={mines} mines={mines}
rows={rows} rows={rows}

View File

@@ -12,11 +12,11 @@ const Board = ({ dummyBoard, handleCellClick, handleCellContextMenu }) => {
<div <div
className="board-row-cell box" className="board-row-cell box"
id={`cell_${rowId}_${colId}`} id={`cell_${rowId}_${colId}`}
key={`cell_${rowId}_${colId}`}
onClick={() => handleCellClick(rowId, colId)} onClick={() => handleCellClick(rowId, colId)}
onContextMenu={event => onContextMenu={event =>
handleCellContextMenu(event, rowId, colId) handleCellContextMenu(event, rowId, colId)
} }>
key={`cell_${rowId}_${colId}`}>
{' '} {' '}
</div> </div>
))} ))}
@@ -30,8 +30,8 @@ Board.propTypes = {
cols: PropTypes.number.isRequired, cols: PropTypes.number.isRequired,
dummyBoard: PropTypes.array.isRequired, dummyBoard: PropTypes.array.isRequired,
handleCellClick: PropTypes.func.isRequired, handleCellClick: PropTypes.func.isRequired,
rows: PropTypes.number.isRequired, handleCellContextMenu: PropTypes.func.isRequired,
handleCellContextMenu: PropTypes.func.isRequired rows: PropTypes.number.isRequired
}; };
export default Board; export default Board;

View File

@@ -6,6 +6,7 @@ import './styles.scss';
const Header = ({ const Header = ({
cols, cols,
flags, flags,
handleCreateNewGame,
handleSetMines, handleSetMines,
mines, mines,
rows, rows,
@@ -37,16 +38,14 @@ const Header = ({
/> />
</div> </div>
</div> </div>
<div <div
className="reset box" className="reset box"
onMouseDown={event => { onMouseDown={event => {
event.target.innerHTML = '😮'; event.target.innerHTML = '😮';
console.log('onmousedown');
}} }}
onMouseUp={event => { onMouseUp={event => {
event.target.innerHTML = '🙂'; event.target.innerHTML = '🙂';
console.log('onMouseUp'); handleCreateNewGame(event);
}}> }}>
🙂 🙂
</div> </div>
@@ -58,6 +57,7 @@ const Header = ({
Header.propTypes = { Header.propTypes = {
cols: PropTypes.number, cols: PropTypes.number,
flags: PropTypes.number, flags: PropTypes.number,
handleCreateNewGame: PropTypes.func.isRequired,
handleSetMines: PropTypes.func.isRequired, handleSetMines: PropTypes.func.isRequired,
mines: PropTypes.number, mines: PropTypes.number,
rows: PropTypes.number, rows: PropTypes.number,

14
src/services/base.js Normal file
View File

@@ -0,0 +1,14 @@
import axios from 'axios';
export const getResponseData = response => response.data;
export class RestClient {
constructor() {
this.client = axios.create({
baseURL: process.env.REACT_APP_API_URL,
headers: {
'Content-Type': 'application/json'
}
});
}
}

11
src/services/game.js Normal file
View File

@@ -0,0 +1,11 @@
import { RestClient, getResponseData } from './base';
export class GameService extends RestClient {
base = '/';
createNewGame(rows, cols, mines) {
return this.client
.post(`${this.base}games`, { rows, cols, mines })
.then(getResponseData);
}
}

2
src/services/index.js Normal file
View File

@@ -0,0 +1,2 @@
export { GameService } from './game';
export { RestClient } from './base';