From 6bf370b25f842ec9dfaf8300f76d336336ebee94 Mon Sep 17 00:00:00 2001 From: Shekar Siri Date: Tue, 8 Apr 2025 12:40:22 +0200 Subject: [PATCH] feat(filterItem): improve value handling in toJson method Enhanced the method in to handle various cases for the property more robustly. Added checks for empty or invalid values and adjusted mapping logic for specific filter types like and . - Updated type annotations for better clarity. - Removed unnecessary trailing commas in imports. - Improved readability and maintainability of the code. --- frontend/app/mstore/types/filterItem.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/frontend/app/mstore/types/filterItem.ts b/frontend/app/mstore/types/filterItem.ts index 6082aae4b..b6ae1214b 100644 --- a/frontend/app/mstore/types/filterItem.ts +++ b/frontend/app/mstore/types/filterItem.ts @@ -2,7 +2,7 @@ import { FilterCategory, FilterKey, FilterType } from 'Types/filter/filterType'; import { conditionalFiltersMap, filtersMap, - mobileConditionalFiltersMap, + mobileConditionalFiltersMap } from 'Types/filter/newFilter'; import { action, makeAutoObservable, observable } from 'mobx'; @@ -35,7 +35,7 @@ export default class FilterItem { makeAutoObservable(this); if (Array.isArray(data.filters)) { - data.filters = data.filters.map(function (i: Record) { + data.filters = data.filters.map(function(i: Record) { return new FilterItem(i); }); } @@ -132,22 +132,29 @@ export default class FilterItem { return this; } - toJson(): any { + toJson(): Record { const isMetadata = this.category === FilterCategory.METADATA; - const json = { + const json: Record = { type: isMetadata ? FilterKey.METADATA : this.key, isEvent: Boolean(this.isEvent), - value: this.value.map((i: any) => i ? i.toString() : ''), operator: this.operator, source: isMetadata ? this.key.replace(/^_/, '') : this.source, sourceOperator: this.sourceOperator, filters: Array.isArray(this.filters) ? this.filters.map((i) => i.toJson()) - : [], + : [] }; - if (this.type === FilterKey.DURATION) { + + if (!this.value || !Array.isArray(this.value) || this.value.length === 0 || this.value[0] === '') { + json.value = []; + } else if (this.type === FilterKey.DURATION) { json.value = this.value.map((i: any) => (!i ? 0 : i)); + } else if (this.type === FilterKey.FETCH_DURATION) { + json.value = this.value.map((i: any) => (!i ? 0 : String(i))); + } else { + json.value = this.value.map((i: any) => i ? String(i) : ''); } + return json; } }