All files / src/form/fullTextSearchList FullTextSearchPanel.tsx

1.45% Statements 2/137
0% Branches 0/107
0% Functions 0/39
1.47% Lines 2/136

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572                                                            66x                                           66x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Input, Pagination, Popover, Space, Tag, Empty, List, Button } from 'antd';
import { SortAscendingOutlined, SortDescendingOutlined } from '@ant-design/icons';
import {
  FormProps,
  RecordProps, SearchConditions,
  SearchValue,
  SortDirection, TableMetaProps
} from '@props/RecordProps';
import { ReloadOutlined, SaveOutlined, CloseCircleOutlined } from '@ant-design/icons';
import { getColumnTransKey, isObjectType } from '@utils/ColumnsUtils';
import { fetchFormIdAndExtInfoByType, fetchSearchResult } from '@utils/FetchUtils';
import { getPagination } from '../../components';
import { ObjectIdsDisplayComponent } from '../../form/fields';
import { FullTextSearchDefaultPageSize } from '@config/base';
import { isBlank, isNotBlank } from '@utils/StringUtils';
import CardSearchResultCell from './CardSearchResultCell';
import { CreateAction, PredefinedFilters } from '../../form';
import ActionButtons from '../action/ActionButtons';
import {
  getSearchConditionsForDomain,
  removeSearchConditions, SearchComponent
} from "@kernel/ServerSideSearcher";
import { MatcherDisplay } from "@kernel/ServerSideSearcherConfig";
import i18n from "@config/i18n";
import { emptyMethod } from "@utils/Constants";
import SaveDynamicFilterPopover from "../../components/filters/SaveDynamicFilterPopover";
import { isEqual } from 'lodash';
 
const { Search } = Input;
 
interface FullTextSearchPanelProps {
  zIndex: number;
  domainName: string;
  columns: Array<TableMetaProps>;
  fullTextSearchColumns: Array<TableMetaProps>;
  additionalSearchColumns: Array<TableMetaProps>;
  sortColumns: Array<TableMetaProps>;
  panelDisplayColumns: Array<TableMetaProps>;
  fetchDataCallback: (data: Array<RecordProps>) => void;
  refreshData: boolean;
  ownerClass?: string;
  ownerIds?: Array<number>;
  ownerClassColumnName?: string;
  contentDisplayColumns: Array<TableMetaProps>;
  changeCallback: (record: RecordProps, selected: boolean) => void;
  initShowCreateModal?: boolean;
  formExtInfo?: FormProps;
  setOwnerIds?: (ownerIds: Array<number>) => void;
}
 
