From 247505bb935a782bab6aa314983a4204a9682d5c Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 20 Mar 2020 14:48:46 +1100 Subject: [PATCH] Refactored modal, added popup for image selection and changed map data layout --- src/components/AddMapButton.js | 110 ++++++++++++++++++++++--- src/components/AddPartyMemberButton.js | 38 +-------- src/components/Modal.js | 35 ++++++++ src/routes/Game.js | 26 +++--- 4 files changed, 151 insertions(+), 58 deletions(-) create mode 100644 src/components/Modal.js diff --git a/src/components/AddMapButton.js b/src/components/AddMapButton.js index 8b43316..0623b6b 100644 --- a/src/components/AddMapButton.js +++ b/src/components/AddMapButton.js @@ -1,25 +1,47 @@ -import React, { useRef } from "react"; -import { IconButton } from "theme-ui"; +import React, { useRef, useState } from "react"; +import { IconButton, Box, Button, Image, Flex, Label, Input } from "theme-ui"; + +import Modal from "./Modal"; + +const defaultMapSize = 22; function AddMapButton({ handleMapChange }) { const fileInputRef = useRef(); - function handleIconClicked() { + function openImageDialog(e) { if (fileInputRef.current) { fileInputRef.current.click(); } } + const [isAddModalOpen, setIsAddModalOpen] = useState(false); + function openModal() { + setIsAddModalOpen(true); + } + function closeModal() { + setIsAddModalOpen(false); + } + + const mapDataRef = useRef(null); + const [mapSource, setImageSource] = useState(null); + function handleImageUpload(event) { + mapDataRef.current = { file: event.target.files[0], rows, columns }; + setImageSource(URL.createObjectURL(mapDataRef.current.file)); + } + + function handleDone() { + if (mapDataRef.current && mapSource) { + handleMapChange(mapDataRef.current, mapSource); + } + closeModal(); + } + + const [rows, setRows] = useState(defaultMapSize); + const [columns, setColumns] = useState(defaultMapSize); + return ( <> - - + + + { + e.preventDefault(); + handleDone(); + }} + > + + + + + + + + setRows(e.target.value)} + /> + + + + setColumns(e.target.value)} + /> + + + {mapSource ? ( + + ) : ( + + )} + + + ); } diff --git a/src/components/AddPartyMemberButton.js b/src/components/AddPartyMemberButton.js index d9b0f3f..e425dc9 100644 --- a/src/components/AddPartyMemberButton.js +++ b/src/components/AddPartyMemberButton.js @@ -1,23 +1,13 @@ import React, { useState } from "react"; -import Modal from "react-modal"; -import { - IconButton, - Flex, - Box, - Label, - Close, - useThemeUI, - Text -} from "theme-ui"; +import { IconButton, Flex, Box, Label, Text } from "theme-ui"; + +import Modal from "./Modal"; function AddPartyMemberButton({ streamId }) { const [isAddModalOpen, setIsAddModalOpen] = useState(false); - const { theme } = useThemeUI(); - function openModal() { setIsAddModalOpen(true); } - function closeModal() { setIsAddModalOpen(false); } @@ -38,32 +28,12 @@ function AddPartyMemberButton({ streamId }) { - + {streamId} - diff --git a/src/components/Modal.js b/src/components/Modal.js new file mode 100644 index 0000000..a86bd2e --- /dev/null +++ b/src/components/Modal.js @@ -0,0 +1,35 @@ +import React from "react"; +import Modal from "react-modal"; +import { useThemeUI, Close } from "theme-ui"; + +function StyledModal({ isOpen, onRequestClose, children }) { + const { theme } = useThemeUI(); + + return ( + + {children} + + + ); +} + +export default StyledModal; diff --git a/src/routes/Game.js b/src/routes/Game.js index 3afd2a2..effaf21 100644 --- a/src/routes/Game.js +++ b/src/routes/Game.js @@ -32,14 +32,14 @@ function Game() { } }, [gameId, peerId, connectTo, streams]); - const [imageSource, setImageSource] = useState(null); - const imageDataRef = useRef(null); + const [mapSource, setMapSource] = useState(null); + const mapDataRef = useRef(null); - function handleMapChange(event) { - imageDataRef.current = event.target.files[0]; - setImageSource(URL.createObjectURL(imageDataRef.current)); + function handleMapChange(mapData, mapSource) { + mapDataRef.current = mapData; + setMapSource(mapSource); for (let connection of Object.values(connections)) { - connection.send({ id: "image", data: imageDataRef.current }); + connection.send({ id: "map", data: mapDataRef.current }); } } @@ -69,10 +69,10 @@ function Game() { function handleConnectionOpen(connection) { connection.on("data", data => { - if (data.id === "image") { - const blob = new Blob([data.data]); - imageDataRef.current = blob; - setImageSource(URL.createObjectURL(imageDataRef.current)); + if (data.id === "map") { + const blob = new Blob([data.data.file]); + mapDataRef.current = { ...data.data, file: blob }; + setMapSource(URL.createObjectURL(mapDataRef.current.file)); } if (data.id === "tokenEdit") { setMapTokens(prevMapTokens => ({ @@ -89,8 +89,8 @@ function Game() { } function handleConnectionSync(connection) { - if (imageSource) { - connection.send({ id: "image", data: imageDataRef.current }); + if (mapSource) { + connection.send({ id: "map", data: mapDataRef.current }); } connection.send({ id: "tokenEdit", data: mapTokens }); } @@ -113,7 +113,7 @@ function Game() { >