Skip to content

Commit b6a7d74

Browse files
committed
[MNT-25412] Proper date filter clean up after reset event
1 parent 86093ad commit b6a7d74

File tree

6 files changed

+62
-19
lines changed

6 files changed

+62
-19
lines changed

docs/content-services/components/search-date-range.component.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ before that release, please ensure that your component configuration is updated
2525

2626
### Properties
2727

28-
| Name | Type | Description |
28+
| Name | Type | Description |
2929
|--------------|-----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
30-
| field | string | Field to apply the query to. Required value |
31-
| maxDate | string | A fixed date (default format: dd-MMM-yy) or the string `"today"` that will set the maximum searchable date. Default is today. |
32-
| dateFormat | string | Date format. Dates used by the datepicker are Javascript Date objects, using [date-fns](https://date-fns.org/v2.30.0/docs/format) for formatting, so you can use any date format supported by the library. Default is 'dd-MMM-yy (sample date - 07-Jun-23) |
33-
| initialValue | SearchDateRange | Initial value for the component |
30+
| field | string | Field to apply the query to. Required value |
31+
| maxDate | string | A fixed date (default format: dd-MMM-yy) or the string `"today"` that will set the maximum searchable date. Default is today. |
32+
| dateFormat | string | Date format. Dates used by the datepicker are Javascript Date objects, using [date-fns](https://date-fns.org/v2.30.0/docs/format) for formatting, so you can use any date format supported by the library. Default is 'dd-MMM-yy (sample date - 07-Jun-23) |
33+
| initialValue | SearchDateRange | Initial value for the component |
34+
| onReset$ | [`Observable`](http://reactivex.io/documentation/observable.html)`<void>` | Reset event observable |
3435

3536
### Events
3637

lib/content-services/src/lib/search/components/search-date-range-tabbed/search-date-range-tabbed.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[maxDate]="settings.maxDate"
77
[field]="field"
88
[initialValue]="preselectedValues[field]"
9+
[onReset$]="reset$"
910
(changed)="onDateRangedValueChanged($event, field)"
1011
(valid)="tabsValidity[field]=$event" />
1112
</ng-container>

lib/content-services/src/lib/search/components/search-date-range-tabbed/search-date-range-tabbed.component.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,20 @@ describe('SearchDateRangeTabbedComponent', () => {
225225
expect(component.context.update).toHaveBeenCalled();
226226
});
227227

228-
it('should clear values and search filter when widget is reset', () => {
228+
it('should clear values and search filter when widget is reset', (done) => {
229229
spyOn(component.displayValue$, 'next');
230+
component.reset$.subscribe(() => {
231+
expect(component.combinedQuery).toBe('');
232+
expect(component.combinedDisplayValue).toBe('');
233+
expect(component.displayValue$.next).toHaveBeenCalledWith('');
234+
expect(component.context.queryFragments['dateRange']).toEqual('');
235+
expect(component.context.update).toHaveBeenCalled();
236+
component.fields.forEach((field) => expect(component.context.filterRawParams[field]).toBeUndefined());
237+
done();
238+
});
239+
230240
component.reset();
231241
fixture.detectChanges();
232-
expect(component.combinedQuery).toBe('');
233-
expect(component.combinedDisplayValue).toBe('');
234-
expect(component.displayValue$.next).toHaveBeenCalledWith('');
235-
expect(component.context.queryFragments['dateRange']).toEqual('');
236-
expect(component.context.update).toHaveBeenCalled();
237-
component.fields.forEach((field) => expect(component.context.filterRawParams[field]).toBeUndefined());
238242
});
239243

240244
it('should populate filter state when populate filters event has been observed', () => {

lib/content-services/src/lib/search/components/search-date-range-tabbed/search-date-range-tabbed.component.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { Component, DestroyRef, inject, OnInit, ViewEncapsulation } from '@angular/core';
19-
import { ReplaySubject } from 'rxjs';
19+
import { ReplaySubject, Subject } from 'rxjs';
2020
import { map } from 'rxjs/operators';
2121
import { DateRangeType } from './search-date-range/date-range-type';
2222
import { SearchDateRange } from './search-date-range/search-date-range';
@@ -42,6 +42,13 @@ const DEFAULT_DATE_DISPLAY_FORMAT = 'dd-MMM-yy';
4242
encapsulation: ViewEncapsulation.None
4343
})
4444
export class SearchDateRangeTabbedComponent implements SearchWidget, OnInit {
45+
private value: { [key: string]: Partial<SearchDateRange> } = {};
46+
private queryMapByField: Map<string, string> = new Map<string, string>();
47+
private displayValueMapByField: Map<string, string> = new Map<string, string>();
48+
49+
private readonly destroyRef = inject(DestroyRef);
50+
private readonly resetSubject$ = new Subject<void>();
51+
4552
displayValue$ = new ReplaySubject<string>(1);
4653
id: string;
4754
startValue: SearchDateRange = {
@@ -58,12 +65,7 @@ export class SearchDateRangeTabbedComponent implements SearchWidget, OnInit {
5865
tabsValidity: { [key: string]: boolean } = {};
5966
combinedQuery: string;
6067
combinedDisplayValue: string;
61-
62-
private value: { [key: string]: Partial<SearchDateRange> } = {};
63-
private queryMapByField: Map<string, string> = new Map<string, string>();
64-
private displayValueMapByField: Map<string, string> = new Map<string, string>();
65-
66-
private readonly destroyRef = inject(DestroyRef);
68+
reset$ = this.resetSubject$.asObservable();
6769

6870
constructor(private translateService: TranslationService) {}
6971

@@ -121,6 +123,7 @@ export class SearchDateRangeTabbedComponent implements SearchWidget, OnInit {
121123
this.context.queryFragments[this.id] = undefined;
122124
this.context.filterRawParams[this.id] = undefined;
123125
this.submitValues(updateContext);
126+
this.resetSubject$.next();
124127
}
125128

126129
setValue(value: { [key: string]: SearchDateRange }) {

lib/content-services/src/lib/search/components/search-date-range-tabbed/search-date-range/search-date-range.component.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { HarnessLoader } from '@angular/cdk/testing';
2525
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
2626
import { MatCalendarHarness, MatDatepickerToggleHarness } from '@angular/material/datepicker/testing';
2727
import { MatRadioButtonHarness } from '@angular/material/radio/testing';
28+
import { Subject } from 'rxjs';
2829

2930
describe('SearchDateRangeComponent', () => {
3031
let component: SearchDateRangeComponent;
@@ -33,6 +34,7 @@ describe('SearchDateRangeComponent', () => {
3334

3435
const startDateSampleValue = parse('05-Jun-23', 'dd-MMM-yy', new Date());
3536
const endDateSampleValue = parse('07-Jun-23', 'dd-MMM-yy', new Date());
37+
const resetSubject = new Subject<void>();
3638

3739
beforeEach(() => {
3840
TestBed.configureTestingModule({
@@ -51,6 +53,7 @@ describe('SearchDateRangeComponent', () => {
5153
betweenStartDate: null,
5254
betweenEndDate: null
5355
});
56+
component.onReset$ = resetSubject.asObservable();
5457
loader = TestbedHarnessEnvironment.documentRootLoader(fixture);
5558
fixture.detectChanges();
5659
});
@@ -296,4 +299,20 @@ describe('SearchDateRangeComponent', () => {
296299
fixture.detectChanges();
297300
expect(component.changed.emit).toHaveBeenCalledWith(value);
298301
});
302+
303+
it('should reset the form when onReset event is received', () => {
304+
spyOn(component, 'reset').and.callThrough();
305+
component.form.controls.dateRangeType.setValue(component.DateRangeType.BETWEEN);
306+
component.form.controls.betweenStartDate.setValue(startDateSampleValue);
307+
component.form.controls.betweenEndDate.setValue(endDateSampleValue);
308+
fixture.detectChanges();
309+
310+
resetSubject.next();
311+
fixture.detectChanges();
312+
313+
expect(component.form.controls.dateRangeType.value).toEqual(component.DateRangeType.ANY);
314+
expect(component.form.controls.betweenStartDate.value).toBeNull();
315+
expect(component.form.controls.betweenEndDate.value).toBeNull();
316+
expect(component.reset).toHaveBeenCalled();
317+
});
299318
});

lib/content-services/src/lib/search/components/search-date-range-tabbed/search-date-range/search-date-range.component.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { MatInputModule } from '@angular/material/input';
3232
import { MatSelectModule } from '@angular/material/select';
3333
import { MatDatepickerModule } from '@angular/material/datepicker';
3434
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
35+
import { Observable } from 'rxjs';
3536

3637
const DEFAULT_DATE_DISPLAY_FORMAT = 'dd-MMM-yy';
3738

@@ -70,6 +71,9 @@ export class SearchDateRangeComponent implements OnInit {
7071
}
7172
}
7273

74+
@Input()
75+
onReset$: Observable<void>;
76+
7377
@Output()
7478
changed = new EventEmitter<Partial<SearchDateRange>>();
7579
@Output()
@@ -118,6 +122,7 @@ export class SearchDateRangeComponent implements OnInit {
118122
.pipe(takeUntilDestroyed(this.destroyRef))
119123
.subscribe((dateRangeType) => this.updateValidators(dateRangeType));
120124
this.form.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => this.onChange());
125+
this.onReset$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(() => this.reset());
121126
}
122127
private updateValidators(dateRangeType: DateRangeType) {
123128
switch (dateRangeType) {
@@ -189,4 +194,14 @@ export class SearchDateRangeComponent implements OnInit {
189194
return true;
190195
}
191196
}
197+
198+
reset(): void {
199+
this.form.reset({
200+
dateRangeType: DateRangeType.ANY,
201+
inLastValueType: InLastDateType.DAYS,
202+
inLastValue: undefined,
203+
betweenStartDate: undefined,
204+
betweenEndDate: undefined
205+
});
206+
}
192207
}

0 commit comments

Comments
 (0)