const FullTextSearchPanel = (props: FullTextSearchPanelProps): ReactElement => {
  const {
    domainName, fetchDataCallback, fullTextSearchColumns,
    additionalSearchColumns, sortColumns, refreshData,
    ownerClass, ownerIds, ownerClassColumnName,
    contentDisplayColumns, changeCallback, columns, zIndex,
    initShowCreateModal, formExtInfo, panelDisplayColumns, setOwnerIds
  } = props;
  const { t } = useTranslation();
  const [data, setData] = useState<Array<RecordProps>>();
  const [searchKeyword, setSearchKeyword] = useState<string>("");
  const [loading, setLoading] = useState<boolean>(false);
  const [current, setCurrent] = useState<number>(1);
  const [total, setTotal] = useState<number>(0);
  const [pageSize, setPageSize] = useState<number>(FullTextSearchDefaultPageSize);
  const [sortField, setSortField] = useState<TableMetaProps>();
  const [sortDirection, setSortDirection] = useState<SortDirection>();
  const [updateFormId, setUpdateFormId] = useState<number>(-1);
  const [selectedData, setSelectedData] = useState<Array<RecordProps>>([]);
  const [additionalConditions, setAdditionalConditions] = useState<SearchConditions>({});
  const [ownerClassColumnNameState, setOwnerClassColumnNameState] = useState<string>(ownerClassColumnName ?? "");
  const [ownerIdsState, setOwnerIdsState] = useState<Array<number> | undefined>(ownerIds);
  const [refreshPredefinedFilters, setRefreshPredefinedFilters] = useState(0);
 
  const refresh = (): void => {
    setRefreshPredefinedFilters(prevRefresh => prevRefresh + 1);
  };
 
  useEffect(() => {
    if (!isEqual(ownerIds, ownerIdsState)) {
      setOwnerIdsState(ownerIds);
    }
  }, [ownerIds, ownerIdsState]);
 
  useEffect(() => {
    if (ownerClassColumnName !== ownerClassColumnNameState && ownerClassColumnName != null) {
      setOwnerClassColumnNameState(ownerClassColumnName);
    }
  }, [ownerClassColumnName, ownerClassColumnNameState]);
 
  const ownerBlank = isBlank(ownerClass) || !((ownerIdsState?.length ?? 0) > 0);
 
  const getSortLabel = (): string => {
    if (sortField == null || sortDirection == null) {
      return t("Click to select sort field");
    }
    return t(`Sort ${sortDirection}`, {
      column: sortField.title ?? t(getColumnTransKey(domainName, sortField.key))
    });
  };
 
  useEffect(() => {
    fetchFormIdAndExtInfoByType(domainName, 'Update')
      .then((res: FormProps) => {
        setUpdateFormId(res.id);
      })
      .catch(e => console.error(`Failed to fetch update form Id of ${domainName}: ${e}`));
  }, [domainName]);
 
  const emptyElem = (description: string): ReactElement => {
    return (<Empty
      image={Empty.PRESENTED_IMAGE_SIMPLE}
      description={t(description)}
      style={{ paddingTop: "1rem" }}
    />);
  };
  //ATTENTION: 这里是实际控制结果显示面板的高度的
  // 连带控制分页组件的下边界的对齐
  //FIXME #833 根据是否有 owner 信息来自动设定 height
  const height = `${window.innerHeight - 162}px`;
 
  /**
   * Prepares the search conditions for full text search based on the search keyword and full text search columns.
   * @returns {SearchConditions} The search conditions for full text search.
   */
  const prepareFullTextSearchCondition = useCallback((): SearchConditions => {
    const searchConditions = {} as SearchConditions;
    fullTextSearchColumns
      .forEach(c => {
        searchConditions[c.key] = {
          value: searchKeyword,
          matchMode: "contains",
          columnKey: c.key,
          status: "outOfSync"
        };
      });
    return searchConditions;
  }, [fullTextSearchColumns, searchKeyword]);
 
  /**
   * Handles the search action for the full text search panel.
   * @param {string} value - The search keyword.
   * @param {Object} props - The optional properties for the search action.
   * @param {number} props.newCurrent - The new current page number.
   * @param {number} props.newPageSize - The new page size.
   * @param {SortDirection} props.newSortDirection - The new sort direction.
   * @param {TableMetaProps} props.newSortField - The new sort field.
   * @param {SearchConditions} newAdditionalConditions - The additional search conditions.
   */
  const onSearch = useCallback((
    value: string, props: {
      newCurrent?: number;
      newPageSize?: number;
      newSortDirection?: SortDirection;
      newSortField?: TableMetaProps
    },
    newAdditionalConditions: SearchConditions
  ) => {
    // eslint-disable-next-line react/prop-types
    const { newCurrent, newPageSize, newSortDirection, newSortField } = props;
    const sortConditionBlank = (isBlank(newSortDirection as string) || newSortField == null);
    const moreConditions = newAdditionalConditions ?? additionalConditions;
    const moreConditionBlank = (moreConditions == null || JSON.stringify(moreConditions) === "{}");
    const conditionBlank = isBlank(value) && isBlank(searchKeyword) && sortConditionBlank && moreConditionBlank;
    let fullTextConditions = {} as SearchConditions;
    //如果之前保存的搜索条件和新的搜索条件都为空, 且 owner 信息也为空, 则从后台获取所有的数据
    if (conditionBlank && ownerBlank) {
      // Do nothing if all conditions is blank
    } else if (!conditionBlank) {
      fullTextConditions = prepareFullTextSearchCondition();
    }
    setLoading(true);
    fetchSearchResult({
      domainName,
      searchConditions: { ...moreConditions, ...formExtInfo?.extInfo?.conditions },
      fullTextConditions,
      max: newPageSize ?? pageSize,
      current: newCurrent ?? 0, isFullTextSearch: true,
      sortDirection: newSortDirection ?? sortDirection,
      // eslint-disable-next-line react/prop-types
      sortField: newSortField?.key ?? sortField?.key,
      ownerClass,
      ownerIds: ownerIdsState,
      ownerClassColumnName: ownerClassColumnNameState,
    }).then((result) => {
      const { data, total } = result;
      fetchDataCallback(data);
      setData(data);
      setTotal(total);
    }).finally(() => setLoading(false))
      .catch(e => console.error(`Failed to search ${domainName} with condition ${fullTextConditions}: ${e}`));
 
  }, [
    domainName, fetchDataCallback, pageSize,
    searchKeyword, sortDirection, sortField, ownerClass,
    ownerIdsState, ownerBlank, additionalConditions, formExtInfo,
    prepareFullTextSearchCondition, ownerClassColumnNameState
  ]);
 
  useEffect(() => {
    setSelectedData([]);
    onSearch(searchKeyword, {}, additionalConditions);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [refreshData, ownerIdsState, ownerClassColumnNameState, ownerClass, additionalConditions, searchKeyword]);
 
  // const onSelectLabel = (selectedKeys: Array<number>): void => {
  //   if (selectedKeys.length > 0) {
  //   }
  // };
 
  const elements: Array<ReactElement> = additionalSearchColumns
    .map(column => {
      const { key, title, type } = column;
      const props = {
        setSearchInputs: emptyMethod,
        refreshSearchResult: () => {
          const cc = getSearchConditionsForDomain(domainName) ?? {};
          setAdditionalConditions(cc);
          onSearch(searchKeyword, {}, cc);
        },
        domainName,
        searchInputs: {},
        column,
        ownerClass,
        ownerIds: ownerIdsState,
        ownerClassColumnName: ownerClassColumnNameState,
        max: pageSize,
        current,
        zIndex,
        clearFilters: () => {
          removeSearchConditions(domainName, key);
        },
        confirm: emptyMethod,
        selectedKeys: [],
        setSelectedKeys: emptyMethod,
      };
      let currCondition = null;
      for (const conditionKey in additionalConditions) {
        if (!Object.prototype.hasOwnProperty.call(additionalConditions, conditionKey)) {
          continue;
        }
        if (conditionKey === key) {
          currCondition = additionalConditions[conditionKey];
          break;
        }
      }
      const matchMode = `matchMode:${MatcherDisplay[currCondition?.matchMode ?? "="]}`;
      const matchValue = Array.isArray(currCondition?.value) ?
        currCondition?.value.map((v: SearchValue) =>
          <Tag key={v?.toString()} className="search-condition-field">{v}</Tag>) : currCondition?.value;
      const value = (isObjectType(type)) ?
        <ObjectIdsDisplayComponent
          text={matchValue?.toString().split(",")}
          record={{
            id: -1,
            [key]: matchValue,
            objectType: type,
          } as RecordProps}
          columnKey={key}
          zIndex={zIndex}
          flatDisplay={true}
        /> : matchValue;
      const titleDisplay = (currCondition == null) ?
        title : <>{title} {i18n.t(matchMode)} {value}</>;
      return (
        <Popover
          trigger={["click"]}
          key={key}
          content={
            <div>
              <SearchComponent {...props} />
            </div>
          }
        >
          <Tag
            key={key}
            title={title}
            className="search-condition-tag"
          >
            {titleDisplay}
          </Tag>
        </Popover>
      );
    });
 
  const sortCallback = (column?: TableMetaProps, direction?: SortDirection): void => {
    setSortField(column);
    setSortDirection(direction);
    onSearch(searchKeyword, {
      newSortField: column,
      newSortDirection: direction
    }, additionalConditions);
  };
 
  const sortContent = (<Space
    direction="vertical"
    size={4}
  >
    <span
      style={{ cursor: "pointer", width: "100%", display: "block" }}
      onClick={() => sortCallback(undefined, undefined)}
    >
      <CloseCircleOutlined /> {t('Clear sort')}
    </span>
    {sortColumns
      .map(c => {
        return (
          <Space
            direction="vertical"
            key={c.key}
            size={4}
            style={{ width: "100%" }}
          >
            <span
              style={{ cursor: 'n-resize', width: "100%", display: "block" }}
              onClick={() => sortCallback(c, 'asc')}
            >
              <SortDescendingOutlined /> {c.title}</span>
            <span
              style={{ cursor: 's-resize', width: "100%", display: "block" }}
              onClick={() => sortCallback(c, 'desc')}
            ><SortAscendingOutlined /> {c.title}</span>
          </Space>
        );
      })}
  </Space>);
 
  const result = (
    (data == null) ?
      emptyElem('Please search above') : ((data?.length === 0) ?
        emptyElem('No record found') : (
          <List
            style={{ height }}
            grid={{
              gutter: 16,
              xs: 1,
              sm: 1,
              md: 1,
              lg: 1,
              xl: 1,
              xxl: 1,
            }}
            dataSource={data}
            renderItem={item => (
              <List.Item key={item.id}>
                <CardSearchResultCell
                  zIndex={zIndex}
                  searchKeyword={searchKeyword}
                  changeCallback={(record, selected) => {
                    if (selected) {
                      selectedData.push(record);
                    } else {
                      selectedData.splice(selectedData.indexOf(record, 1));
                    }
                    setSelectedData(selectedData);
                    changeCallback(record, selected);
                  }}
                  updateFormId={updateFormId}
                  record={item}
                  domainName={domainName}
                  columns={columns}
                  contentDisplayColumns={contentDisplayColumns}
                  panelDisplayColumns={panelDisplayColumns}
                  key={item.id}
                />
              </List.Item>
            )}
          />
        ))
  );
 
  const refreshTableIcon = (
    <ReloadOutlined
      title={t("Refresh data table")}
      className="link-icon refresh-icon"
      onClick={() => onSearch(searchKeyword, {}, additionalConditions)}
    />
  );
 
  const createIcon = (
    <CreateAction
      initShowCreateModal={initShowCreateModal}
      domainName={domainName}
      ownerId={ownerIdsState?.[ownerIdsState?.length - 1]}
      ownerClass={ownerClass}
      callback={(): void => onSearch(searchKeyword, {}, additionalConditions)}
      zIndex={zIndex + 1}
      showIconAndText={true}
    />
  );
 
  const [visiblePopover, setVisiblePopover] = useState<string>();
  const actionMode = (selectedData.length > 0) ? "multiple" : "class";
  const actionButtons = (<span className="action-icons">
    <ActionButtons
      visiblePopover={visiblePopover}
      setVisiblePopoverCallback={(key?: string) => setVisiblePopover(key)}
      domainName={domainName}
      fetchDataCallback={() => fetchDataCallback(data ?? [])}
      mode={actionMode}
      selectedData={selectedData}
      zIndex={zIndex}
    />
  </span>);
 
  const hasCondition = (additionalConditions != null) && (Object.keys(additionalConditions).length > 0)
    || (searchKeyword != null && searchKeyword != '')
    || (sortField != null && sortDirection != null)
    || (ownerIdsState != null && ownerIdsState.length > 0
      && ownerClassColumnNameState != null && ownerClassColumnNameState.length > 0
      && ownerClass != null);
 
  const saveFilterButton = (
    <span className="save-filter-icon">
      {hasCondition ? (<SaveDynamicFilterPopover
        fullTextSearchConditions={{
          domainName,
          searchConditions: { ...additionalConditions, ...formExtInfo?.extInfo?.conditions },
          fullTextConditions: prepareFullTextSearchCondition(),
          max: pageSize,
          current: 0,
          isFullTextSearch: true,
          sortDirection: sortDirection,
          // eslint-disable-next-line react/prop-types
          sortField: sortField?.key,
          ownerClass,
          ownerIds: ownerIdsState,
          ownerClassColumnName: ownerClassColumnNameState,
          keyword: searchKeyword,
        }}
        domainName={domainName}
        zIndex={zIndex + 1}
        updateCallback={() => refresh()}
        displayElement={<Button
          icon={<SaveOutlined title={t("Save filter")} style={{ cursor: "pointer", marginRight: "-4px" }} />}
          size="small"
          type="link"
        >{t('Create dynamic filter')}</Button>}
      />) : <></>}
    </span>
  );
 
  const searchPanel = (
    <div className='search-box-container'>
      <Space direction="vertical" size={"small"} style={{ width: "100%" }}>
        <span>
          <Search
            autoFocus={true}
            loading={loading}
            placeholder={t("Input search condition")}
            allowClear
            enterButton={t("Search")}
            size="large"
            onSearch={(value: string) => {
              //如果之前的搜索条件不为空,输入框中的新的搜索条件为空
              //表示是清空搜索框,这种情况下,不发送搜索请求
              if (isNotBlank(searchKeyword) && isBlank(value)) {
                setSearchKeyword('');
                return;
              }
              onSearch(value, {}, additionalConditions);
            }}
            onChange={(e) => {
              setSearchKeyword(e.target.value);
            }}
            value={searchKeyword}
          />
          {createIcon}
          {refreshTableIcon}
          {selectedData?.length > 0 && actionButtons}
        </span>
        <Space direction="horizontal" size="small">
          <div>
            <span>{t('Other filter conditions')}</span>
            <span>{elements}</span>
          </div>
          <div>
            <span>{t('Result sorting')}</span>
            <Popover
              content={sortContent}
              placement="bottom"
              trigger="click"
            >
              <Tag className='search-condition-tag'>
                {getSortLabel()}
              </Tag>
            </Popover>
          </div>
          <div style={{ paddingTop: "4px" }}>
            {saveFilterButton}
          </div>
        </Space>
        <PredefinedFilters
          key={refreshPredefinedFilters}
          domainName={domainName}
          fetchDataCallback={emptyMethod}
          fullTextFetchDataCallback={(conditions) => {
            setSearchKeyword(conditions?.keyword ?? "");
            setSortDirection(conditions?.sortDirection ?? undefined);
            setSortField(conditions?.sortField ?
              columns.find((column) => column.key === conditions.sortField) : undefined);
            setAdditionalConditions(conditions?.searchConditions ?? {});
            setOwnerIds?.(conditions?.ownerIds ?? []);
            setOwnerIdsState(conditions?.ownerIds ?? []);
            setOwnerClassColumnNameState(conditions?.ownerClassColumnName ?? "");
          }}
          columns={columns}
          displayStyle="inline"
          zIndex={zIndex}
          fullTextSearchConditions={{
            domainName,
            searchConditions: { ...additionalConditions, ...formExtInfo?.extInfo?.conditions },
            fullTextConditions: prepareFullTextSearchCondition(),
            max: pageSize,
            current: 0,
            isFullTextSearch: true,
            sortDirection: sortDirection,
            // eslint-disable-next-line react/prop-types
            sortField: sortField?.key,
            ownerClass,
            ownerIds: ownerIdsState,
            ownerClassColumnName: ownerClassColumnNameState,
            keyword: searchKeyword,
          }}
        />
      </Space>
    </div>
  );
 
  const pagination = (
    <Pagination
      {...getPagination({ current, total, pageSize })}
      hideOnSinglePage={false}
      onChange={(page: number, pageSize?: number) => {
        setCurrent(page);
        setPageSize(pageSize ?? FullTextSearchDefaultPageSize);
        onSearch(searchKeyword, { newCurrent: page, newPageSize: pageSize }, additionalConditions);
      }}
      onShowSizeChange={(current: number, pageSize: number) => {
        setCurrent(current);
        setPageSize(pageSize);
        onSearch(searchKeyword, {
          newCurrent: current,
          newPageSize: pageSize
        }, additionalConditions);
      }}
      responsive={true}
    />
  );
 
  const hasData = (data != null && data.length > 0);
  const addrClassName = hasData ? '' : 'full-text-search-panel-container-no-data';
  return (
    <div
      className={`full-text-search-panel-container ${addrClassName}`}
    >
      <Space
        direction="vertical"
        size={4}
      >
        {searchPanel}
        {result}
        {hasData && pagination}
      </Space>
    </div>
  );
};
 
export default FullTextSearchPanel;