Added toasts to export/import success

This commit is contained in:
Mitchell McCaffrey 2021-02-16 17:32:41 +11:00
parent b809cdf620
commit b8d3c24e1d

View File

@ -3,6 +3,7 @@ import { Box, Label, Text, Button, Flex } from "theme-ui";
import { saveAs } from "file-saver";
import * as Comlink from "comlink";
import shortid from "shortid";
import { useToasts } from "react-toast-notifications";
import Modal from "../components/Modal";
import LoadingOverlay from "../components/LoadingOverlay";
@ -23,6 +24,7 @@ const importDBName = "OwlbearRodeoImportDB";
function ImportExportModal({ isOpen, onRequestClose }) {
const { userId } = useAuth();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState();
@ -32,6 +34,19 @@ function ImportExportModal({ isOpen, onRequestClose }) {
const [showImportSelector, setShowImportSelector] = 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() {
if (fileInputRef.current) {
fileInputRef.current.click();
@ -116,7 +131,7 @@ function ImportExportModal({ isOpen, onRequestClose }) {
const importDB = getDatabase({}, importDBName);
const db = getDatabase({});
try {
// Keep track of a mapping of old token ids to new ones to apply them to the map states
let newTokenIds = {};
if (checkedTokens.length > 0) {
@ -154,7 +169,11 @@ function ImportExportModal({ isOpen, onRequestClose }) {
await db.table("maps").bulkAdd(newMaps);
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();
importDB.close();
db.close();
@ -195,6 +214,7 @@ function ImportExportModal({ isOpen, onRequestClose }) {
tokenIds
);
saveAs(blob, `${shortid.generate()}.owlbear`);
addSuccessToast("Exported", checkedMaps, checkedTokens);
} catch (e) {
setError(e);
}