to do: pass props back to parent from children

This commit is contained in:
Mikayla Dobson
2022-07-23 14:05:36 -05:00
parent 3cfad260bf
commit a76823f2b5
7 changed files with 132 additions and 52 deletions

View File

@@ -1,10 +1,11 @@
import { useEffect, useState } from 'react'; import { useCallback, useEffect, useState } from 'react';
import { FullDeck, NobleData, StateProps } from '../../util/types'; import { AppState, FullDeck, NobleData, StateProps } from '../../util/types';
import AllPlayers from '../Player/AllPlayers'; import AllPlayers from '../Player/AllPlayers';
import AvailableChips from '../Resources/AvailableChips'; import AvailableChips from '../Resources/AvailableChips';
import CardRow from './CardRow'; import CardRow from './CardRow';
import Nobles from './Nobles'; import Nobles from './Nobles';
import NobleStore from '../../data/nobles.json'; import NobleStore from '../../data/nobles.json';
import useActionStatus from '../../util/useActionStatus';
export default function Gameboard({ state, setState }: StateProps) { export default function Gameboard({ state, setState }: StateProps) {
const [view, setView] = useState(<p>Loading...</p>) const [view, setView] = useState(<p>Loading...</p>)
@@ -31,8 +32,8 @@ export default function Gameboard({ state, setState }: StateProps) {
<CardRow tier={3} cards={state.gameboard.cardRows.tierThree} /> <CardRow tier={3} cards={state.gameboard.cardRows.tierThree} />
<CardRow tier={2} cards={state.gameboard.cardRows.tierTwo} /> <CardRow tier={2} cards={state.gameboard.cardRows.tierTwo} />
<CardRow tier={1} cards={state.gameboard.cardRows.tierOne} /> <CardRow tier={1} cards={state.gameboard.cardRows.tierOne} />
<AvailableChips chipSelection={chipSelection} state={state} setState={setState} /> <AvailableChips liftFromChildren={liftFromChildren} chipSelection={chipSelection} state={state} setState={setState} />
<AllPlayers chipSelection={chipSelection} state={state} setState={setState} /> <AllPlayers liftFromChildren={liftFromChildren} chipSelection={chipSelection} state={state} setState={setState} />
</div> </div>
) )
} }
@@ -83,5 +84,9 @@ export default function Gameboard({ state, setState }: StateProps) {
setNobles(); setNobles();
} }
const liftFromChildren = useCallback((childState: AppState) => {
setState(childState);
}, [state]);
return view return view
} }

View File

