From a76823f2b56f97ce0f2fca7b91b03a963c64a36b Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 23 Jul 2022 14:05:36 -0500 Subject: [PATCH] to do: pass props back to parent from children --- src/components/Gameboard/Gameboard.tsx | 13 ++++-- src/components/Player/AllPlayers.tsx | 11 +++-- src/components/Player/Player.tsx | 34 +++++++------- src/components/Resources/AvailableChips.tsx | 25 +++++------ src/util/types.ts | 34 +++++++------- src/util/useActionStatus.tsx | 17 +++++++ src/util/useActionType.tsx | 50 +++++++++++++++++++++ 7 files changed, 132 insertions(+), 52 deletions(-) create mode 100644 src/util/useActionStatus.tsx create mode 100644 src/util/useActionType.tsx diff --git a/src/components/Gameboard/Gameboard.tsx b/src/components/Gameboard/Gameboard.tsx index d687614..50afc36 100644 --- a/src/components/Gameboard/Gameboard.tsx +++ b/src/components/Gameboard/Gameboard.tsx @@ -1,10 +1,11 @@ -import { useEffect, useState } from 'react'; -import { FullDeck, NobleData, StateProps } from '../../util/types'; +import { useCallback, useEffect, useState } from 'react'; +import { AppState, FullDeck, NobleData, StateProps } from '../../util/types'; import AllPlayers from '../Player/AllPlayers'; import AvailableChips from '../Resources/AvailableChips'; import CardRow from './CardRow'; import Nobles from './Nobles'; import NobleStore from '../../data/nobles.json'; +import useActionStatus from '../../util/useActionStatus'; export default function Gameboard({ state, setState }: StateProps) { const [view, setView] = useState(

Loading...

) @@ -31,8 +32,8 @@ export default function Gameboard({ state, setState }: StateProps) { - - + + ) } @@ -83,5 +84,9 @@ export default function Gameboard({ state, setState }: StateProps) { setNobles(); } + const liftFromChildren = useCallback((childState: AppState) => { + setState(childState); + }, [state]); + return view } \ No newline at end of file diff --git a/src/components/Player/AllPlayers.tsx b/src/components/Player/AllPlayers.tsx index cbbd7f4..a123b3e 100644 --- a/src/components/Player/AllPlayers.tsx +++ b/src/components/Player/AllPlayers.tsx @@ -1,23 +1,26 @@ import Player from "./Player"; import { v4 } from "uuid"; import { PlayerData, StateProps } from "../../util/types"; -import { Dispatch, SetStateAction } from "react"; +import { Dispatch, SetStateAction, useEffect } from "react"; import "./AllPlayers.css" interface AllPlayersProps extends StateProps { + liftFromChildren?: any, chipSelection: { selection: String[], setSelection: Dispatch>> } } -export default function AllPlayers({ state, setState, chipSelection }: AllPlayersProps) { - const {selection, setSelection} = chipSelection; +export default function AllPlayers({ state, setState, chipSelection, liftFromChildren }: AllPlayersProps) { + useEffect(() => { + console.log(state); + }, [state]) return (
{ - state.players?.map((player: PlayerData) => ) + state.players?.map((player: PlayerData) => ) }
) diff --git a/src/components/Player/Player.tsx b/src/components/Player/Player.tsx index a79b6cc..0946cb3 100644 --- a/src/components/Player/Player.tsx +++ b/src/components/Player/Player.tsx @@ -1,6 +1,7 @@ import { AppState, ActionPrompts, GameActions, PlayerData, ResourceCost, StateProps } from "../../util/types"; import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { TurnOrderUtil } from "../../util/TurnOrderUtil"; +import useActionType from "../../util/useActionType"; import { v4 } from "uuid"; interface PlayerProps extends StateProps { @@ -8,33 +9,35 @@ interface PlayerProps extends StateProps { chipSelection: { selection: String[], setSelection: Dispatch>> - } + }, + 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 [actionType, setActionType] = useState(); - const [dynamic, setDynamic] = useState(); + const [actionType, setActionType] = useState(GameActions.AWAIT); + const [dynamic, setDynamic] = useState(state.players.find((element: PlayerData) => element.id === player.id)); const { selection, setSelection } = chipSelection; - useEffect(() => { - setDynamic(state.players.find((element: PlayerData) => element.id === player.id)); - }, [state]); - useEffect(() => { return; }, [selection, setSelection]) useEffect(() => { + const newState = useActionType(state, actionType); + console.log(newState); + switch (actionType) { case GameActions.GETCHIPS: - console.log('get chips'); + setActionPrompt(ActionPrompts[1]); getChips(); setSelection([]); - setActionType(GameActions.AWAIT); break; - case GameActions.AWAIT: - console.log('waiting for next action'); + case GameActions.BUYCARD: + setActionPrompt(ActionPrompts[2]); + break; + case GameActions.RESERVECARD: + setActionPrompt(ActionPrompts[3]); break; default: break; @@ -70,6 +73,8 @@ export default function Player({ player, state, setState, chipSelection }: Playe }, } }) + + liftFromChildren(state); } return ( @@ -83,9 +88,8 @@ export default function Player({ player, state, setState, chipSelection }: Playe

{dynamic?.turnActive ? actionPrompt : "..."}

- - - + +
diff --git a/src/components/Resources/AvailableChips.tsx b/src/components/Resources/AvailableChips.tsx index ba5d4fb..5f4bc11 100644 --- a/src/components/Resources/AvailableChips.tsx +++ b/src/components/Resources/AvailableChips.tsx @@ -2,37 +2,36 @@ import { GameActions, ResourceCost, StateProps } from "../../util/types"; import { v4 } from "uuid"; import "./AvailableChips.css" import { Dispatch, SetStateAction, useEffect, useState } from "react"; +import useActionStatus from "../../util/useActionStatus"; interface AvailableChipsProps extends StateProps { + liftFromChildren: any, chipSelection: { selection: String[], setSelection: Dispatch>> } } -export default function AvailableChips({ state, chipSelection }: AvailableChipsProps) { +export default function AvailableChips({ state, chipSelection, liftFromChildren }: AvailableChipsProps) { const { selection, setSelection } = chipSelection; const handleSelection = (key: string) => { - let newValue = selection; + console.log(key) + console.log(state); - if (newValue.length > 3) return; - newValue.push(key); + if (selection.length > 3) return; - setSelection(newValue); + setSelection((prev) => { + let newValue = prev; + newValue.push(key); + return newValue; + }) } useEffect(() => { - return; + useActionStatus(state); }, [state]) - useEffect(() => { - switch (state.actions) { - case (GameActions.GETCHIPS): - console.log('get chips'); - } - }) - return (
diff --git a/src/util/types.ts b/src/util/types.ts index f374aae..52af9ad 100644 --- a/src/util/types.ts +++ b/src/util/types.ts @@ -13,22 +13,7 @@ export interface AppState { }, round: number, players: Array, - actions?: { - getChips?: { - active: boolean - chips?: Array - valid?: boolean - } - buyCard?: { - active: boolean - card?: CardData | null - } - reserveCard?: { - active: boolean - card?: CardData | null - includeGold?: boolean - } - } + actions: ActionTypes } export interface StateProps { @@ -43,6 +28,23 @@ export enum GameActions { AWAIT } +export interface ActionTypes { + getChips: { + active: boolean + chips?: Array + valid?: boolean + } + buyCard: { + active: boolean + card?: CardData | null + } + reserveCard: { + active: boolean + card?: CardData | null + includeGold?: boolean + } +} + export enum ActionPrompts { "Choose your action type below:", "Make a selection of three different available resources, or two of the same.", diff --git a/src/util/useActionStatus.tsx b/src/util/useActionStatus.tsx new file mode 100644 index 0000000..f632a2d --- /dev/null +++ b/src/util/useActionStatus.tsx @@ -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; + } +} \ No newline at end of file diff --git a/src/util/useActionType.tsx b/src/util/useActionType.tsx new file mode 100644 index 0000000..3b1d623 --- /dev/null +++ b/src/util/useActionType.tsx @@ -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 + } +} \ No newline at end of file