Added setting change cache to token settings modal

This commit is contained in:
Mitchell McCaffrey 2020-06-28 15:43:45 +10:00
parent 7a3213a0f9
commit c6a9455e8f

View File

@ -11,6 +11,7 @@ import blobToBuffer from "../helpers/blobToBuffer";
import TokenDataContext from "../contexts/TokenDataContext"; import TokenDataContext from "../contexts/TokenDataContext";
import AuthContext from "../contexts/AuthContext"; import AuthContext from "../contexts/AuthContext";
import { isEmpty } from "../helpers/shared";
function SelectTokensModal({ isOpen, onRequestClose }) { function SelectTokensModal({ isOpen, onRequestClose }) {
const { userId } = useContext(AuthContext); const { userId } = useContext(AuthContext);
@ -34,6 +35,7 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
function handleTokenAdd(token) { function handleTokenAdd(token) {
addToken(token); addToken(token);
setSelectedTokenId(token.id);
} }
async function handleImagesUpload(files) { async function handleImagesUpload(files) {
@ -69,8 +71,8 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
handleTokenAdd({ handleTokenAdd({
file: buffer, file: buffer,
name, name,
type: "file",
id: shortid.generate(), id: shortid.generate(),
type: "file",
created: Date.now(), created: Date.now(),
lastModified: Date.now(), lastModified: Date.now(),
owner: userId, owner: userId,
@ -86,7 +88,8 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
}); });
} }
function handleTokenSelect(token) { async function handleTokenSelect(token) {
await applyTokenChanges();
setSelectedTokenId(token.id); setSelectedTokenId(token.id);
} }
@ -100,12 +103,28 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
*/ */
const [showMoreSettings, setShowMoreSettings] = useState(false); const [showMoreSettings, setShowMoreSettings] = useState(false);
async function handleTokenSettingsChange(key, value) { const [tokenSettingChanges, setTokenSettingChanges] = useState({});
await updateToken(selectedTokenId, { [key]: value });
function handleTokenSettingsChange(key, value) {
setTokenSettingChanges((prevChanges) => ({ ...prevChanges, [key]: value }));
} }
async function applyTokenChanges() {
if (selectedTokenId && !isEmpty(tokenSettingChanges)) {
await updateToken(selectedTokenId, tokenSettingChanges);
setTokenSettingChanges({});
}
}
async function handleRequestClose() {
await applyTokenChanges();
onRequestClose();
}
const selectedTokenWithChanges = { ...selectedToken, ...tokenSettingChanges };
return ( return (
<Modal isOpen={isOpen} onRequestClose={onRequestClose}> <Modal isOpen={isOpen} onRequestClose={handleRequestClose}>
<ImageDrop onDrop={handleImagesUpload} dropText="Drop token to upload"> <ImageDrop onDrop={handleImagesUpload} dropText="Drop token to upload">
<input <input
onChange={(event) => handleImagesUpload(event.target.files)} onChange={(event) => handleImagesUpload(event.target.files)}
@ -126,12 +145,12 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
<TokenTiles <TokenTiles
tokens={ownedTokens} tokens={ownedTokens}
onTokenAdd={openImageDialog} onTokenAdd={openImageDialog}
selectedToken={selectedToken} selectedToken={selectedTokenWithChanges}
onTokenSelect={handleTokenSelect} onTokenSelect={handleTokenSelect}
onTokenRemove={handleTokenRemove} onTokenRemove={handleTokenRemove}
/> />
<TokenSettings <TokenSettings
token={selectedToken} token={selectedTokenWithChanges}
showMore={showMoreSettings} showMore={showMoreSettings}
onSettingsChange={handleTokenSettingsChange} onSettingsChange={handleTokenSettingsChange}
onShowMoreChange={setShowMoreSettings} onShowMoreChange={setShowMoreSettings}
@ -139,7 +158,7 @@ function SelectTokensModal({ isOpen, onRequestClose }) {
<Button <Button
variant="primary" variant="primary"
disabled={imageLoading} disabled={imageLoading}
onClick={onRequestClose} onClick={handleRequestClose}
> >
Done Done
</Button> </Button>