mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-28 22:42:11 +01:00
Merged PR 1850: feat(libs-shared-filter): improve date range equality for default filter inpu...
feat(libs-shared-filter): improve date range equality for default filter input detection Enhance the isDefaultFilterInput method to compare DateRangeFilterInput values by parsing ISO date strings to Date objects before comparison. This ensures that date ranges are considered equal even if their string representations differ in precision (e.g., "2023-06-05T22:00:00Z" vs. "2023-06-05T22:00:00.000Z"). This change improves filter reset and default state detection reliability for date range filters. Ref: #5142
This commit is contained in:
committed by
Lorenz Hilpert
parent
7283caab15
commit
9857d86bdf
@@ -4,6 +4,7 @@ import { getState, patchState, signalState } from '@ngrx/signals';
|
||||
import { filterMapping, mapFilterInputToRecord } from './mappings';
|
||||
import { isEqual } from 'lodash';
|
||||
import {
|
||||
DateRangeFilterInput,
|
||||
FilterInput,
|
||||
OrderByDirection,
|
||||
OrderByDirectionSchema,
|
||||
@@ -12,6 +13,7 @@ import {
|
||||
} from './schemas';
|
||||
import { FILTER_ON_COMMIT, FILTER_ON_INIT, QUERY_SETTINGS } from './tokens';
|
||||
import { logger } from '@isa/core/logging';
|
||||
import { parseISO } from 'date-fns';
|
||||
|
||||
@Injectable()
|
||||
export class FilterService {
|
||||
@@ -211,9 +213,47 @@ export class FilterService {
|
||||
(i) => i.key === filterInput.key,
|
||||
);
|
||||
|
||||
// For DateRange inputs, compare dates by value, not string precision
|
||||
if (filterInput.type === InputType.DateRange) {
|
||||
return this._areDateRangeInputsEqual(
|
||||
currentInputState as DateRangeFilterInput,
|
||||
defaultInputState as DateRangeFilterInput,
|
||||
);
|
||||
}
|
||||
|
||||
return isEqual(currentInputState, defaultInputState);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two date range filter inputs for equality, accounting for possible differences
|
||||
* in date string precision (e.g., "2023-06-05T22:00:00Z" vs. "2023-06-05T22:00:00.000Z").
|
||||
* Converts ISO date strings to Date objects before comparison to ensure consistent results.
|
||||
*
|
||||
* @param currentInput - The current DateRangeFilterInput state
|
||||
* @param defaultInput - The default DateRangeFilterInput state
|
||||
* @returns True if both start and stop dates are equal (ignoring string precision), false otherwise
|
||||
*/
|
||||
private _areDateRangeInputsEqual = (
|
||||
currentInput: DateRangeFilterInput | undefined,
|
||||
defaultInput: DateRangeFilterInput | undefined,
|
||||
): boolean => {
|
||||
const currentStart = currentInput?.start;
|
||||
const defaultStart = defaultInput?.start;
|
||||
const currentStop = currentInput?.stop;
|
||||
const defaultStop = defaultInput?.stop;
|
||||
|
||||
return (
|
||||
isEqual(
|
||||
currentStart ? parseISO(currentStart) : currentStart,
|
||||
defaultStart ? parseISO(defaultStart) : defaultStart,
|
||||
) &&
|
||||
isEqual(
|
||||
currentStop ? parseISO(currentStop) : currentStop,
|
||||
defaultStop ? parseISO(defaultStop) : defaultStop,
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* Computes the number of filter inputs that are currently selected (i.e., not in their default state).
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user