From 67e2484d25b3dcca3d24c30cf57b4b91bbd5b8c0 Mon Sep 17 00:00:00 2001 From: mayurkumarp Date: Wed, 13 Jul 2022 12:04:35 +0530 Subject: [PATCH 1/2] Fixed empty spells after cleaning database --- .../workspaces/contexts/ThothInterfaceProvider.tsx | 14 ++++++++++++-- packages/server/src/routes/spells/index.ts | 9 +-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx b/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx index 47d265db7..5be7f8ec8 100644 --- a/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx +++ b/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx @@ -12,18 +12,23 @@ import { usePubSub } from '../../contexts/PubSubProvider' import { useFetchFromImageCacheMutation } from '@/state/api/visualGenerationsApi' import { useGetSpellQuery, useRunSpellMutation } from '@/state/api/spells' import { useAuth } from '@/contexts/AuthProvider' +import { useDispatch } from 'react-redux' +import { closeTab } from '@/state/tabs' +import { useNavigate } from 'react-router' const Context = createContext(undefined!) export const useThothInterface = () => useContext(Context) const ThothInterfaceProvider = ({ children, tab }) => { + const dispatch = useDispatch() + const navigate = useNavigate() const { events, publish, subscribe } = usePubSub() const spellRef = useRef(null) const [fetchFromImageCache] = useFetchFromImageCacheMutation() const { user } = useAuth() const [_runSpell] = useRunSpellMutation() - const { data: _spell } = useGetSpellQuery( + const { data: _spell, isError } = useGetSpellQuery( { spellId: tab.spellId, userId: user?.id as string, @@ -34,9 +39,14 @@ const ThothInterfaceProvider = ({ children, tab }) => { ) useEffect(() => { + if (!_spell && isError) { + dispatch(closeTab(tab.id)) + navigate('/home') + return + } if (!_spell) return spellRef.current = _spell - }, [_spell]) + }, [_spell, isError]) const { $PLAYTEST_INPUT, diff --git a/packages/server/src/routes/spells/index.ts b/packages/server/src/routes/spells/index.ts index 6725e0c0b..842b69589 100644 --- a/packages/server/src/routes/spells/index.ts +++ b/packages/server/src/routes/spells/index.ts @@ -303,14 +303,7 @@ const getSpellHandler = async (ctx: Koa.Context) => { }) if (!spell) { - const newSpell = await creatorToolsDatabase.spells.create({ - userId: ctx.state.user?.id ?? ctx.query.userId, - name, - graph: { id: 'demo@0.1.0', nodes: {} }, - gameState: {}, - modules: [], - }) - ctx.body = newSpell + throw new Error('Spell not found') } else { let userId = ctx.state.user?.id ?? ctx.query.userId if (spell?.userId !== userId) throw new Error('spell not found') From d08037eef8c58c910a6dc2036199384a190d9e5f Mon Sep 17 00:00:00 2001 From: mayurkumarp Date: Fri, 15 Jul 2022 15:32:48 +0530 Subject: [PATCH 2/2] Renamed error variable --- .../src/workspaces/contexts/ThothInterfaceProvider.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx b/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx index 5be7f8ec8..5e6dc1c7a 100644 --- a/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx +++ b/packages/client/src/workspaces/contexts/ThothInterfaceProvider.tsx @@ -28,7 +28,7 @@ const ThothInterfaceProvider = ({ children, tab }) => { const [fetchFromImageCache] = useFetchFromImageCacheMutation() const { user } = useAuth() const [_runSpell] = useRunSpellMutation() - const { data: _spell, isError } = useGetSpellQuery( + const { data: _spell, isError: _isSpellError } = useGetSpellQuery( { spellId: tab.spellId, userId: user?.id as string, @@ -39,14 +39,16 @@ const ThothInterfaceProvider = ({ children, tab }) => { ) useEffect(() => { - if (!_spell && isError) { + console.log(); + + if (!_spell && _isSpellError) { dispatch(closeTab(tab.id)) navigate('/home') return } if (!_spell) return spellRef.current = _spell - }, [_spell, isError]) + }, [_spell, _isSpellError]) const { $PLAYTEST_INPUT,