|
| 1 | +import React, { useEffect, useState } from 'react'; |
| 2 | +import { LabelModel, TableListModel } from '../../../api'; |
| 3 | +import { LabelsApiClient, SearchApiClient } from '../../../services/apiClient'; |
| 4 | +import { CheckTypes } from '../../../shared/routes'; |
| 5 | +import { useDecodedParams } from '../../../utils'; |
| 6 | +import Button from '../../Button'; |
| 7 | +import Input from '../../Input'; |
| 8 | +import TableList from '../../TableList'; |
| 9 | +type TLabel = LabelModel & { clicked: boolean }; |
| 10 | +type TTableWithSchema = TableListModel & { schema?: string }; |
| 11 | +export default function ConnectionTables() { |
| 12 | + const { |
| 13 | + checkTypes, |
| 14 | + connection |
| 15 | + }: { |
| 16 | + checkTypes: CheckTypes; |
| 17 | + connection: string; |
| 18 | + } = useDecodedParams(); |
| 19 | + const [tables, setTables] = useState<TableListModel[]>([]); |
| 20 | + const [filters, setFilters] = useState<any>({ |
| 21 | + page: 1, |
| 22 | + limit: 50, |
| 23 | + checkType: checkTypes |
| 24 | + }); |
| 25 | + const [table, setTable] = useState(''); |
| 26 | + const [schema, setSchema] = useState(''); |
| 27 | + const [labels, setLabels] = useState<TLabel[]>([]); |
| 28 | + |
| 29 | + const onChangeFilters = (obj: Partial<any>) => { |
| 30 | + setFilters((prev: any) => ({ |
| 31 | + ...prev, |
| 32 | + ...obj |
| 33 | + })); |
| 34 | + }; |
| 35 | + |
| 36 | + const onChangeLabels = (index: number) => { |
| 37 | + const arr = [...labels]; |
| 38 | + arr[index] = { ...arr[index], clicked: !arr[index].clicked }; |
| 39 | + |
| 40 | + const filteredlabels: string[] = arr |
| 41 | + .filter((x) => x.clicked && x.label) |
| 42 | + .map((x) => x.label) |
| 43 | + .filter((x): x is string => typeof x === 'string'); |
| 44 | + |
| 45 | + getTables(filteredlabels); |
| 46 | + setLabels(arr); |
| 47 | + }; |
| 48 | + |
| 49 | + const getTables = async (labels: string[] = []) => { |
| 50 | + const addPrefix = (str: string) => { |
| 51 | + return str.includes('*') || str.length === 0 ? str : '*' + str + '*'; |
| 52 | + }; |
| 53 | + return SearchApiClient.findTables( |
| 54 | + connection, |
| 55 | + addPrefix(schema), |
| 56 | + addPrefix(table), |
| 57 | + labels, |
| 58 | + filters.page, |
| 59 | + filters.pageSize, |
| 60 | + filters.checkType |
| 61 | + ).then((res) => { |
| 62 | + const arr: TTableWithSchema[] = []; |
| 63 | + res.data.forEach((item) => { |
| 64 | + const jItem = { ...item, schema: item.target?.schema_name }; |
| 65 | + arr.push(jItem); |
| 66 | + }); |
| 67 | + setTables(arr); |
| 68 | + return arr; |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + const refetchTables = (tables?: TableListModel[]) => { |
| 73 | + tables?.forEach((table) => { |
| 74 | + if (!table?.data_quality_status) { |
| 75 | + setTimeout(() => { |
| 76 | + getTables(); |
| 77 | + }, 5000); |
| 78 | + } |
| 79 | + }); |
| 80 | + }; |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + getTables().then((res) => { |
| 84 | + refetchTables(res); |
| 85 | + }); |
| 86 | + const getLabels = () => { |
| 87 | + LabelsApiClient.getAllLabelsForTables().then((res) => { |
| 88 | + const array: TLabel[] = res.data.map((item) => { |
| 89 | + return { ...item, clicked: false }; |
| 90 | + }); |
| 91 | + setLabels(array); |
| 92 | + }); |
| 93 | + }; |
| 94 | + getLabels(); |
| 95 | + }, [connection, filters]); |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + <div className="flex items-center gap-x-4 mb-4 mt-2 px-4"> |
| 100 | + <Input |
| 101 | + label="Schema name" |
| 102 | + value={schema} |
| 103 | + onChange={(e) => setSchema(e.target.value)} |
| 104 | + /> |
| 105 | + <Input |
| 106 | + label="Table name" |
| 107 | + value={table} |
| 108 | + onChange={(e) => setTable(e.target.value)} |
| 109 | + /> |
| 110 | + <Button |
| 111 | + label="Search" |
| 112 | + onClick={() => |
| 113 | + getTables( |
| 114 | + labels |
| 115 | + .filter((x) => x.clicked && x.label) |
| 116 | + .map((x) => x.label) |
| 117 | + .filter((x): x is string => typeof x === 'string') |
| 118 | + ) |
| 119 | + } |
| 120 | + color="primary" |
| 121 | + className="mt-5" |
| 122 | + /> |
| 123 | + </div> |
| 124 | + <TableList |
| 125 | + tables={tables} |
| 126 | + setTables={setTables} |
| 127 | + filters={filters} |
| 128 | + onChangeFilters={onChangeFilters} |
| 129 | + labels={labels} |
| 130 | + onChangeLabels={onChangeLabels} |
| 131 | + /> |
| 132 | + </> |
| 133 | + ); |
| 134 | +} |
0 commit comments