Skip to content

Commit

Permalink
merge utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jcger committed Nov 21, 2023
1 parent 8931e00 commit 8ab1626
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions x-pack/plugins/cases/public/components/all_cases/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import { difference } from 'lodash';
import type { ParsedUrlQueryParams, PartialQueryParams } from '../../../../common/ui/types';
import type { CasesColumnSelection } from '../types';
import type { CasesColumnsConfiguration } from '../use_cases_columns_configuration';

export const parseUrlQueryParams = (parsedUrlParams: ParsedUrlQueryParams): PartialQueryParams => {
const urlParams: PartialQueryParams = {
Expand All @@ -28,3 +31,45 @@ export const parseUrlQueryParams = (parsedUrlParams: ParsedUrlQueryParams): Part

return urlParams;
};

export const mergeSelectedColumnsWithConfiguration = ({
selectedColumns,
casesColumnsConfig,
}: {
selectedColumns: CasesColumnSelection[];
casesColumnsConfig: CasesColumnsConfiguration;
}): CasesColumnSelection[] => {
const result = selectedColumns.reduce((accumulator, { field, isChecked }) => {
if (
field in casesColumnsConfig &&
casesColumnsConfig[field].field !== '' &&
casesColumnsConfig[field].canDisplay
) {
accumulator.push({
field: casesColumnsConfig[field].field,
name: casesColumnsConfig[field].name,
isChecked,
});
}
return accumulator;
}, [] as CasesColumnSelection[]);

// This will include any new customFields and/or changes to the case attributes
const missingColumns = difference(
Object.keys(casesColumnsConfig),
selectedColumns.map(({ field }) => field)
);

missingColumns.forEach((field) => {
// can be an empty string
if (casesColumnsConfig[field].field && casesColumnsConfig[field].canDisplay) {
result.push({
field: casesColumnsConfig[field].field,
name: casesColumnsConfig[field].name,
isChecked: casesColumnsConfig[field].isCheckedDefault,
});
}
});

return result;
};

0 comments on commit 8ab1626

Please sign in to comment.