use-submit-form.ts


Overview

The use-submit-form.ts file defines a React hook useSubmitForm that facilitates submitting database connection parameters and an optional SQL query to test connectivity to a database. It employs schema validation with Zod to ensure the form data conforms to expected types and constraints. The hook integrates with a custom hook useTestDbConnect (imported from elsewhere in the application) that handles the actual database connectivity test request.

This file is primarily concerned with form data validation and submission logic for database connectivity testing within a React application, abstracting away request handling and loading state management.


Detailed Explanation

Schemas

ExeSQLFormSchema

An object schema defining the expected structure of the core database connection parameters:

Property

Type

Description

Validation

db_type

string

Type of the database (e.g., MySQL, PostgreSQL)

Non-empty string

database

string

Database name

Non-empty string

username

string

Username for authentication

Non-empty string

host

string

Database host address

Non-empty string

port

number

Port number for database connection

Number (no explicit range)

password

string

Password for authentication

Non-empty string

max_records

number

Maximum number of records to fetch/return

Number (no explicit range)

This schema is a plain object, not a Zod schema instance but used as part of the full form schema.

FormSchema

A Zod object schema combining the optional SQL query string with the database connection schema:

const FormSchema = z.object({
  sql: z.string().optional(),
  ...ExeSQLFormSchema,
});

This schema is used to validate the entire form data before submission.


Function: useSubmitForm

Purpose

Defines a React hook that provides form submission logic specifically for testing database connections using the given form data.

Implementation Details

Parameters

Return Value

An object:

{
  loading: boolean,
  onSubmit: (data: z.infer<typeof FormSchema>) => Promise<void>
}

Usage Example

import React from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { FormSchema, useSubmitForm } from './use-submit-form';

function DatabaseTestForm() {
  const { loading, onSubmit } = useSubmitForm();
  const { register, handleSubmit } = useForm({
    resolver: zodResolver(FormSchema),
  });

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register('db_type')} placeholder="Database Type" />
      <input {...register('database')} placeholder="Database Name" />
      <input {...register('username')} placeholder="Username" />
      <input {...register('host')} placeholder="Host" />
      <input type="number" {...register('port', { valueAsNumber: true })} placeholder="Port" />
      <input {...register('password')} type="password" placeholder="Password" />
      <input type="number" {...register('max_records', { valueAsNumber: true })} placeholder="Max Records" />
      <textarea {...register('sql')} placeholder="SQL Query (optional)" />
      <button type="submit" disabled={loading}>
        {loading ? 'Testing...' : 'Test Connection'}
      </button>
    </form>
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram

This flowchart represents the relationship between the main entities in this file: the schemas, the useTestDbConnect hook, and the useSubmitForm hook that consumes them.

flowchart TD
    A[Form Data] -->|Validated by| B[FormSchema (Zod)]
    B -->|Validated Data| C[useSubmitForm Hook]
    C -->|Calls| D[useTestDbConnect Hook]
    D -->|Performs| E[Test DB Connection]
    C -->|Exposes| F[onSubmit(data)]
    C -->|Exposes| G[loading flag]

    style A fill:#f9f,stroke:#333,stroke-width:1px
    style B fill:#bbf,stroke:#333,stroke-width:1px
    style C fill:#bfb,stroke:#333,stroke-width:1px
    style D fill:#fbf,stroke:#333,stroke-width:1px
    style E fill:#ffb,stroke:#333,stroke-width:1px
    style F fill:#fbf,stroke:#333,stroke-width:1px
    style G fill:#fbf,stroke:#333,stroke-width:1px

Summary


This documentation should enable developers to understand the role of this file, how to use the exported hook and schemas, and how it fits into the larger system for database connection testing.