make typescript happy

This commit is contained in:
fmtabbara
2021-08-31 12:55:13 +01:00
parent b95893bb02
commit d8e6a5fb2e
2 changed files with 80 additions and 81 deletions
@@ -1,28 +1,22 @@
import React, { useState } from 'react' import React from 'react'
import { import { List, ListItem } from '@material-ui/core'
Button,
Checkbox,
FormControlLabel,
Grid,
InputAdornment,
List,
ListItem,
TextField,
Theme,
} from '@material-ui/core'
import { useTheme } from '@material-ui/styles'
import { DocEntry } from './DocEntry' import { DocEntry } from './DocEntry'
export const ApiList = () => { export const ApiList = () => {
const [advancedShown, setAdvancedShown] = React.useState(false)
const theme: Theme = useTheme()
return ( return (
<List> <List>
<ListItem><DocEntry function={{ name: 'connect_with_mnemonic', args: [{ name: 'mnemonic', type: 'str' }] }} /></ListItem> <ListItem>
<ListItem><DocEntry function={{ name: 'get_balance', args: [] }} /></ListItem> <DocEntry
function={{
name: 'connect_with_mnemonic',
args: [{ name: 'mnemonic', type: 'str' }],
}}
/>
</ListItem>
<ListItem>
<DocEntry function={{ name: 'get_balance', args: [] }} />
</ListItem>
</List> </List>
) )
} }
@@ -1,85 +1,90 @@
import React, { useState } from 'react' import React, { useState } from 'react'
import { import { Button, Card, TextField } from '@material-ui/core'
Box,
Button,
Card,
Checkbox,
Divider,
FormControlLabel,
Grid,
InputAdornment,
TextField,
Theme,
} from '@material-ui/core'
import { useTheme } from '@material-ui/styles'
import { invoke } from '@tauri-apps/api' import { invoke } from '@tauri-apps/api'
import CardContent from '@material-ui/core/CardContent'; import CardContent from '@material-ui/core/CardContent'
interface DocEntryProps { interface DocEntryProps {
function: FunctionDef; function: FunctionDef
} }
interface FunctionDef { interface FunctionDef {
name: string, name: string
args: ArgDef[] args: ArgDef[]
} }
interface ArgDef { interface ArgDef {
name: string, name: string
type: string type: string
} }
const argKey = (functionName: string, arg: string) => `${functionName}_${arg}` const argKey = (functionName: string, arg: string) => `${functionName}_${arg}`
function collectArgs(functionName: string, args: ArgDef[]) { function collectArgs(functionName: string, args: ArgDef[]) {
let invokeArgs = {} let invokeArgs: { [key: string]: string } = {}
for (let arg of args) { args.forEach((arg) => {
invokeArgs[arg.name] = document.getElementById(argKey(functionName, arg.name)).value const elem: HTMLElement | null = document.getElementById(
} argKey(functionName, arg.name)
return invokeArgs )
if (elem) invokeArgs[arg.name] = (elem as HTMLInputElement).value || ''
})
return invokeArgs
} }
export const DocEntry = (props: DocEntryProps) => { export const DocEntry = (props: DocEntryProps) => {
const [card, setCard] = React.useState(<Card />) const [card, setCard] = React.useState(<Card />)
const theme: Theme = useTheme()
const onClick = () => { const onClick = () => {
invoke(props.function.name, collectArgs(props.function.name, props.function.args)).then( invoke(
(result) => { props.function.name,
setCard(<Card><CardContent>{JSON.stringify(result, null, 4)}</CardContent></Card>) collectArgs(props.function.name, props.function.args)
} )
).catch( .then((result) => {
(e) => setCard(<Card><CardContent>{e}</CardContent></Card>) setCard(
<Card>
<CardContent>{JSON.stringify(result, null, 4)}</CardContent>
</Card>
) )
} })
.catch((e) =>
setCard(
<Card>
<CardContent>{e}</CardContent>
</Card>
)
)
}
let fields = [] return (
for (let arg of props.function.args) { <div>
fields.push(<TextField <Button
variant="contained"
color="primary"
size="small"
disableElevation
onClick={onClick}
>
{props.function.name}
</Button>
<Button
variant="contained"
size="small"
disableElevation
onClick={() => setCard(<Card />)}
>
X
</Button>
<div>
{props.function.args.map((arg) => (
<TextField
label={arg.name} label={arg.name}
id={argKey(props.function.name, arg.name)} id={argKey(props.function.name, arg.name)}
key={argKey(props.function.name, arg.name)} /> key={argKey(props.function.name, arg.name)}
) />
} ))}
return ( </div>
<div> <br />
<Button variant="contained" {card}
color="primary" </div>
size="small" )
disableElevation
onClick={onClick}>
{props.function.name}
</Button>
<Button
variant="contained"
size="small"
disableElevation
onClick={() => setCard(<Card />)}>X
</Button>
<div>{fields}</div>
<br />
{card}
</div>
)
} }