reload maps every time the select map modal is opened
This commit is contained in:
parent
fbdb68596e
commit
a07b95ec2d
@ -1,8 +1,6 @@
|
|||||||
import React, { useState, useEffect } from "react";
|
import React, { useState } from "react";
|
||||||
import { Flex, Image as UIImage, IconButton, Box, Text } from "theme-ui";
|
import { Flex, Image as UIImage, IconButton, Box, Text } from "theme-ui";
|
||||||
|
|
||||||
import db from "../../database";
|
|
||||||
|
|
||||||
import RemoveMapIcon from "../../icons/RemoveMapIcon";
|
import RemoveMapIcon from "../../icons/RemoveMapIcon";
|
||||||
import ResetMapIcon from "../../icons/ResetMapIcon";
|
import ResetMapIcon from "../../icons/ResetMapIcon";
|
||||||
import ExpandMoreDotIcon from "../../icons/ExpandMoreDotIcon";
|
import ExpandMoreDotIcon from "../../icons/ExpandMoreDotIcon";
|
||||||
@ -12,6 +10,7 @@ import { mapSources as defaultMapSources } from "../../maps";
|
|||||||
|
|
||||||
function MapTile({
|
function MapTile({
|
||||||
map,
|
map,
|
||||||
|
mapState,
|
||||||
isSelected,
|
isSelected,
|
||||||
onMapSelect,
|
onMapSelect,
|
||||||
onMapRemove,
|
onMapRemove,
|
||||||
@ -20,21 +19,11 @@ function MapTile({
|
|||||||
}) {
|
}) {
|
||||||
const mapSource = useDataSource(map, defaultMapSources);
|
const mapSource = useDataSource(map, defaultMapSources);
|
||||||
const [isMapTileMenuOpen, setIsTileMenuOpen] = useState(false);
|
const [isMapTileMenuOpen, setIsTileMenuOpen] = useState(false);
|
||||||
const [hasMapState, setHasMapState] = useState(false);
|
|
||||||
const isDefault = map.type === "default";
|
const isDefault = map.type === "default";
|
||||||
|
const hasMapState =
|
||||||
useEffect(() => {
|
mapState &&
|
||||||
async function checkForMapState() {
|
(Object.values(mapState.tokens).length > 0 ||
|
||||||
const state = await db.table("states").get(map.id);
|
mapState.drawActions.length > 0);
|
||||||
if (
|
|
||||||
state &&
|
|
||||||
(Object.values(state.tokens).length > 0 || state.drawActions.length > 0)
|
|
||||||
) {
|
|
||||||
setHasMapState(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
checkForMapState();
|
|
||||||
}, [map]);
|
|
||||||
|
|
||||||
const expandButton = (
|
const expandButton = (
|
||||||
<IconButton
|
<IconButton
|
||||||
@ -81,7 +70,6 @@ function MapTile({
|
|||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
setHasMapState(false);
|
|
||||||
setIsTileMenuOpen(false);
|
setIsTileMenuOpen(false);
|
||||||
onMapReset(map.id);
|
onMapReset(map.id);
|
||||||
}}
|
}}
|
||||||
|
@ -9,6 +9,7 @@ import MapTile from "./MapTile";
|
|||||||
function MapTiles({
|
function MapTiles({
|
||||||
maps,
|
maps,
|
||||||
selectedMap,
|
selectedMap,
|
||||||
|
selectedMapState,
|
||||||
onMapSelect,
|
onMapSelect,
|
||||||
onMapAdd,
|
onMapAdd,
|
||||||
onMapRemove,
|
onMapRemove,
|
||||||
@ -56,7 +57,10 @@ function MapTiles({
|
|||||||
<MapTile
|
<MapTile
|
||||||
key={map.id}
|
key={map.id}
|
||||||
map={map}
|
map={map}
|
||||||
isSelected={map.id === selectedMap}
|
mapState={
|
||||||
|
selectedMap && map.id === selectedMap.id && selectedMapState
|
||||||
|
}
|
||||||
|
isSelected={selectedMap && map.id === selectedMap.id}
|
||||||
onMapSelect={onMapSelect}
|
onMapSelect={onMapSelect}
|
||||||
onMapRemove={onMapRemove}
|
onMapRemove={onMapRemove}
|
||||||
onMapReset={onMapReset}
|
onMapReset={onMapReset}
|
||||||
|
11
src/helpers/usePrevious.js
Normal file
11
src/helpers/usePrevious.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
function usePrevious(value) {
|
||||||
|
const ref = useRef();
|
||||||
|
useEffect(() => {
|
||||||
|
ref.current = value;
|
||||||
|
}, [value]);
|
||||||
|
return ref.current;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default usePrevious;
|
@ -10,6 +10,8 @@ import MapSettings from "../components/map/MapSettings";
|
|||||||
|
|
||||||
import AuthContext from "../contexts/AuthContext";
|
import AuthContext from "../contexts/AuthContext";
|
||||||
|
|
||||||
|
import usePrevious from "../helpers/usePrevious";
|
||||||
|
|
||||||
import { maps as defaultMaps } from "../maps";
|
import { maps as defaultMaps } from "../maps";
|
||||||
|
|
||||||
const defaultMapSize = 22;
|
const defaultMapSize = 22;
|
||||||
@ -40,6 +42,8 @@ function SelectMapModal({
|
|||||||
}) {
|
}) {
|
||||||
const { userId } = useContext(AuthContext);
|
const { userId } = useContext(AuthContext);
|
||||||
|
|
||||||
|
const wasOpen = usePrevious(isOpen);
|
||||||
|
|
||||||
const [imageLoading, setImageLoading] = useState(false);
|
const [imageLoading, setImageLoading] = useState(false);
|
||||||
|
|
||||||
// The map selected in the modal
|
// The map selected in the modal
|
||||||
@ -80,10 +84,18 @@ function SelectMapModal({
|
|||||||
(a, b) => a.timestamp - b.timestamp
|
(a, b) => a.timestamp - b.timestamp
|
||||||
);
|
);
|
||||||
setMaps(sortedMaps);
|
setMaps(sortedMaps);
|
||||||
|
if (selectedMap) {
|
||||||
|
const map = await db.table("maps").get(selectedMap.id);
|
||||||
|
const state = await db.table("states").get(selectedMap.id);
|
||||||
|
setSelectedMap(map);
|
||||||
|
setSelectedMapState(state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadMaps();
|
if (!wasOpen && isOpen) {
|
||||||
}, [userId]);
|
loadMaps();
|
||||||
|
}
|
||||||
|
}, [userId, isOpen, wasOpen, selectedMap]);
|
||||||
|
|
||||||
const fileInputRef = useRef();
|
const fileInputRef = useRef();
|
||||||
|
|
||||||
@ -169,9 +181,9 @@ function SelectMapModal({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleMapSelect(map) {
|
async function handleMapSelect(map) {
|
||||||
|
setSelectedMapState(await db.table("states").get(map.id));
|
||||||
setSelectedMap(map);
|
setSelectedMap(map);
|
||||||
db.table("states").get(map.id).then(setSelectedMapState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleMapReset(id) {
|
async function handleMapReset(id) {
|
||||||
@ -260,7 +272,8 @@ function SelectMapModal({
|
|||||||
maps={maps}
|
maps={maps}
|
||||||
onMapAdd={openImageDialog}
|
onMapAdd={openImageDialog}
|
||||||
onMapRemove={handleMapRemove}
|
onMapRemove={handleMapRemove}
|
||||||
selectedMap={selectedMap && selectedMap.id}
|
selectedMap={selectedMap}
|
||||||
|
selectedMapState={selectedMapState}
|
||||||
onMapSelect={handleMapSelect}
|
onMapSelect={handleMapSelect}
|
||||||
onMapReset={handleMapReset}
|
onMapReset={handleMapReset}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user