All files / src/form/list FinderFormDisplayPanel.tsx

80% Statements 36/45
38.88% Branches 7/18
100% Functions 8/8
80% Lines 36/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                      66x   4x     4x 3x   3x 3x 3x   3x 1x 1x 1x 1x     3x 1x           3x 1x 1x 1x         3x 3x 3x             3x   3x 6x   6x 6x 6x 6x   6x   6x             6x   6x 6x         6x                 3x                                           3x                                    
import React, { ReactElement, useCallback, useEffect, useState } from "react";
import { Button, Form, Space } from "antd";
import { SearchOutlined, } from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
 
import { fetchFormIdAndExtInfoByType, fetchDomainMeta } from "@utils/FetchUtils";
import { SearchValue, SearchPanelProps, TableMetaProps } from "@props/RecordProps";
import { getEditableController } from "@kernel/EditableComponentsMapping";
import { getFieldTitle } from "@utils/ComponentUtils";
import { getFinderConditions, setFinderCondition } from "@kernel/ServerSideFinder";
 
export const FinderFormDisplayPanel = (props: SearchPanelProps): ReactElement => {
 
  const formName = "finder-form";
 
  const { finderInitiated, setFinderInitiated, domainName,
    columns, fetchDataCallback, tableMode, zIndex } = props;
  const { t } = useTranslation();
 
  const [form] = Form.useForm();
  const [searching, setSearching] = useState<boolean>(false);
  const [domainMeta, setDomainMeta] = useState<Array<TableMetaProps>>([]);
 
  const search = useCallback((): void  => {
    setSearching(true);
    setFinderCondition(domainName, form.getFieldsValue(), columns);
    fetchDataCallback();
    setSearching(false);
  }, [columns, form, domainName, fetchDataCallback]);
 
  useEffect(() => {
    Iif (!finderInitiated) {
      search();
      setFinderInitiated(true);
    }
  }, [setFinderInitiated, finderInitiated, search]);
 
  useEffect(() => {
    fetchFormIdAndExtInfoByType(domainName, "Finder").then(json => {
      fetchDomainMeta(domainName, json.id).then((domainMeta) => {
        setDomainMeta(domainMeta);
      });
    });
  }, [domainName]);
 
  const conditions = getFinderConditions(domainName);
  const initialValues: { [propName: string]: SearchValue } = {};
  for (const property in conditions) {
    const condition = conditions[property];
    if (null != condition && null != condition.value) {
      initialValues[condition.columnKey] = condition.value;
    }
  }
 
  const hideColumnTypes = ['array', 'subTable', 'file', 'image'];
 
  const fields = domainMeta
    .filter(column => !hideColumnTypes.includes(column.type) && column.display !== false)
    .map(column => {
      const { type, helpText, key, unique, email, title } = column;
      const fieldTitle = getFieldTitle({ domainName, helpText, title, key, unique, email });
      const isEnum = type.indexOf("_enums_") > -1;
      const gc = getEditableController;
      let renderFunc;
      Iif (type === 'id') {
        renderFunc = gc("long");
      } else Iif (gc(type) == null) {
        if (isEnum) {
          renderFunc = gc("enum");
        } else {
          renderFunc = gc("object");
        }
      } else {
        renderFunc = gc(type, tableMode);
      }
      const options = undefined;
      const controller = (renderFunc == null) ? undefined : renderFunc({
        updatable: true, type, fieldTitle, form, key, options,
        domainName, fieldValue: initialValues[key], record: undefined, zIndex, multiple: false,
        column
      });
      return controller == null ? undefined : (<Form.Item
        label={fieldTitle}
        name={column.key}
        key={column.key}
      >{controller}</Form.Item>
      );
    });
 
  const searchButton = (
    <div
      style={{ textAlign: "center", margin: "auto" }}
    >
      <Space
        direction="horizontal"
        size="middle"
      >
        <Button
          title={t("Search")}
          type="primary"
          htmlType="submit"
          onClick={search}
          form={formName}
          disabled={searching}
          loading={searching}
        >
          <span><SearchOutlined /> {t('Search')}</span>
        </Button>
      </Space>
    </div>
  );
 
  return (
    <div className="finder-form">
      <Form
        layout="inline"
        id={formName}
        form={form}
        colon={false}
        initialValues={initialValues}
      >
        {fields}
      </Form>
      { searchButton}
    </div>
  );
 
};
 
export default FinderFormDisplayPanel;