Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
4.5 kB
2
Indexable
Never
import React, { useCallback } from 'react';

import { ReactiveList } from '@appbaseio/reactivesearch';
import { debounce, throttle } from 'lodash';
import { shallowEqual, useSelector } from 'react-redux';

import { ElasticSearch } from '@/components';
import { IOpportunityEntitiesResponse } from '@/services/opportunity-entities';
import { IJiraProject } from '@/services/project-entities';
import { ConfigESIndex } from '@/store/config/selector';
import { SelectWorkspaceView } from '@/store/workspace/selectors';

interface IESTableSyncProps {
  event?: IOpportunityEntitiesResponse;
  setListDataIssue?: React.Dispatch<React.SetStateAction<never[]>>;
  projectSelected?: IJiraProject | undefined;
  typeJiraSelected?: any;
  //   setCommentFirstLoading: React.Dispatch<React.SetStateAction<never[]>>;
  //   lastRefresh: number;
  refreshData: () => void;
}

const TableSyncES = ({
  event,
  setListDataIssue,
  projectSelected,
  typeJiraSelected,
  refreshData,
}: IESTableSyncProps) => {
  const selectedWorkspace = useSelector(SelectWorkspaceView);
  const ES_INDICES = useSelector(ConfigESIndex, shallowEqual);
  const workspaceLocal = JSON.parse(localStorage.getItem('workspace-id') ?? '');

  const defaultQuery = useCallback(() => {
    const shouldQueries = typeJiraSelected
      .filter((item: any) => item !== '1')
      .map((item: any) => ({
        bool: {
          must: [
            {
              match: {
                'jira_issue.issue_type.id.keyword': item,
              },
            },
          ],
        },
      }));

    console.log('@@@ shouldQueries', shouldQueries);

    return {
      query: {
        bool: {
          must: [
            {
              term: {
                'workspace.id': selectedWorkspace?.id ?? Number(workspaceLocal) ?? 1,
              },
            },
            // {
            //   nested: {
            //     path: 'jira_issue',
            //     query: {
            //       bool: {
            //         must: [
            //           {
            //             match: {
            //               'jira_issue.project_id': projectSelected?.project_id ?? 1,
            //             },
            //           },
            //           {
            //             bool: {
            //               should: shouldQueries,
            //             },
            //           },
            //         ],
            //       },
            //     },
            //     inner_hits: {
            //       highlight: {
            //         fields: {
            //           jira_issue: {},
            //         },
            //       },
            //     },
            //   },
            // },
            {
              nested: {
                path: 'jira_issue',
                query: {
                  match: {
                    'jira_issue.project_id': '10014',
                  },
                },
                inner_hits:{},
              },
            },
            {
              nested: {
                path: 'jira_issue',
                query: {
                  match: {
                    'jira_issue.project_id': '10016',
                  },
                },
                inner_hits:{},
              },
            },
          ],
          filter: {
            term: {
              del_flg: false,
            },
          },
        },
      },
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [selectedWorkspace, workspaceLocal, projectSelected, typeJiraSelected]);

  return (
    <ElasticSearch index={ES_INDICES.project_entity_index_name}>
      <ReactiveList
        componentId="ListOpportunity"
        dataField="last_updated_at"
        // sortBy={order}
        renderResultStats={() => null}
        renderNoResults={() => <></>}
        defaultQuery={throttle(defaultQuery, 500)}
        size={10000}
        infiniteScroll={false}
        scrollOnChange={false}
        loader={<div />}
        onData={({ data }) => {
          let issues: any[] = [];
          data?.forEach((item: any) => issues.push(...item?.inner_hits?.jira_issue?.hits?.hits));
          // eslint-disable-next-line no-underscore-dangle
          issues = issues.map((issue: any) => ({ ...issue?._source }));
          setListDataIssue?.(issues as any);
          console.log('@@@', { data, issues });
          return data;
        }}
        render={() => <></>}
      />
    </ElasticSearch>
  );
};
export default TableSyncES;