From 97c52f3cc9051a746b731ca3f5909abde1a5ddc4 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:03:14 -0700 Subject: [PATCH 01/59] Add auth header to spell service. Use live get spell endpoint. --- client/src/state/spells.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index e6981562c..b44767140 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -2,6 +2,7 @@ import { createSelector } from '@reduxjs/toolkit' import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' import { Spell as SpellType } from '@latitudegames/thoth-core/types' +import { getAuthHeader } from '../utils/authHelper' import { initDB } from '../database' function camelize(str) { @@ -45,18 +46,25 @@ const versions: Record = {} export const spellApi = createApi({ reducerPath: 'spellApi', baseQuery: fetchBaseQuery({ - baseUrl: process.env.REACT_APP_API_URL || 'localhost:8000/', + baseUrl: `${process.env.REACT_APP_API_URL}/game` || 'localhost:8000/game', + prepareHeaders: headers => { + const authHeader = getAuthHeader() + if (authHeader?.Authorization) + headers.set('authorization', authHeader['Authorization']) + return headers + }, }), tagTypes: ['Spell', 'Version'], endpoints: builder => ({ getSpells: builder.query({ providesTags: ['Spell'], - async queryFn() { - const spellModel = await _spellModel() - const spells = await spellModel.getSpells() + query: () => '/spells', + // async queryFn() { + // const spellModel = await _spellModel() + // const spells = await spellModel.getSpells() - return { data: spells.map(spell => spell.toJSON()) } - }, + // return { data: spells.map(spell => spell.toJSON()) } + // }, }), getSpell: builder.query({ providesTags: ['Spell'], From 855c35fdedb041162b9326091e404eebe800c81f Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:18:43 -0700 Subject: [PATCH 02/59] Connect new spell to save spell endpoint --- client/src/state/spells.ts | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index b44767140..59b4ffe44 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -59,12 +59,6 @@ export const spellApi = createApi({ getSpells: builder.query({ providesTags: ['Spell'], query: () => '/spells', - // async queryFn() { - // const spellModel = await _spellModel() - // const spells = await spellModel.getSpells() - - // return { data: spells.map(spell => spell.toJSON()) } - // }, }), getSpell: builder.query({ providesTags: ['Spell'], @@ -85,13 +79,16 @@ export const spellApi = createApi({ }), newSpell: builder.mutation>({ invalidatesTags: ['Spell'], - async queryFn(spellData) { - const newSpell = { gameState: {}, ...spellData } - const spellModel = await _spellModel() - - const spell = await spellModel.newSpell(newSpell) - - return { data: spell.toJSON() } + query: spellData => { + const spell = { + ...spellData, + gameState: {}, + } + return { + url: '/spells/save', + method: 'POST', + body: spell, + } }, }), deploySpell: builder.mutation({ From 15fd37eee3d8fa853a5e0208a65409d1b9de4033 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:43:06 -0700 Subject: [PATCH 03/59] Change access route to graph in editor provider --- client/src/contexts/EditorProvider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/contexts/EditorProvider.tsx b/client/src/contexts/EditorProvider.tsx index d842862bb..391ae0cf8 100644 --- a/client/src/contexts/EditorProvider.tsx +++ b/client/src/contexts/EditorProvider.tsx @@ -86,7 +86,7 @@ const EditorProvider = ({ children }) => { // set editor to the map setEditor(newEditor) - if (tab.type === 'spell') newEditor.loadGraph(spell.graph) + if (tab.type === 'spell') newEditor.loadGraph(spell.chain.graph) if (tab.type === 'module') { const moduleDoc = await thoth.getModule(tab.module) From da0e3e8dd6594e2f37a9fa4f375e842df4543be5 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:43:47 -0700 Subject: [PATCH 04/59] Change route to access graph in module provider --- client/src/contexts/ModuleProvider.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/contexts/ModuleProvider.tsx b/client/src/contexts/ModuleProvider.tsx index 37bfcde4e..af1095d31 100644 --- a/client/src/contexts/ModuleProvider.tsx +++ b/client/src/contexts/ModuleProvider.tsx @@ -77,7 +77,7 @@ const ModuleProvider = ({ children }) => { const getSpellModules = async spell => { // should actually look for spells that have a data.module key set to a string - const moduleNames = Object.values(spell.graph.nodes) + const moduleNames = Object.values(spell.chain.graph.nodes) .filter((n: any) => n.name === 'Module') .map((n: any) => n.data.name) From 4ea159daa2d425f327c0e38b2753a0365b20157c Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:44:08 -0700 Subject: [PATCH 05/59] Use default name in create new to create tab --- client/src/features/StartScreen/components/CreateNew.jsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/client/src/features/StartScreen/components/CreateNew.jsx b/client/src/features/StartScreen/components/CreateNew.jsx index 87c7a60aa..d29f63e80 100644 --- a/client/src/features/StartScreen/components/CreateNew.jsx +++ b/client/src/features/StartScreen/components/CreateNew.jsx @@ -48,7 +48,11 @@ const CreateNew = () => { }) await clearTabs() - await openTab({ name: spell.name, spellId: spell.name, type: 'spell' }) + await openTab({ + name: placeholderName, + spellId: placeholderName, + type: 'spell', + }) setLocation('/thoth') } From 4b7260f4781f99b03d3dac3f4b026fa778ff0a3c Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:44:22 -0700 Subject: [PATCH 06/59] Check for trab spell as well before loading --- client/src/features/Thoth/components/Workspace.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/features/Thoth/components/Workspace.jsx b/client/src/features/Thoth/components/Workspace.jsx index 34adaa7c4..05a56fcbc 100644 --- a/client/src/features/Thoth/components/Workspace.jsx +++ b/client/src/features/Thoth/components/Workspace.jsx @@ -39,7 +39,7 @@ const Workspace = ({ tab, appPubSub }) => { }, [editor]) useEffect(() => { - if (!tab) return + if (!tab || !tab.spell) return loadSpell(tab.spell) }, [tab]) From baf35bbdca1700628aa8db6f0841b5579a0919d7 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:44:49 -0700 Subject: [PATCH 07/59] Switch get spell to use proper endpoint --- client/src/state/spells.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index 59b4ffe44..e807e5e7d 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -62,11 +62,10 @@ export const spellApi = createApi({ }), getSpell: builder.query({ providesTags: ['Spell'], - async queryFn(spellId) { - const spellModel = await _spellModel() - const spell = await spellModel.getSpell(spellId) - - return { data: spell.toJSON() } + query: spellId => { + return { + url: `spells/${spellId}`, + } }, }), saveSpell: builder.mutation, Partial>({ From 71efbc6c01c8ebed4510c8606d6f47178c1d4daf Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 12:45:56 -0700 Subject: [PATCH 08/59] Add implementation note to save spell service reducer --- client/src/state/spells.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index e807e5e7d..8738084a6 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -72,6 +72,7 @@ export const spellApi = createApi({ invalidatesTags: ['Spell'], async queryFn(spell) { const spellModel = await _spellModel() + // get modules here and add the, serialize spell into json blob, and send to sendpoint. const updatedSpell = await spellModel.saveSpell(spell.name, spell) return { data: updatedSpell.toJSON() } }, From c72abd070aab77d72edfd5cd3b1caa43bb818b25 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 17:56:14 -0700 Subject: [PATCH 09/59] Add get spell modules helper function to module model --- client/src/database/models/moduleModel.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/client/src/database/models/moduleModel.ts b/client/src/database/models/moduleModel.ts index 1db3ba8a9..a0e51d1d3 100644 --- a/client/src/database/models/moduleModel.ts +++ b/client/src/database/models/moduleModel.ts @@ -68,6 +68,21 @@ const loadModuleModel = db => { return await db.modules.insert(newModule) } + const getSpellModules = async spell => { + // should actually look for spells that have a data.module key set to a string + const moduleNames = Object.values(spell.chain.graph.nodes) + .filter((n: any) => n.name === 'Module') + .map((n: any) => n.data.name) + + const moduleDocs = await Promise.all( + moduleNames.map(moduleName => getModule(moduleName)) + ) + + // todo need tobe recursive probably. Or we add the modules used to the spell when created? + + return moduleDocs.map(module => module.toJSON()) + } + return { insert, getModules, @@ -76,6 +91,7 @@ const loadModuleModel = db => { updateModule, findOneModule, updateOrCreate, + getSpellModules, } } export default loadModuleModel From b8e98109d07a4d0a81856cc6be7218a6cca2a733 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 17:57:08 -0700 Subject: [PATCH 10/59] Get save spell working utilizing queryFn --- client/src/state/spells.ts | 45 ++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index 8738084a6..760f54bef 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -1,9 +1,14 @@ import { createSelector } from '@reduxjs/toolkit' -import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' +import { + createApi, + fetchBaseQuery, + FetchBaseQueryError, +} from '@reduxjs/toolkit/query/react' import { Spell as SpellType } from '@latitudegames/thoth-core/types' import { getAuthHeader } from '../utils/authHelper' import { initDB } from '../database' +import { QueryReturnValue } from '@reduxjs/toolkit/dist/query/baseQueryTypes' function camelize(str) { return str @@ -13,16 +18,23 @@ function camelize(str) { .replace(/\s+/g, '') } -const _spellModel = async () => { +// const _spellModel = async () => { +// const db = await initDB() +// const { spells } = db.models +// return spells +// } + +const _moduleModel = async () => { const db = await initDB() - const { spells } = db.models - return spells + const { modules } = db.models + return modules } export interface Spell { id?: string user?: Record | null | undefined name: string graph: SpellType + modules: SpellType[] gameState: Record createdAt?: number updatedAt?: number @@ -70,11 +82,26 @@ export const spellApi = createApi({ }), saveSpell: builder.mutation, Partial>({ invalidatesTags: ['Spell'], - async queryFn(spell) { - const spellModel = await _spellModel() - // get modules here and add the, serialize spell into json blob, and send to sendpoint. - const updatedSpell = await spellModel.saveSpell(spell.name, spell) - return { data: updatedSpell.toJSON() } + // needed to use queryFn as query option didnt seem to allow async functions. + async queryFn(spell, api, extraOptions, baseQuery) { + const moduleModel = await _moduleModel() + const modules = await moduleModel.getSpellModules(spell) + + spell.modules = modules + + const baseQueryOptions = { + url: 'spells/save', + body: spell, + method: 'POST', + } + + // cast into proper response shape expected by queryFn return + // probbably a way to directly pass in type args to baseQuery but couldnt find. + return baseQuery(baseQueryOptions) as QueryReturnValue< + Partial, + FetchBaseQueryError, + unknown + > }, }), newSpell: builder.mutation>({ From d46067d36803eeb992132aa24887440b597d9c5a Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 18:14:11 -0700 Subject: [PATCH 11/59] hook up deploy spell and get deployments --- client/src/state/spells.ts | 45 ++++++++++++-------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/client/src/state/spells.ts b/client/src/state/spells.ts index 760f54bef..bdd10ece9 100644 --- a/client/src/state/spells.ts +++ b/client/src/state/spells.ts @@ -10,13 +10,13 @@ import { getAuthHeader } from '../utils/authHelper' import { initDB } from '../database' import { QueryReturnValue } from '@reduxjs/toolkit/dist/query/baseQueryTypes' -function camelize(str) { - return str - .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { - return index === 0 ? word.toLowerCase() : word.toUpperCase() - }) - .replace(/\s+/g, '') -} +// function camelize(str) { +// return str +// .replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) { +// return index === 0 ? word.toLowerCase() : word.toUpperCase() +// }) +// .replace(/\s+/g, '') +// } // const _spellModel = async () => { // const db = await initDB() @@ -52,9 +52,6 @@ export interface DeployArgs { message: string } -// stubbed temp data -const versions: Record = {} - export const spellApi = createApi({ reducerPath: 'spellApi', baseQuery: fetchBaseQuery({ @@ -120,33 +117,19 @@ export const spellApi = createApi({ }), deploySpell: builder.mutation({ invalidatesTags: ['Version'], - async queryFn({ spellId, message }) { - if (!versions[spellId]) versions[spellId] = [] - - const _versions = versions[spellId] - const version = '0.0.' + (_versions.length + 1) - const url = `${process.env.REACT_APP_API_URL}/spells/${camelize( - spellId - )}/${version}` - const deployment = { version, message, spellId, url } - - versions[spellId].push(deployment) - + query({ spellId, message }) { return { - data: deployment as DeployedSpellVersion, + url: `/spells/${spellId}/deploy`, + body: { + message, + }, + method: 'POST', } }, }), getDeployments: builder.query({ providesTags: ['Version'], - async queryFn(spellId) { - console.log('egtting versions!') - const result = versions[spellId] || [] - console.log('results', result) - return { - data: result.reverse(), - } - }, + query: spellId => ({ url: `/spells/deployed/${spellId}` }), }), }), }) From a8f81feabb764cbcbe095be1bd58c28257ea3689 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 18:14:29 -0700 Subject: [PATCH 12/59] Fix fetching empty deployments bug --- .../features/Thoth/components/EditorWindow/Deployment.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx index 1ac68143a..9d0ce00ac 100644 --- a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx +++ b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx @@ -22,9 +22,10 @@ const DeploymentView = ({ open, setOpen, spellId }) => { const [deploySpell] = useDeploySpellMutation() const spell = useSelector(state => selectSpellById(state, spellId)) - const { data: deployments, isLoading } = useGetDeploymentsQuery( - spell?.name || '' - ) + const name = spell?.name as string + const { data: deployments, isLoading } = useGetDeploymentsQuery(name, { + skip: !spell?.name, + }) const deploy = message => { if (!spell) return From faffb1076459841144912b7d36da388d77a98754 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 18:19:17 -0700 Subject: [PATCH 13/59] Add url to all versions --- .../Thoth/components/EditorWindow/Deployment.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx index 9d0ce00ac..7e6a09541 100644 --- a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx +++ b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx @@ -33,6 +33,12 @@ const DeploymentView = ({ open, setOpen, spellId }) => { enqueueSnackbar('Spell deployed', { variant: 'success' }) } + const buildUrl = version => { + return encodeURI( + `${process.env.REACT_APP_API_URL}/games/spells/${spellId}/${version}` + ) + } + const copy = url => { const el = document.createElement('textarea') el.value = url @@ -128,10 +134,12 @@ const DeploymentView = ({ open, setOpen, spellId }) => { > - +

Change notes

Date: Sat, 9 Oct 2021 18:24:23 -0700 Subject: [PATCH 14/59] Return empty array of no modules on spell --- client/src/database/models/moduleModel.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/client/src/database/models/moduleModel.ts b/client/src/database/models/moduleModel.ts index a0e51d1d3..5e9f608c7 100644 --- a/client/src/database/models/moduleModel.ts +++ b/client/src/database/models/moduleModel.ts @@ -80,6 +80,7 @@ const loadModuleModel = db => { // todo need tobe recursive probably. Or we add the modules used to the spell when created? + if (moduleDocs.length === 0) return [] return moduleDocs.map(module => module.toJSON()) } From f98024728182d9b3a03edf366945963921959721 Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sat, 9 Oct 2021 18:25:01 -0700 Subject: [PATCH 15/59] Filter out possible undefined from module fetching --- client/src/database/models/moduleModel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/database/models/moduleModel.ts b/client/src/database/models/moduleModel.ts index 5e9f608c7..fecf7d793 100644 --- a/client/src/database/models/moduleModel.ts +++ b/client/src/database/models/moduleModel.ts @@ -81,7 +81,7 @@ const loadModuleModel = db => { // todo need tobe recursive probably. Or we add the modules used to the spell when created? if (moduleDocs.length === 0) return [] - return moduleDocs.map(module => module.toJSON()) + return moduleDocs.filter(Boolean).map(module => module.toJSON()) } return { From b721672f16c72deb21c735c3246a28c76740da1b Mon Sep 17 00:00:00 2001 From: Michael Sharpe Date: Sun, 10 Oct 2021 00:07:39 -0700 Subject: [PATCH 16/59] fix readonly dom property eror --- .../src/features/Thoth/components/EditorWindow/Deployment.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx index 7e6a09541..cd0645583 100644 --- a/client/src/features/Thoth/components/EditorWindow/Deployment.tsx +++ b/client/src/features/Thoth/components/EditorWindow/Deployment.tsx @@ -135,7 +135,7 @@ const DeploymentView = ({ open, setOpen, spellId }) => {