Fix selectMapButton name and a bug with deleting a map that isn't the current one

This commit is contained in:
Mitchell McCaffrey 2020-04-23 21:54:58 +10:00
parent 81da404eec
commit 5539fcf16a
5 changed files with 63 additions and 27 deletions

View File

@ -22,6 +22,7 @@ function Map({
onMapTokenChange, onMapTokenChange,
onMapTokenRemove, onMapTokenRemove,
onMapChange, onMapChange,
onMapStateChange,
onMapDraw, onMapDraw,
onMapDrawUndo, onMapDrawUndo,
onMapDrawRedo, onMapDrawRedo,
@ -296,6 +297,8 @@ function Map({
</Box> </Box>
<MapControls <MapControls
onMapChange={onMapChange} onMapChange={onMapChange}
onMapStateChange={onMapStateChange}
currentMap={map}
onToolChange={setSelectedTool} onToolChange={setSelectedTool}
selectedTool={selectedTool} selectedTool={selectedTool}
disabledTools={disabledTools} disabledTools={disabledTools}

View File

@ -1,7 +1,7 @@
import React, { useState, useEffect, useRef } from "react"; import React, { useState, useEffect, useRef } from "react";
import { Flex, Box, IconButton, Label } from "theme-ui"; import { Flex, Box, IconButton, Label } from "theme-ui";
import SelectMapButton from "./SelectMapIcon"; import SelectMapButton from "./SelectMapButton";
import ExpandMoreIcon from "../../icons/ExpandMoreIcon"; import ExpandMoreIcon from "../../icons/ExpandMoreIcon";
import PanToolIcon from "../../icons/PanToolIcon"; import PanToolIcon from "../../icons/PanToolIcon";
import BrushToolIcon from "../../icons/BrushToolIcon"; import BrushToolIcon from "../../icons/BrushToolIcon";
@ -22,6 +22,8 @@ import EraseAllIcon from "../../icons/EraseAllIcon";
function MapControls({ function MapControls({
onMapChange, onMapChange,
onMapStateChange,
currentMap,
onToolChange, onToolChange,
selectedTool, selectedTool,
disabledTools, disabledTools,
@ -233,7 +235,11 @@ function MapControls({
p={2} p={2}
ref={expanedMenuRef} ref={expanedMenuRef}
> >
<SelectMapButton onMapChange={onMapChange} /> <SelectMapButton
onMapChange={onMapChange}
onMapStateChange={onMapStateChange}
currentMap={currentMap}
/>
{divider} {divider}
<IconButton <IconButton
aria-label="Pan Tool" aria-label="Pan Tool"

View File

@ -4,7 +4,7 @@ import { IconButton } from "theme-ui";
import SelectMapModal from "../../modals/SelectMapModal"; import SelectMapModal from "../../modals/SelectMapModal";
import SelectMapIcon from "../../icons/SelectMapIcon"; import SelectMapIcon from "../../icons/SelectMapIcon";
function SelectMapButton({ onMapChange }) { function SelectMapButton({ onMapChange, onMapStateChange, currentMap }) {
const [isAddModalOpen, setIsAddModalOpen] = useState(false); const [isAddModalOpen, setIsAddModalOpen] = useState(false);
function openModal() { function openModal() {
setIsAddModalOpen(true); setIsAddModalOpen(true);
@ -13,10 +13,7 @@ function SelectMapButton({ onMapChange }) {
setIsAddModalOpen(false); setIsAddModalOpen(false);
} }
function handleDone(map, mapState) { function handleDone() {
if (map) {
onMapChange(map, mapState);
}
closeModal(); closeModal();
} }
@ -33,6 +30,9 @@ function SelectMapButton({ onMapChange }) {
isOpen={isAddModalOpen} isOpen={isAddModalOpen}
onRequestClose={closeModal} onRequestClose={closeModal}
onDone={handleDone} onDone={handleDone}
onMapChange={onMapChange}
onMapStateChange={onMapStateChange}
currentMap={currentMap}
/> />
</> </>
); );

View File

@ -18,10 +18,19 @@ const defaultMapState = {
drawActions: [], drawActions: [],
}; };
function SelectMapModal({ isOpen, onRequestClose, onDone }) { function SelectMapModal({
isOpen,
onRequestClose,
onDone,
onMapChange,
onMapStateChange,
// The map currently being view in the map screen
currentMap,
}) {
const [imageLoading, setImageLoading] = useState(false); const [imageLoading, setImageLoading] = useState(false);
const [currentMap, setCurrentMap] = useState(null); // The map selected in the modal
const [selectedMap, setSelectedMap] = useState(null);
const [maps, setMaps] = useState([]); const [maps, setMaps] = useState([]);
// Load maps from the database and ensure state is properly setup // Load maps from the database and ensure state is properly setup
useEffect(() => { useEffect(() => {
@ -121,48 +130,60 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
await db.table("maps").add(map); await db.table("maps").add(map);
await db.table("states").add({ ...defaultMapState, mapId: map.id }); await db.table("states").add({ ...defaultMapState, mapId: map.id });
setMaps((prevMaps) => [map, ...prevMaps]); setMaps((prevMaps) => [map, ...prevMaps]);
setCurrentMap(map); setSelectedMap(map);
setGridX(map.gridX); setGridX(map.gridX);
setGridY(map.gridY); setGridY(map.gridY);
} }
// Keep track of removed maps
async function handleMapRemove(id) { async function handleMapRemove(id) {
await db.table("maps").delete(id); await db.table("maps").delete(id);
await db.table("states").delete(id); await db.table("states").delete(id);
setMaps((prevMaps) => { setMaps((prevMaps) => {
const filtered = prevMaps.filter((map) => map.id !== id); const filtered = prevMaps.filter((map) => map.id !== id);
setCurrentMap(filtered[0]); setSelectedMap(filtered[0]);
return filtered; return filtered;
}); });
// Removed the map from the map screen if needed
if (currentMap.id === selectedMap.id) {
onMapChange(null);
}
} }
function handleMapSelect(map) { function handleMapSelect(map) {
setCurrentMap(map); setSelectedMap(map);
setGridX(map.gridX); setGridX(map.gridX);
setGridY(map.gridY); setGridY(map.gridY);
} }
async function handleMapReset(id) { async function handleMapReset(id) {
await db.table("states").put({ ...defaultMapState, mapId: id }); const state = { ...defaultMapState, mapId: id };
await db.table("states").put(state);
// Reset the state of the current map if needed
if (currentMap.id === selectedMap.id) {
onMapStateChange(state);
}
} }
async function handleSubmit(e) { async function handleSubmit(e) {
e.preventDefault(); e.preventDefault();
if (currentMap) { if (selectedMap) {
let currentMapState = let currentMapState =
(await db.table("states").get(currentMap.id)) || defaultMapState; (await db.table("states").get(selectedMap.id)) || defaultMapState;
onDone(currentMap, currentMapState); onMapStateChange(currentMapState);
onMapChange(selectedMap);
onDone();
} }
onDone(null, null); onDone();
} }
async function handleGridXChange(e) { async function handleGridXChange(e) {
const newX = e.target.value; const newX = e.target.value;
await db.table("maps").update(currentMap.id, { gridX: newX }); await db.table("maps").update(selectedMap.id, { gridX: newX });
setGridX(newX); setGridX(newX);
setMaps((prevMaps) => { setMaps((prevMaps) => {
const newMaps = [...prevMaps]; const newMaps = [...prevMaps];
const i = newMaps.findIndex((map) => map.id === currentMap.id); const i = newMaps.findIndex((map) => map.id === selectedMap.id);
if (i > -1) { if (i > -1) {
newMaps[i].gridX = newX; newMaps[i].gridX = newX;
} }
@ -172,11 +193,11 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
async function handleGridYChange(e) { async function handleGridYChange(e) {
const newY = e.target.value; const newY = e.target.value;
await db.table("maps").update(currentMap.id, { gridY: newY }); await db.table("maps").update(selectedMap.id, { gridY: newY });
setGridY(newY); setGridY(newY);
setMaps((prevMaps) => { setMaps((prevMaps) => {
const newMaps = [...prevMaps]; const newMaps = [...prevMaps];
const i = newMaps.findIndex((map) => map.id === currentMap.id); const i = newMaps.findIndex((map) => map.id === selectedMap.id);
if (i > -1) { if (i > -1) {
newMaps[i].gridY = newY; newMaps[i].gridY = newY;
} }
@ -232,7 +253,7 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
maps={maps} maps={maps}
onMapAdd={openImageDialog} onMapAdd={openImageDialog}
onMapRemove={handleMapRemove} onMapRemove={handleMapRemove}
selectedMap={currentMap && currentMap.id} selectedMap={selectedMap && selectedMap.id}
onMapSelect={handleMapSelect} onMapSelect={handleMapSelect}
onMapReset={handleMapReset} onMapReset={handleMapReset}
/> />
@ -244,7 +265,7 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
name="gridX" name="gridX"
value={gridX} value={gridX}
onChange={handleGridXChange} onChange={handleGridXChange}
disabled={currentMap === null || currentMap.default} disabled={selectedMap === null || selectedMap.default}
min={1} min={1}
/> />
</Box> </Box>
@ -255,7 +276,7 @@ function SelectMapModal({ isOpen, onRequestClose, onDone }) {
name="gridY" name="gridY"
value={gridY} value={gridY}
onChange={handleGridYChange} onChange={handleGridYChange}
disabled={currentMap === null || currentMap.default} disabled={selectedMap === null || selectedMap.default}
min={1} min={1}
/> />
</Box> </Box>

View File

@ -48,12 +48,17 @@ function Game() {
} }
}, [debouncedMapState]); }, [debouncedMapState]);
function handleMapChange(newMap, newMapState) { function handleMapChange(newMap) {
setMap(newMap); setMap(newMap);
for (let peer of Object.values(peers)) {
peer.connection.send({ id: "map", data: newMap });
}
}
function handleMapStateChange(newMapState) {
setMapState(newMapState); setMapState(newMapState);
for (let peer of Object.values(peers)) { for (let peer of Object.values(peers)) {
peer.connection.send({ id: "mapState", data: newMapState }); peer.connection.send({ id: "mapState", data: newMapState });
peer.connection.send({ id: "map", data: newMap });
} }
} }
@ -169,7 +174,7 @@ function Game() {
if (data.id === "map") { if (data.id === "map") {
// If we have a file convert it to a url // If we have a file convert it to a url
// TODO clear the file url of the previous map if it's a blob // TODO clear the file url of the previous map if it's a blob
if (data.data.file) { if (data.data && data.data.file) {
const file = new Blob([data.data.file]); const file = new Blob([data.data.file]);
const source = URL.createObjectURL(file); const source = URL.createObjectURL(file);
setMap({ ...data.data, file, source }); setMap({ ...data.data, file, source });
@ -323,6 +328,7 @@ function Game() {
onMapTokenChange={handleMapTokenChange} onMapTokenChange={handleMapTokenChange}
onMapTokenRemove={handleMapTokenRemove} onMapTokenRemove={handleMapTokenRemove}
onMapChange={handleMapChange} onMapChange={handleMapChange}
onMapStateChange={handleMapStateChange}
onMapDraw={handleMapDraw} onMapDraw={handleMapDraw}
onMapDrawUndo={handleMapDrawUndo} onMapDrawUndo={handleMapDrawUndo}
onMapDrawRedo={handleMapDrawRedo} onMapDrawRedo={handleMapDrawRedo}