Moved token request to DB and added sanity check to putToken

This commit is contained in:
Mitchell McCaffrey
2021-01-24 17:50:52 +11:00
parent ae32068ccb
commit 1c143f5464
2 changed files with 18 additions and 17 deletions

View File

@@ -7,7 +7,6 @@ import DatabaseContext from "./DatabaseContext";
import DatabaseWorker from "worker-loader!../workers/DatabaseWorker"; // eslint-disable-line import/no-webpack-loader-syntax
import { tokens as defaultTokens } from "../tokens";
import { logError } from "../helpers/logging";
const TokenDataContext = React.createContext();
@@ -106,16 +105,7 @@ export function TokenDataProvider({ children }) {
}
async function putToken(token) {
try {
await database.table("tokens").put(token);
} catch {
if (token?.file) {
const { file, ...rest } = token;
logError(`Unable to save token ${JSON.stringify(rest)}`);
} else {
logError(`Unable to save token ${JSON.stringify(token)}`);
}
}
await database.table("tokens").put(token);
setTokens((prevTokens) => {
const newTokens = [...prevTokens];
const i = newTokens.findIndex((t) => t.id === token.id);
@@ -157,6 +147,11 @@ export function TokenDataProvider({ children }) {
return tokens.find((token) => token.id === tokenId);
}
async function getTokenFromDB(tokenId) {
let token = await database.table("tokens").get(tokenId);
return token;
}
const ownedTokens = tokens.filter((token) => token.owner === userId);
const tokensById = tokens.reduce((obj, token) => {
@@ -176,6 +171,7 @@ export function TokenDataProvider({ children }) {
getToken,
tokensById,
tokensLoading,
getTokenFromDB,
};
return (