Skip to content

Commit

Permalink
Handle query that's not a function
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihal committed Jul 20, 2022
1 parent c08d424 commit 13f7f9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 9 additions & 3 deletions components/Editor/QueryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,23 @@ interface State {
}

const QueryForm: React.FC<QueryProps> = ({ id, fn, openByDefault, storageKey }) => {
const fnIsAFunction = typeof fn === 'function'

const { addLine } = useConsole()
const [open, setOpen] = useEditorState(`${storageKey}-open`, !!openByDefault)
const [storedValues, setStoredValues] = useEditorState(
`${storageKey}-values`,
JSON.stringify([...new Array(fn.length)].map(() => ''))
fnIsAFunction ? `${storageKey}-values` : null,
JSON.stringify([...new Array(fnIsAFunction ? fn.length : 0)].map(() => ''))
)
const [state, setState] = useState<State>({
status: STATUS.READY,
})

const values = JSON.parse(storedValues)
if (!fnIsAFunction) {
return <div>"{id}" is not a function</div>
}

const values = JSON.parse(storedValues || '{}')
const setValues = (newVals: string[]) => setStoredValues(JSON.stringify(newVals))

const functionNames = functionToParamNames(fn)
Expand Down
12 changes: 8 additions & 4 deletions hooks/editor-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ export const EDITOR_TYPES = {
}

export function useEditorState<T = any>(
key: string,
key?: string | null,
defaultState?: T,
storageKey: string = EDITOR_TYPES.EDITOR_STATE
): [T, (val: T) => void] {
const updater = getUpdater(key)
): [T | null, (val: T) => void] {
const updater = getUpdater(key || 'undefined')
updater.register()

const value = getEditorState({ key, storageKey }) || defaultState || null
if (!key) {
return [null, () => null]
}

const value: T | null = getEditorState({ key, storageKey }) || defaultState || null

const setValue = (newVal: T) => {
setEditorState({ key, value: newVal, storageKey })
Expand Down

0 comments on commit 13f7f9e

Please sign in to comment.