Skip to content

Commit

Permalink
refactor: Simplify arrow function parameters and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
guidomodarelli committed Sep 11, 2024
1 parent 4ee23c7 commit e15982c
Showing 1 changed file with 20 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { tDataGridColumn } from './use-data-grid';
import { getCellActions } from './get-cell-actions';
import { FILTER_OPERATOR, PatternDataSourceFilterManager } from '../data-source';

export const parseData = (
resultsHits: SearchResponse['hits']['hits'],
): any[] => {
const data = resultsHits.map(hit => {
export const parseData = (resultsHits: SearchResponse['hits']['hits']): any[] => {
const data = resultsHits.map((hit) => {
if (!hit) {
return {};
}
Expand All @@ -28,20 +26,15 @@ export const parseData = (
return data;
};

export const getFieldFormatted = (
rowIndex,
columnId,
indexPattern,
rowsParsed,
) => {
const field = indexPattern.fields.find(field => field.name === columnId);
export const getFieldFormatted = (rowIndex, columnId, indexPattern, rowsParsed) => {
const field = indexPattern.fields.find((field) => field.name === columnId);
let fieldValue = null;
if (columnId.includes('.')) {
// when the column is a nested field. The column could have 2 to n levels
// get dinamically the value of the nested field
const nestedFields = columnId.split('.');
fieldValue = rowsParsed[rowIndex];
nestedFields.forEach(field => {
nestedFields.forEach((field) => {
if (fieldValue) {
fieldValue = fieldValue[field];
}
Expand Down Expand Up @@ -74,25 +67,14 @@ export const getFieldFormatted = (
};

// receive search params
export const exportSearchToCSV = async (
params: SearchParams,
): Promise<void> => {
export const exportSearchToCSV = async (params: SearchParams): Promise<void> => {
const DEFAULT_MAX_SIZE_PER_CALL = 1000;
const {
indexPattern,
filters = [],
query,
sorting,
fields,
pagination,
} = params;
const { indexPattern, filters = [], query, sorting, fields, pagination } = params;
// when the pageSize is greater than the default max size per call (10000)
// then we need to paginate the search
const mustPaginateSearch =
pagination?.pageSize && pagination?.pageSize > DEFAULT_MAX_SIZE_PER_CALL;
const pageSize = mustPaginateSearch
? DEFAULT_MAX_SIZE_PER_CALL
: pagination?.pageSize;
const pageSize = mustPaginateSearch ? DEFAULT_MAX_SIZE_PER_CALL : pagination?.pageSize;
const totalHits = pagination?.pageSize || DEFAULT_MAX_SIZE_PER_CALL;
let pageIndex = params.pagination?.pageIndex || 0;
let hitsCount = 0;
Expand Down Expand Up @@ -123,13 +105,13 @@ export const exportSearchToCSV = async (
}

const resultsFields = fields;
const data = allHits.map(hit => {
const data = allHits.map((hit) => {
// check if the field type is a date
const dateFields = indexPattern.fields.getByType('date');
const dateFieldsNames = dateFields.map(field => field.name);
const dateFieldsNames = dateFields.map((field) => field.name);
const flattenHit = indexPattern.flattenHit(hit);
// replace the date fields with the formatted date
dateFieldsNames.forEach(field => {
dateFieldsNames.forEach((field) => {
if (flattenHit[field]) {
flattenHit[field] = beautifyDate(flattenHit[field]);
}
Expand All @@ -144,8 +126,8 @@ export const exportSearchToCSV = async (
if (!data || data.length === 0) return;

const parsedData = data
.map(row => {
const parsedRow = resultsFields?.map(field => {
.map((row) => {
const parsedRow = resultsFields?.map((field) => {
const value = row[field];
if (value === undefined || value === null) {
return '';
Expand Down Expand Up @@ -185,11 +167,9 @@ export const parseColumns = (
}

const columns = fields
.filter(field => field.name !== '_source')
.map(field => {
const defaultColumn = defaultColumns.find(
column => column.id === field.name,
);
.filter((field) => field.name !== '_source')
.map((field) => {
const defaultColumn = defaultColumns.find((column) => column.id === field.name);
return {
...field,
id: field.name,
Expand Down Expand Up @@ -231,12 +211,12 @@ export const parseColumns = (
*/
export const getAllCustomRenders = (
customColumns: tDataGridColumn[],
discoverColumns: tDataGridColumn[],
discoverColumns: tDataGridColumn[]
): tDataGridColumn[] => {
const customColumnsWithRender = customColumns.filter(column => column.render);
const allColumns = discoverColumns.map(column => {
const customColumnsWithRender = customColumns.filter((column) => column.render);
const allColumns = discoverColumns.map((column) => {
const customColumn = customColumnsWithRender.find(
customColumn => customColumn.id === column.id,
(customColumn) => customColumn.id === column.id
);
return customColumn || column;
});
Expand Down

0 comments on commit e15982c

Please sign in to comment.