Skip to content

Commit

Permalink
add loading state and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-james-watson committed Apr 25, 2021
1 parent 3b49bf7 commit 84fbe88
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 383 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
sourceType: "module",
},
plugins: ["react", "@typescript-eslint"],
rules: {},
rules: {
"@typescript-eslint/explicit-module-boundary-types": 0
},
}
121 changes: 121 additions & 0 deletions components/board.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import React from "react";
import { DragDropContext, DropResult } from "react-beautiful-dnd";
import { GameState } from "../types/game";
import useAutoMoveSensor from "../lib/useAutoMoveSensor";
import { checkCorrect, getRandomItem, preloadImage } from "../lib/items";
import NextItemList from "./next-item-list";
import PlayedItemList from "./played-item-list";
import styles from "../styles/board.module.scss";

interface Props {
state: GameState;
setState: (state: GameState) => void;
}

export default function Board(props: Props) {
const { state, setState } = props;

async function onDragEnd(result: DropResult) {
const { source, destination } = result;

if (
!destination ||
state.next === null ||
(source.droppableId === "next" && destination.droppableId === "next")
) {
return;
}

const item = { ...state.next };

if (source.droppableId === "next" && destination.droppableId === "played") {
const newDeck = [...state.deck];
const newPlayed = [...state.played];
const newNext = state.nextButOne;
const newNextButOne = getRandomItem(newDeck, newPlayed);
const newImageCache = [preloadImage(newNextButOne.image)];

const { correct, delta } = checkCorrect(
newPlayed,
item,
destination.index
);
newPlayed.splice(destination.index, 0, {
...state.next,
played: { correct },
});

setState({
...state,
deck: newDeck,
imageCache: newImageCache,
next: newNext,
nextButOne: newNextButOne,
played: newPlayed,
badlyPlaced: correct
? null
: {
index: destination.index,
rendered: false,
delta,
},
});
} else if (
source.droppableId === "played" &&
destination.droppableId === "played"
) {
const newPlayed = [...state.played];
const [item] = newPlayed.splice(source.index, 1);
newPlayed.splice(destination.index, 0, item);

setState({
...state,
played: newPlayed,
badlyPlaced: null,
});
}
}

// Ensure that newly placed items are rendered as draggables before trying to
// move them to the right place if needed.
React.useLayoutEffect(() => {
if (
state.badlyPlaced &&
state.badlyPlaced.index !== null &&
!state.badlyPlaced.rendered
) {
setState({
...state,
badlyPlaced: { ...state.badlyPlaced, rendered: true },
});
}
}, [setState, state]);

console.log(state);

return (
<DragDropContext
onDragEnd={onDragEnd}
onDragUpdate={() => {
setTimeout(() => {
// debugger;
}, 1000);
}}
sensors={[useAutoMoveSensor.bind(null, state)]}
>
<div className={styles.wrapper}>
<div className={styles.top}>
<NextItemList next={state.next} />
</div>
<div id="bottom" className={styles.bottom}>
<PlayedItemList
badlyPlacedIndex={
state.badlyPlaced === null ? null : state.badlyPlaced.index
}
items={state.played}
/>
</div>
</div>
</DragDropContext>
);
}
Loading

0 comments on commit 84fbe88

Please sign in to comment.