use-edit-query.ts


Overview

The use-edit-query.ts file defines a custom React hook useEditQueryRecord designed to manage the editing lifecycle of query records within a form. It facilitates viewing, adding, updating, and deleting query records represented by the BeginQuery interface. The hook integrates tightly with React Hook Form for form state management and leverages modal visibility hooks to control UI interactions related to editing query entries.

This hook is primarily used in contexts where multiple query inputs need to be managed dynamically inside a form, such as query builders or filter configuration UIs. It abstracts the logic for manipulating the list of queries, modal controls, and the currently selected record, providing a clean API for components to handle query editing workflows.


Detailed Explanations

useEditQueryRecord

export const useEditQueryRecord = ({
  form,
}: INextOperatorForm & { form: UseFormReturn }) => { ... }

Purpose

A custom React hook that manages the internal state and behavior required to edit a collection of query records (BeginQuery[]) in a form. It handles:

Parameters

Returns

An object containing handlers and state values for managing query records:

Property

Type

Description

ok

(record: BeginQuery) => void

Function to add or update a query record in the form. Closes the modal after updating.

currentRecord

BeginQuery

The currently selected query record for editing.

setRecord

(record: BeginQuery) => void

Setter function to update the currently selected record.

visible

boolean

Boolean indicating whether the edit modal is currently visible.

hideModal

() => void

Function to hide the edit modal.

showModal

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

Function to show the edit modal, optionally setting the index and record to edit.

otherThanCurrentQuery

BeginQuery[]

Array of all query records except the one currently being edited.

handleDeleteRecord

(idx: number) => void

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

Usage Example

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

const QueryEditorComponent = () => {
  const form = useForm<{ inputs: BeginQuery[] }>({
    defaultValues: { inputs: [] },
  });

  const {
    ok: saveQuery,
    currentRecord,
    setRecord,
    visible,
    showModal,
    hideModal,
    otherThanCurrentQuery,
    handleDeleteRecord,
  } = useEditQueryRecord({ form });

  return (
    <>
      <button onClick={() => showModal()}>Add Query</button>
      {visible && (
        <Modal onClose={hideModal}>
          <QueryForm
            record={currentRecord}
            onSave={saveQuery}
            onCancel={hideModal}
          />
        </Modal>
      )}
      <QueryList
        queries={otherThanCurrentQuery}
        onEdit={(idx, record) => showModal(idx, record)}
        onDelete={handleDeleteRecord}
      />
    </>
  );
};

Implementation Details and Algorithms


Interaction with Other System Parts


Mermaid Diagram

The following class diagram illustrates the structure and key methods/properties of the useEditQueryRecord hook, including its interactions with imported hooks and form state.

classDiagram
    class useEditQueryRecord {
        - index: number
        - inputs: BeginQuery[]
        - otherThanCurrentQuery: BeginQuery[]
        + ok(record: BeginQuery): void
        + currentRecord: BeginQuery
        + setRecord(record: BeginQuery): void
        + visible: boolean
        + hideModal(): void
        + showModal(idx?: number, record?: BeginQuery): void
        + handleDeleteRecord(idx: number): void
    }
    class useSetSelectedRecord~T~ {
        + setRecord(record: T): void
        + currentRecord: T
    }
    class useSetModalState {
        + visible: boolean
        + showModal(): void
        + hideModal(): void
    }
    class UseFormReturn {
        + getValues(name?: string): any
        + setValue(name: string, value: any): void
        + control: object
    }
    useEditQueryRecord --> useSetSelectedRecord~BeginQuery~
    useEditQueryRecord --> useSetModalState
    useEditQueryRecord --> UseFormReturn

Summary

This documentation should help developers understand the purpose, usage, and internal workings of the use-edit-query.ts file, aiding maintenance and integration.