use-submit-form.ts


Overview

The use-submit-form.ts file defines a React hook useSubmitForm designed to handle the submission of a form that collects database connection parameters and an optional SQL query. It integrates form validation using the zod schema validation library and manages the connection testing process via a custom hook useTestDbConnect.

This file primarily focuses on:


Detailed Documentation

1. Schemas

ExeSQLFormSchema

Field

Type

Validation

Description

db_type

string

Required, minimum length 1

The type of database (e.g., MySQL, PostgreSQL).

database

string

Required, minimum length 1

The database name.

username

string

Required, minimum length 1

The username for authentication.

host

string

Required, minimum length 1

The database host address.

port

number

Required

The port number to connect to.

password

string

Required, minimum length 1

The password for authentication.

max_records

number

Required

The maximum number of records to fetch or process.


FormSchema

Usage Example:

const validData = FormSchema.parse({
  db_type: 'mysql',
  database: 'my_db',
  username: 'admin',
  host: 'localhost',
  port: 3306,
  password: 'secret',
  max_records: 100,
  sql: 'SELECT * FROM users',
});

2. Functions and Hooks

useSubmitForm

import React from 'react';
import { useSubmitForm, FormSchema } from './use-submit-form';
import { useForm } from 'react-hook-form';

function DbConnectionForm() {
  const { onSubmit, loading } = useSubmitForm();
  const { register, handleSubmit, errors } = useForm({
    resolver: async (values) => {
      try {
        FormSchema.parse(values);
        return { values, errors: {} };
      } catch (e) {
        return { values: {}, errors: e.formErrors.fieldErrors };
      }
    },
  });

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      {/* form inputs for db_type, database, username, host, port, password, max_records, sql */}
      <button type="submit" disabled={loading}>Test Connection</button>
    </form>
  );
}

Implementation Details


Integration and Interactions


Visual Diagram

flowchart TD
    A[useSubmitForm Hook] --> B[useTestDbConnect Hook]
    A --> C[onSubmit(data: FormSchema)]
    C --> B
    B --> D[loading: boolean]

    subgraph FormSchema
        F1[db_type: string]
        F2[database: string]
        F3[username: string]
        F4[host: string]
        F5[port: number]
        F6[password: string]
        F7[max_records: number]
        F8[sql?: string]
    end

    C --> FormSchema

Diagram Explanation:


Summary

The use-submit-form.ts file provides a clean, reusable hook for managing the submission and validation of database connection forms, leveraging zod for schema validation and a custom hook for the connection lifecycle. It promotes separation of concerns and can be easily integrated into React components responsible for database connectivity testing workflows.


If further integration details or related files become available, the documentation can be extended to cover additional interactions or backend API specifics.