Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework query editor layout #655

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const TypescriptEditor = lazyComponent(() => import('../../TypescriptEditor'), {

const JsExpressionEditorRoot = styled('div')(({ theme }) => ({
height: 150,
border: '1px solid black',
borderColor: theme.palette.divider,
border: `1px solid ${theme.palette.divider}`,
}));

export interface JsExpressionEditorProps extends WithControlledProp<string> {
Expand Down
229 changes: 130 additions & 99 deletions packages/toolpad-app/src/components/AppEditor/PageEditor/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
TextField,
InputAdornment,
Divider,
Typography,
Toolbar,
MenuItem,
SxProps,
Alert,
Box,
} from '@mui/material';
import * as React from 'react';
import AddIcon from '@mui/icons-material/Add';
Expand Down Expand Up @@ -317,112 +317,143 @@ function QueryNodeEditorDialog<Q, P>({
const queryEditorContext = React.useMemo(() => ({ appId, connectionId }), [appId, connectionId]);

return (
<Dialog fullWidth maxWidth="lg" open={open} onClose={handleClose} scroll="body">
<DialogTitle>Edit Query ({node.id})</DialogTitle>
<Dialog fullWidth maxWidth="xl" open={open} onClose={handleClose}>
<DialogTitle>
<Stack direction="row" gap={2}>
<NodeNameEditor node={node} />
<ConnectionSelect
dataSource={dataSourceId}
value={input.attributes.connectionId.value || null}
onChange={handleConnectionChange}
/>
</Stack>
</DialogTitle>
<Divider />
{dataSourceId && dataSource ? (
<DialogContent>
<Stack spacing={2} py={1}>
<Stack direction="row" gap={2}>
<NodeNameEditor node={node} />
<ConnectionSelect
dataSource={dataSourceId}
value={input.attributes.connectionId.value || null}
onChange={handleConnectionChange}
/>
<DialogContent sx={{ overflow: 'hidden', display: 'flex', flexDirection: 'column', p: 0 }}>
<Box sx={{ display: 'flex', flexDirection: 'row', minHeight: 0 }}>
<Stack
sx={{
flex: 1,
minWidth: 0,
overflow: 'auto',
gap: 2,
px: 3,
py: 1,
}}
>
<ConnectionContextProvider value={queryEditorContext}>
<dataSource.QueryEditor
connectionParams={connection?.attributes.params.value}
value={{
query: input.attributes.query.value,
params: input.params,
}}
liveParams={liveParams}
onChange={handleQueryChange}
globalScope={pageState}
/>
</ConnectionContextProvider>

<Grid container direction="row" spacing={1}>
{/* TODO: move transform inside of the dataSource.QueryEditor and remove the conditional */}
{dataSourceId === 'function' ? null : (
<React.Fragment>
<Divider />
<Grid item xs={6}>
<Stack>
<FormControlLabel
label="Transform response"
control={
<Checkbox
checked={input.attributes.transformEnabled?.value ?? false}
onChange={handleTransformEnabledChange}
inputProps={{ 'aria-label': 'controlled' }}
/>
}
/>

<JsExpressionEditor
globalScope={{}}
value={
input.attributes.transform?.value ?? '(data) => {\n return data;\n}'
}
onChange={handleTransformFnChange}
disabled={!input.attributes.transformEnabled?.value}
/>
</Stack>
</Grid>
</React.Fragment>
)}
</Grid>
</Stack>

<Divider />
<Typography>Build query:</Typography>
<ConnectionContextProvider value={queryEditorContext}>
<dataSource.QueryEditor
connectionParams={connection?.attributes.params.value}
value={{
query: input.attributes.query.value,
params: input.params,
{/* TODO: move preview inside of the dataSource.QueryEditor and remove the conditional */}
{dataSourceId === 'function' ? null : (
<Box
sx={{
flex: 1,
minWidth: 0,
borderLeft: 1,
borderColor: 'divider',
display: 'flex',
flexDirection: 'column',
}}
liveParams={liveParams}
onChange={handleQueryChange}
globalScope={pageState}
/>
</ConnectionContextProvider>
<Divider />
<Typography>Options:</Typography>
<Grid container direction="row" spacing={1}>
<Grid item xs={4}>
<Stack direction="column" gap={1}>
<FormControlLabel
control={
<Checkbox
checked={input.attributes.refetchOnWindowFocus?.value ?? true}
onChange={handleRefetchOnWindowFocusChange}
/>
}
label="Refetch on window focus"
/>
<FormControlLabel
control={
<Checkbox
checked={input.attributes.refetchOnReconnect?.value ?? true}
onChange={handleRefetchOnReconnectChange}
/>
}
label="Refetch on network reconnect"
/>
<TextField
InputProps={{
startAdornment: <InputAdornment position="start">s</InputAdornment>,
}}
sx={{ maxWidth: 300 }}
type="number"
label="Refetch interval"
value={refetchIntervalInSeconds(input.attributes.refetchInterval?.value) ?? ''}
onChange={handleRefetchIntervalChange}
/>
</Stack>
</Grid>
<Grid item xs={6}>
<Stack>
<FormControlLabel
label="Transform response"
control={
<Checkbox
checked={input.attributes.transformEnabled?.value ?? false}
onChange={handleTransformEnabledChange}
inputProps={{ 'aria-label': 'controlled' }}
/>
}
/>

<JsExpressionEditor
globalScope={{}}
value={input.attributes.transform?.value ?? '(data) => {\n return data;\n}'}
onChange={handleTransformFnChange}
disabled={!input.attributes.transformEnabled?.value}
/>
</Stack>
</Grid>
</Grid>
<Divider />
<Toolbar disableGutters>
<LoadingButton
size="medium"
disabled={previewParams === paramsObject && previewQuery === input}
loading={isPreviewLoading}
loadingPosition="start"
variant="contained"
onClick={handleUpdatePreview}
startIcon={<PlayArrowIcon />}
>
Preview
</LoadingButton>
</Toolbar>
{queryPreview.error ? <ErrorAlert error={queryPreview.error} /> : null}
{queryPreview.isSuccess ? <JsonView src={queryPreview.data} /> : null}
<Toolbar>
<LoadingButton
size="medium"
disabled={previewParams === paramsObject && previewQuery === input}
loading={isPreviewLoading}
loadingPosition="start"
variant="contained"
onClick={handleUpdatePreview}
startIcon={<PlayArrowIcon />}
>
Preview
</LoadingButton>
</Toolbar>
<Box sx={{ flex: 1, minHeight: 0, px: 3, py: 1, overflow: 'auto' }}>
{queryPreview.error ? <ErrorAlert error={queryPreview.error} /> : null}
{queryPreview.isSuccess ? <JsonView src={queryPreview.data} /> : null}
</Box>
</Box>
)}
</Box>
<Divider />
<Stack direction="row" alignItems="center" sx={{ pt: 2, px: 3, gap: 2 }}>
<FormControlLabel
control={
<Checkbox
checked={input.attributes.refetchOnWindowFocus?.value ?? true}
onChange={handleRefetchOnWindowFocusChange}
/>
}
label="Refetch on window focus"
/>
<FormControlLabel
control={
<Checkbox
checked={input.attributes.refetchOnReconnect?.value ?? true}
onChange={handleRefetchOnReconnectChange}
/>
}
label="Refetch on network reconnect"
/>
<TextField
InputProps={{
startAdornment: <InputAdornment position="start">s</InputAdornment>,
}}
sx={{ maxWidth: 300 }}
type="number"
label="Refetch interval"
value={refetchIntervalInSeconds(input.attributes.refetchInterval?.value) ?? ''}
onChange={handleRefetchIntervalChange}
/>
</Stack>
</DialogContent>
) : (
<DialogContent>
<Alert severity="error">DaPtaSource &quot;{dataSourceId}&quot; not found</Alert>
<Alert severity="error">Datasource &quot;{dataSourceId}&quot; not found</Alert>
</DialogContent>
)}
<DialogActions>
Expand Down