Added toasts to export/import success
This commit is contained in:
parent
b809cdf620
commit
b8d3c24e1d
@ -3,6 +3,7 @@ import { Box, Label, Text, Button, Flex } from "theme-ui";
|
|||||||
import { saveAs } from "file-saver";
|
import { saveAs } from "file-saver";
|
||||||
import * as Comlink from "comlink";
|
import * as Comlink from "comlink";
|
||||||
import shortid from "shortid";
|
import shortid from "shortid";
|
||||||
|
import { useToasts } from "react-toast-notifications";
|
||||||
|
|
||||||
import Modal from "../components/Modal";
|
import Modal from "../components/Modal";
|
||||||
import LoadingOverlay from "../components/LoadingOverlay";
|
import LoadingOverlay from "../components/LoadingOverlay";
|
||||||
@ -23,6 +24,7 @@ const importDBName = "OwlbearRodeoImportDB";
|
|||||||
|
|
||||||
function ImportExportModal({ isOpen, onRequestClose }) {
|
function ImportExportModal({ isOpen, onRequestClose }) {
|
||||||
const { userId } = useAuth();
|
const { userId } = useAuth();
|
||||||
|
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const [error, setError] = useState();
|
const [error, setError] = useState();
|
||||||
|
|
||||||
@ -32,6 +34,19 @@ function ImportExportModal({ isOpen, onRequestClose }) {
|
|||||||
const [showImportSelector, setShowImportSelector] = useState(false);
|
const [showImportSelector, setShowImportSelector] = useState(false);
|
||||||
const [showExportSelector, setShowExportSelector] = useState(false);
|
const [showExportSelector, setShowExportSelector] = useState(false);
|
||||||
|
|
||||||
|
const { addToast } = useToasts();
|
||||||
|
function addSuccessToast(message, maps, tokens) {
|
||||||
|
const mapText = `${maps.length} map${maps.length > 1 ? "s" : ""}`;
|
||||||
|
const tokenText = `${tokens.length} token${tokens.length > 1 ? "s" : ""}`;
|
||||||
|
if (maps.length > 0 && tokens.length > 0) {
|
||||||
|
addToast(`${message} ${mapText} and ${tokenText}`);
|
||||||
|
} else if (maps.length > 0) {
|
||||||
|
addToast(`${message} ${mapText}`);
|
||||||
|
} else if (tokens.length > 0) {
|
||||||
|
addToast(`${message} ${tokenText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function openFileDialog() {
|
function openFileDialog() {
|
||||||
if (fileInputRef.current) {
|
if (fileInputRef.current) {
|
||||||
fileInputRef.current.click();
|
fileInputRef.current.click();
|
||||||
@ -116,7 +131,7 @@ function ImportExportModal({ isOpen, onRequestClose }) {
|
|||||||
|
|
||||||
const importDB = getDatabase({}, importDBName);
|
const importDB = getDatabase({}, importDBName);
|
||||||
const db = getDatabase({});
|
const db = getDatabase({});
|
||||||
|
try {
|
||||||
// Keep track of a mapping of old token ids to new ones to apply them to the map states
|
// Keep track of a mapping of old token ids to new ones to apply them to the map states
|
||||||
let newTokenIds = {};
|
let newTokenIds = {};
|
||||||
if (checkedTokens.length > 0) {
|
if (checkedTokens.length > 0) {
|
||||||
@ -154,7 +169,11 @@ function ImportExportModal({ isOpen, onRequestClose }) {
|
|||||||
await db.table("maps").bulkAdd(newMaps);
|
await db.table("maps").bulkAdd(newMaps);
|
||||||
await db.table("states").bulkAdd(newStates);
|
await db.table("states").bulkAdd(newStates);
|
||||||
}
|
}
|
||||||
|
addSuccessToast("Imported", checkedMaps, checkedTokens);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
setError(new Error("Unable to import data"));
|
||||||
|
}
|
||||||
await importDB.delete();
|
await importDB.delete();
|
||||||
importDB.close();
|
importDB.close();
|
||||||
db.close();
|
db.close();
|
||||||
@ -195,6 +214,7 @@ function ImportExportModal({ isOpen, onRequestClose }) {
|
|||||||
tokenIds
|
tokenIds
|
||||||
);
|
);
|
||||||
saveAs(blob, `${shortid.generate()}.owlbear`);
|
saveAs(blob, `${shortid.generate()}.owlbear`);
|
||||||
|
addSuccessToast("Exported", checkedMaps, checkedTokens);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(e);
|
setError(e);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user