Skip to content

Commit

Permalink
Use react router action for sql raw query route
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Mar 11, 2024
1 parent c393ba2 commit 44e71db
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/renderer/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ConnectionErrorPage from './routes/errors/ConnectionsErrorPage';
import RootErrorPage from './routes/errors/RootErrorPage';
import { Home } from './routes/home';
import Root from './routes/root';
import SqlPage from './routes/sql.$connectionName';
import SqlPage, { action as sqlPageAction } from './routes/sql.$connectionName';

const appElement = document.getElementById('App');

Expand Down Expand Up @@ -61,6 +61,9 @@ const router = createHashRouter([
{
path: 'connections/:connectionName',
loader: connectionDetailPageLoader,
shouldRevalidate: ({ currentParams, nextParams }) => {
return currentParams.connectionName !== nextParams.connectionName;
},
element: <ConnectionDetailPage />,
children: [
{
Expand All @@ -77,6 +80,7 @@ const router = createHashRouter([
{
path: 'sql',
element: <SqlPage />,
action: sqlPageAction,
},
],
},
Expand Down
1 change: 1 addition & 0 deletions src/renderer/component/MonacoEditor/RawSqlEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function RawSqlEditor({
language: 'sql',
theme: 'currentTheme',
minimap: { enabled: false },
automaticLayout: true,
...memoizedMonacoOptions,
});

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/routes/connections.$connectionName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function loader({ params, request }: RouteParams) {
openedTable ? `/tables/${openedTable}` : ''
}`;

if (!request.url.endsWith(expectedUrl)) {
if (new URL(request.url).pathname !== expectedUrl) {
return redirect(expectedUrl);
}

Expand Down
57 changes: 26 additions & 31 deletions src/renderer/routes/sql.$connectionName.tsx
Original file line number Diff line number Diff line change
@@ -1,56 +1,51 @@
import { useState } from 'react';
import { Button, Flex, Form } from 'antd';
import { ActionFunctionArgs, useFetcher } from 'react-router-dom';
import invariant from 'tiny-invariant';
import { useConnectionContext } from '../../contexts/ConnectionContext';

Check failure on line 4 in src/renderer/routes/sql.$connectionName.tsx

View workflow job for this annotation

GitHub Actions / test (18.x)

'useConnectionContext' is defined but never used. Allowed unused vars must match /^_/u
import { useDatabaseContext } from '../../contexts/DatabaseContext';
import { useTranslation } from '../../i18n';
import { isResultSetHeader, isRowDataPacketArray } from '../../sql/type-guard';
import { QueryResult } from '../../sql/types';
import { RawSqlEditor } from '../component/MonacoEditor/RawSqlEditor';
import TableGrid from '../component/TableGrid';
import { useTableHeight } from '../component/TableLayout/useTableHeight';

const DEFAULT_VALUE = `SELECT *
FROM cart c
WHERE c.id > 5
LIMIT 10;`;
const DEFAULT_VALUE = `SELECT * FROM employees e WHERE e.gender = 'F' LIMIT 10;`;

export async function action({ request, params }: ActionFunctionArgs) {
const { databaseName, connectionName } = params;

invariant(connectionName, 'Connection name is required');
invariant(databaseName, 'Database name is required');

const formData = await request.formData();
const query = formData.get('raw');

invariant(typeof query === 'string', 'Query as string is required');

await window.sql.executeQuery(connectionName, `USE ${databaseName};`);
const result = await window.sql.executeQuery(connectionName, query);

return result;
}

// TODO : create an element for the `yScroll` (actually need to be wrapped in a Flex height 100 and overflow, etc.)
export default function SqlPage() {
const { t } = useTranslation();
const [result, setResult] = useState<Awaited<QueryResult> | null>(null);
const { database } = useDatabaseContext();
const { currentConnectionName } = useConnectionContext();
const [form] = Form.useForm();
const fetcher = useFetcher();

invariant(currentConnectionName, 'Connection name is required');
const result = fetcher.data;

const [yTableScroll, resizeRef] = useTableHeight();

return (
<Flex vertical gap="small" style={{ height: '100%' }}>
<Form
form={form}
initialValues={{
raw: DEFAULT_VALUE,
}}
onFinish={async (values) => {
const query = values.raw;

// TODO submit the form the get the error handling
if (database) {
await window.sql.executeQuery(
currentConnectionName,
`USE ${database};`
);
}

const result = await window.sql.executeQuery(
currentConnectionName,
query
);

setResult(result);
initialValues={{ raw: DEFAULT_VALUE }}
onFinish={(values) => {
fetcher.submit(values, {
method: 'post',
});
}}
>
<Form.Item name="raw" valuePropName="defaultValue">
Expand Down

0 comments on commit 44e71db

Please sign in to comment.