From 3dfb1186f9d3a8dd3bad28f501a4a26873ccefdf Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Sun, 18 Jul 2021 09:13:10 +1000 Subject: [PATCH] Move nickname generation to db populate --- src/contexts/PlayerContext.tsx | 6 ------ src/database.ts | 3 +++ 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/contexts/PlayerContext.tsx b/src/contexts/PlayerContext.tsx index 0307585..ef19607 100644 --- a/src/contexts/PlayerContext.tsx +++ b/src/contexts/PlayerContext.tsx @@ -3,8 +3,6 @@ import React, { useEffect, useContext } from "react"; import { useDatabase } from "./DatabaseContext"; import { useUserId } from "./UserIdContext"; -import { getRandomMonster } from "../helpers/monsters"; - import useNetworkedState, { SetNetworkedState, } from "../hooks/useNetworkedState"; @@ -50,10 +48,6 @@ export function PlayerProvider({ session, children }: PlayerProviderProps) { ...prevState, nickname: storedNickname.value, })); - } else { - const name = getRandomMonster(); - setPlayerState((prevState) => ({ ...prevState, nickname: name })); - database?.table("user").add({ key: "nickname", value: name }); } } diff --git a/src/database.ts b/src/database.ts index 0c565da..0d1013f 100644 --- a/src/database.ts +++ b/src/database.ts @@ -5,6 +5,7 @@ import { v4 as uuid } from "uuid"; import { loadVersions, UpgradeEventHandler } from "./upgrade"; import { getDefaultMaps } from "./maps"; import { getDefaultTokens } from "./tokens"; +import { getRandomMonster } from "./helpers/monsters"; /** * Populate DB with initial data @@ -14,6 +15,8 @@ function populate(db: Dexie) { db.on("populate", () => { const userId = uuid(); db.table("user").add({ key: "userId", value: userId }); + const nickname = getRandomMonster(); + db.table("user").add({ key: "nickname", value: nickname }); const { maps, mapStates } = getDefaultMaps(userId); db.table("maps").bulkAdd(maps); db.table("states").bulkAdd(mapStates);