Board state in redux

This commit is contained in:
Dane Johnson 2022-02-21 11:27:39 -06:00
parent 9ed9066247
commit c20e6358c6
5 changed files with 45 additions and 5 deletions

View File

@ -21,3 +21,8 @@
border-collapse: collapse; border-collapse: collapse;
text-align: center; text-align: center;
} }
.gameboard td {
color: yellow;
font-weight: bold;;
}

View File

@ -1,19 +1,34 @@
import React from 'react'; import React from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { useBoard } from '../hooks/board.js';
import './GameBoard.css'; import './GameBoard.css';
const makeCell = (isSelected, isDouble, i) => {
let s = '';
if (!isSelected) {
if (!isDouble) {
s = `$${200 * (i + 1)}`;
} else {
s = `$${400 * (i + 1)}`;
}
}
return <td>{s}</td>;
}
export default function GameBoard() { export default function GameBoard() {
const board = useBoard();
return ( return (
<div className="gameboard"> <div className="gameboard">
<div className="roomCode">Room Code: ???</div> <div className="roomCode">Room Code: ???</div>
<div className="board"> <div className="board">
<table> <table>
<tr> <tr>
{ _.range(6).map(i => <th>{`Category ${i + 1}`}</th>) } { board.categories.map(c => <th>{c}</th>) }
</tr> </tr>
{ _.range(5).map(r => <tr>{ { board.selected.map((r, i) => <tr> {
_.range(6).map(c => <td> {`$${200 * (r + 1)}`} </td>) r.map(selected => makeCell(selected, board.double, i))
}</tr>)} }</tr>)}
</table> </table>
</div> </div>

View File

@ -0,0 +1,5 @@
import { useSelector } from 'react-redux';
export function useBoard() {
return useSelector(state => state.board);
}

View File

@ -0,0 +1,15 @@
import { createSlice } from '@reduxjs/toolkit';
import _ from 'lodash';
export const boardSlice = createSlice({
name: 'board',
initialState: {
mode: 'table',
categories: _.range(1, 7).map(n => "Category " + n),
double: false,
selected: _.chunk(_.fill(Array(30), false), 6),
},
reducers: {} // TODO
});
export default boardSlice.reducer;

View File

@ -1,10 +1,10 @@
import { configureStore } from '@reduxjs/toolkit'; import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './reducers/counter.js';
import gamestateReducer from './reducers/gamestate.js'; import gamestateReducer from './reducers/gamestate.js';
import boardReducer from './reducers/board.js';
export default configureStore({ export default configureStore({
reducer: { reducer: {
counter: counterReducer,
gamestate: gamestateReducer, gamestate: gamestateReducer,
board: boardReducer,
} }
}); });