use-edit-query.ts


Overview

The use-edit-query.ts file defines a custom React hook named useEditQueryRecord designed to manage the editing lifecycle of query records within a form context. This hook integrates tightly with react-hook-form to manipulate an array of query inputs (BeginQuery objects), enabling functionalities such as adding, updating, deleting, and selecting query records. It also manages modal visibility states for editing records and tracks the currently selected record.

This hook abstracts complex form and UI state management related to query editing into a reusable logic piece, promoting cleaner component code and separation of concerns.


Detailed Explanation

useEditQueryRecord Hook

Signature

useEditQueryRecord({
  form,
}: INextOperatorForm & { form: UseFormReturn }): {
  ok: (record: BeginQuery) => void;
  currentRecord: BeginQuery;
  setRecord: (record: BeginQuery) => void;
  visible: boolean;
  hideModal: () => void;
  showModal: (idx?: number, record?: BeginQuery) => void;
  otherThanCurrentQuery: BeginQuery[];
  handleDeleteRecord: (idx: number) => void;
}

Parameters

Returns

An object exposing the following properties and methods:

Name

Type

Description

ok

(record: BeginQuery) => void

Function to add a new query record or update an existing one in the inputs list.

currentRecord

BeginQuery

The currently selected query record for editing.

setRecord

(record: BeginQuery) => void

Setter function to update the current selected record.

visible

boolean

Boolean indicating if the modal for editing query records is visible.

hideModal

() => void

Function to hide the modal.

showModal

(idx?: number, record?: BeginQuery) => void

Function to show the modal, optionally setting the index and current record being edited.

otherThanCurrentQuery

BeginQuery[]

Array of query records excluding the one currently being edited (tracked by index).

handleDeleteRecord

(idx: number) => void

Function to delete a query record at the specified index from the inputs list.


Internal State and Hooks


Functions and Methods

handleEditRecord(record: BeginQuery): void

handleEditRecord({
  queryText: 'SELECT * FROM users',
  id: 'query1',
  // ...other BeginQuery properties
});

handleShowModal(idx?: number, record?: BeginQuery): void

handleShowModal(2, inputs[2]); // Edit the record at index 2
handleShowModal(); // Show modal for adding a new record

handleDeleteRecord(idx: number): void

handleDeleteRecord(1); // Deletes the record at index 1

Implementation Details and Algorithms


Interaction with Other System Parts


Usage Example

import React from 'react';
import { useForm } from 'react-hook-form';
import { useEditQueryRecord } from './use-edit-query';
import { BeginQuery } from '../../interface';

type FormValues = {
  inputs: BeginQuery[];
};

const QueryEditorComponent = () => {
  const form = useForm<FormValues>({ defaultValues: { inputs: [] } });
  const {
    ok: saveQuery,
    currentRecord,
    setRecord,
    visible,
    hideModal,
    showModal,
    otherThanCurrentQuery,
    handleDeleteRecord,
  } = useEditQueryRecord({ form });

  return (
    <>
      <button onClick={() => showModal()}>Add Query</button>
      {otherThanCurrentQuery.map((query, idx) => (
        <div key={idx}>
          <span>{query.queryText}</span>
          <button onClick={() => showModal(idx, query)}>Edit</button>
          <button onClick={() => handleDeleteRecord(idx)}>Delete</button>
        </div>
      ))}

      {visible && (
        <Modal onClose={hideModal}>
          <QueryForm
            initialValues={currentRecord}
            onSave={(updatedRecord) => saveQuery(updatedRecord)}
          />
        </Modal>
      )}
    </>
  );
};

Visual Diagram

flowchart TD
    A[useEditQueryRecord Hook]
    A --> B[useSetSelectedRecord<BeginQuery>]
    A --> C[useSetModalState]
    A --> D[useState(index)]
    A --> E[useWatch(inputs)]
    A --> F[useMemo(otherThanCurrentQuery)]
    A --> G[handleEditRecord(record)]
    A --> H[handleShowModal(idx?, record?)]
    A --> I[handleDeleteRecord(idx)]

    G --> E
    G --> D
    G --> C
    H --> B
    H --> D
    H --> C
    I --> E
    I --> D

Summary

use-edit-query.ts provides a powerful and reusable hook to manage query record editing within a form, backed by robust state handling for modal visibility and selected records. It abstracts away complex form manipulation logic, providing clear APIs for components to integrate query editing features seamlessly.

This hook plays a critical role in systems dealing with dynamic query inputs, enabling add/edit/delete workflows with clean state management and UI feedback.