@@ -1,23 +1,26 @@
import Player from "./Player"; import Player from "./Player";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { PlayerData, StateProps } from "../../util/types"; import { PlayerData, StateProps } from "../../util/types";
import { Dispatch, SetStateAction } from "react"; import { Dispatch, SetStateAction, useEffect } from "react";
import "./AllPlayers.css" import "./AllPlayers.css"
interface AllPlayersProps extends StateProps { interface AllPlayersProps extends StateProps {
liftFromChildren?: any,
chipSelection: { chipSelection: {
selection: String[], selection: String[],
setSelection: Dispatch<SetStateAction<Array<String>>> setSelection: Dispatch<SetStateAction<Array<String>>>
} }
} }
export default function AllPlayers({ state, setState, chipSelection }: AllPlayersProps) { export default function AllPlayers({ state, setState, chipSelection, liftFromChildren }: AllPlayersProps) {
const {selection, setSelection} = chipSelection; useEffect(() => {
console.log(state);
}, [state])
return ( return (
<div className="all-players"> <div className="all-players">
{ {
state.players?.map((player: PlayerData) => <Player key={v4()} chipSelection={chipSelection} player={player} state={state} setState={setState} />) state.players?.map((player: PlayerData) => <Player key={v4()} liftFromChildren={liftFromChildren} chipSelection={chipSelection} player={player} state={state} setState={setState} />)
} }
</div> </div>
) )

View File

@@ -1,6 +1,7 @@
import { AppState, ActionPrompts, GameActions, PlayerData, ResourceCost, StateProps } from "../../util/types"; import { AppState, ActionPrompts, GameActions, PlayerData, ResourceCost, StateProps } from "../../util/types";
import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { TurnOrderUtil } from "../../util/TurnOrderUtil"; import { TurnOrderUtil } from "../../util/TurnOrderUtil";
import useActionType from "../../util/useActionType";
import { v4 } from "uuid"; import { v4 } from "uuid";
interface PlayerProps extends StateProps { interface PlayerProps extends StateProps {
@@ -8,33 +9,35 @@ interface PlayerProps extends StateProps {
chipSelection: { chipSelection: {
selection: String[], selection: String[],
setSelection: Dispatch<SetStateAction<Array<String>>> setSelection: Dispatch<SetStateAction<Array<String>>>
} },
liftFromChildren: any
} }
export default function Player({ player, state, setState, chipSelection }: PlayerProps) { export default function Player({ player, state, setState, chipSelection, liftFromChildren }: PlayerProps) {
const [actionPrompt, setActionPrompt] = useState(ActionPrompts[0]); const [actionPrompt, setActionPrompt] = useState(ActionPrompts[0]);
const [actionType, setActionType] = useState<GameActions>(); const [actionType, setActionType] = useState<GameActions>(GameActions.AWAIT);
const [dynamic, setDynamic] = useState<PlayerData>(); const [dynamic, setDynamic] = useState<PlayerData | undefined>(state.players.find((element: PlayerData) => element.id === player.id));
const { selection, setSelection } = chipSelection; const { selection, setSelection } = chipSelection;
useEffect(() => {
setDynamic(state.players.find((element: PlayerData) => element.id === player.id));
}, [state]);
useEffect(() => { useEffect(() => {
return; return;
}, [selection, setSelection]) }, [selection, setSelection])
useEffect(() => { useEffect(() => {
const newState = useActionType(state, actionType);
console.log(newState);
switch (actionType) { switch (actionType) {
case GameActions.GETCHIPS: case GameActions.GETCHIPS:
console.log('get chips'); setActionPrompt(ActionPrompts[1]);
getChips(); getChips();
setSelection([]); setSelection([]);
setActionType(GameActions.AWAIT);
break; break;
case GameActions.AWAIT: case GameActions.BUYCARD:
console.log('waiting for next action'); setActionPrompt(ActionPrompts[2]);
break;
case GameActions.RESERVECARD:
setActionPrompt(ActionPrompts[3]);
break; break;
default: default:
break; break;
@@ -70,6 +73,8 @@ export default function Player({ player, state, setState, chipSelection }: Playe
}, },
} }
}) })
liftFromChildren(state);
} }
return ( return (
@@ -83,9 +88,8 @@ export default function Player({ player, state, setState, chipSelection }: Playe
<p>{dynamic?.turnActive ? actionPrompt : "..."}</p> <p>{dynamic?.turnActive ? actionPrompt : "..."}</p>
<button onClick={() => setActionType(GameActions.GETCHIPS)}>Get Chips</button> <button onClick={() => setActionType(GameActions.GETCHIPS)}>Get Chips</button>
<button onClick={()=> setActionType(GameActions.BUYCARD)}>Buy a Card</button>
<button onClick={()=>{}}>Buy a Card</button> <button onClick={()=> setActionType(GameActions.RESERVECARD)}>Reserve a Card</button>
<button onClick={()=>{}}>Reserve a Card</button>
<div className="player-cards"></div> <div className="player-cards"></div>
<div className="player-resources"></div> <div className="player-resources"></div>
</div> </div>

View File

@@ -2,37 +2,36 @@ import { GameActions, ResourceCost, StateProps } from "../../util/types";
import { v4 } from "uuid"; import { v4 } from "uuid";
import "./AvailableChips.css" import "./AvailableChips.css"
import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { Dispatch, SetStateAction, useEffect, useState } from "react";
import useActionStatus from "../../util/useActionStatus";
interface AvailableChipsProps extends StateProps { interface AvailableChipsProps extends StateProps {
liftFromChildren: any,
chipSelection: { chipSelection: {
selection: String[], selection: String[],
setSelection: Dispatch<SetStateAction<Array<String>>> setSelection: Dispatch<SetStateAction<Array<String>>>
} }
} }
export default function AvailableChips({ state, chipSelection }: AvailableChipsProps) { export default function AvailableChips({ state, chipSelection, liftFromChildren }: AvailableChipsProps) {
const { selection, setSelection } = chipSelection; const { selection, setSelection } = chipSelection;
const handleSelection = (key: string) => { const handleSelection = (key: string) => {
let newValue = selection; console.log(key)
console.log(state);
if (newValue.length > 3) return; if (selection.length > 3) return;
newValue.push(key);
setSelection(newValue); setSelection((prev) => {
let newValue = prev;
newValue.push(key);
return newValue;
})
} }
useEffect(() => { useEffect(() => {
return; useActionStatus(state);
}, [state]) }, [state])
useEffect(() => {
switch (state.actions) {
case (GameActions.GETCHIPS):
console.log('get chips');
}
})
return ( return (
<div className="available-chips"> <div className="available-chips">

View File

@@ -13,22 +13,7 @@ export interface AppState {
}, },
round: number, round: number,
players: Array<PlayerData>, players: Array<PlayerData>,
actions?: { actions: ActionTypes
getChips?: {
active: boolean
chips?: Array<keyof ResourceCost>
valid?: boolean
}
buyCard?: {
active: boolean
card?: CardData | null
}
reserveCard?: {
active: boolean
card?: CardData | null
includeGold?: boolean
}
}
} }
export interface StateProps { export interface StateProps {
@@ -43,6 +28,23 @@ export enum GameActions {
AWAIT AWAIT
} }
export interface ActionTypes {
getChips: {
active: boolean
chips?: Array<keyof ResourceCost>
valid?: boolean
}
buyCard: {
active: boolean
card?: CardData | null
}
reserveCard: {
active: boolean
card?: CardData | null
includeGold?: boolean
}
}
export enum ActionPrompts { export enum ActionPrompts {
"Choose your action type below:", "Choose your action type below:",
"Make a selection of three different available resources, or two of the same.", "Make a selection of three different available resources, or two of the same.",

View File

@@ -0,0 +1,17 @@
import { AppState } from "./types";
export default function useActionStatus(state: AppState) {
switch (true) {
case (state.actions.getChips.active):
console.log('get chips active');
break;
case (state.actions.buyCard.active):
console.log('buy card active');
break;
case (state.actions.reserveCard.active):
console.log("reserve card active")
break;
default:
break;
}
}

View File

@@ -0,0 +1,50 @@
import { ActionTypes, AppState, GameActions } from "./types";
export default function useActionType(state: AppState, action: GameActions) {
let newActions: ActionTypes = {
getChips: { active: false },
buyCard: { active: false },
reserveCard: { active: false }
}
switch (action) {
case (GameActions.GETCHIPS):
newActions = {
...newActions,
getChips: {
active: true,
chips: [],
valid: false
}
}
break;
case (GameActions.BUYCARD):
newActions = {
...newActions,
buyCard: {
active: true,
card: null
}
}
break;
case (GameActions.RESERVECARD):
newActions = {
...newActions,
reserveCard: {
active: true,
card: null,
includeGold: false
}
}
break;
case (GameActions.AWAIT):
break;
default:
break;
}
return {
...state,
actions: newActions
}
}