Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: TTA-90 Modify the date range selection module
  • Loading branch information
jimmyjaramillo committed Aug 16, 2022
commit 4223b7dfe35d6eea1edbef66576a11b26fecc41e
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<label class="col-form-label my-1">Select your Date Range:</label>
<mat-form-field appearance="fill">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [formGroup]="range" [rangePicker]="picker">
<input matStartDate formControlName="start" placeholder="Start date">
<input matEndDate formControlName="end" placeholder="End date">
<mat-date-range-input [formGroup]="range" [rangePicker]="picker" >
<input matStartDate formControlName="start" placeholder="Start date" name="starDate" (dateChange)="dateRangeChange()">
<input matEndDate formControlName="end" placeholder="End date" name="endDate" (dateChange)="dateRangeChange()">
</mat-date-range-input>
<mat-hint>MM/DD/YYYY – MM/DD/YYYY</mat-hint>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,15 @@ describe('TimeRangeCustomComponent', () => {

expect(component.onSubmit).not.toHaveBeenCalled();
});

it('should call range form and delete variable local storage ', () => {
spyOn(localStorage, 'removeItem').withArgs('rangeDatePicker');
component.range.setValue({start: null, end: null});
jasmine.clock().install();
component.dateRangeChange();
jasmine.clock().tick(200);
expect(localStorage.removeItem).toHaveBeenCalledWith('rangeDatePicker');
jasmine.clock().uninstall();
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class TimeRangeCustomComponent implements OnInit, OnChanges {
start: formatDate(moment().startOf('isoWeek').format('l'), DATE_FORMAT, 'en'),
end: formatDate(moment().format('l'), DATE_FORMAT, 'en')
});
localStorage.setItem('rangeDatePicker', 'custom');
this.onSubmit();
}

Expand All @@ -65,4 +66,11 @@ export class TimeRangeCustomComponent implements OnInit, OnChanges {
}
}

dateRangeChange() {
setTimeout(() => {
if (this.range.get('start').value === null || this.range.get('end').value === null) {
localStorage.removeItem('rangeDatePicker');
}
}, 200);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
<div class="list-time-range">
<mat-selection-list class="custom-items-time-range">
<mat-list-option color="primary" class="custom-mat-list-option" (click)="resetTimeRange()">
custom
</mat-list-option>
</mat-selection-list>
<mat-selection-list class="custom-items-time-range" [multiple]="false">
<mat-list-option class="custom-mat-list-option" *ngFor="let item of customPresets" [value]="item" (click)="selectRange(item)" >
{{item}}
</mat-list-option>
</mat-selection-list>
<mat-selection-list class="custom-items-time-range" [multiple]="false">
<mat-list-option class="custom-mat-list-option" *ngFor="let item of customPresets" [value]="item" (click)="selectRange(item)" [selected]="item === rangeDateSelected">
{{item}}
</mat-list-option>
</mat-selection-list>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ describe('TimeRangeOptionsComponent', () => {
expect(component).toBeTruthy();
});

it('should call resetTimeRange method and clean time range input ', () => {
component.resetTimeRange();
expect(component.picker.startAt).toEqual(undefined);
});

it('should click selectRange button and call calculateDateRange method', () => {
spyOn(component, 'calculateDateRange').and.returnValues(['', '']);
component.selectRange('today');
Expand All @@ -65,16 +60,18 @@ describe('TimeRangeOptionsComponent', () => {
expect(new Date(end).toDateString()).toEqual(new Date().toDateString());
});

it('should call calculateMonth and calculateWeek method when is called calculateDateRange method', () => {
it('should call getMondayCurrent, calculateMonth and calculateWeek method when is called calculateDateRange method', () => {

const dataAll = [
{method: 'calculateWeek', options: ['this week', 'last week']},
{method: 'calculateMonth', options: ['this month', 'last month']}];
{method: 'getMondayCurrent', ranges: ['custom']},
{method: 'calculateWeek', ranges: ['this week', 'last week']},
{method: 'calculateMonth', ranges: ['this month', 'last month']}
];

dataAll.forEach((val: any) => {
spyOn(component, val.method);
val.options.forEach((option: any) => {
component.calculateDateRange(option);
val.ranges.forEach((range: any) => {
component.calculateDateRange(range);
expect(component[val.method]).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -130,4 +127,12 @@ describe('TimeRangeOptionsComponent', () => {
expect(toastrServiceStub.error).toHaveBeenCalled();
});

it('should call to method an error when the date created is null from date adapter', () => {
spyOn(component.dateAdapter, 'getYear').and.returnValues(2022);
spyOn(component.dateAdapter, 'getMonth').and.returnValues(7);
component.getMondayCurrent();
expect(component.dateAdapter.getYear).toHaveBeenCalled();
expect(component.dateAdapter.getMonth).toHaveBeenCalled();
});

});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Component, HostBinding, ChangeDetectionStrategy } from '@angular/core';
import { Component, HostBinding, ChangeDetectionStrategy, OnInit } from '@angular/core';
import { DateAdapter } from '@angular/material/core';
import { MatDateRangePicker } from '@angular/material/datepicker';
import { ToastrService } from 'ngx-toastr';


const customPresets = [
'custom',
'today',
'last 7 days',
'this week',
Expand All @@ -22,9 +23,10 @@ type CustomPreset = typeof customPresets[number];
styleUrls: ['./time-range-options.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TimeRangeOptionsComponent<Date> {
export class TimeRangeOptionsComponent<Date> implements OnInit{

customPresets = customPresets;
rangeDateSelected = '';
@HostBinding('class.touch-ui')
readonly isTouchUi = this.picker.touchUi;
constructor(
Expand All @@ -35,8 +37,22 @@ export class TimeRangeOptionsComponent<Date> {
this.dateAdapter.getFirstDayOfWeek = () => 1;
}

ngOnInit() {
this.rangeDateSelected = this.getLocalStorageRange();
}

getLocalStorageRange(): string {
return localStorage.getItem('rangeDatePicker');
}

setLocalStorageRange(range: string): void {
localStorage.setItem('rangeDatePicker', range);
}

selectRange(rangeName: CustomPreset): void {
const [start, end] = this.calculateDateRange(rangeName);
this.setLocalStorageRange(rangeName);

this.picker.select(start);
this.picker.select(end);
this.picker.close();
Expand All @@ -47,6 +63,9 @@ export class TimeRangeOptionsComponent<Date> {
const year = this.dateAdapter.getYear(today);

switch (rangeName) {
case 'custom':
const mondayWeek = this.getMondayCurrent();
return [mondayWeek, today];
case 'today':
return [today, today];
case 'last 7 days': {
Expand Down Expand Up @@ -108,9 +127,14 @@ export class TimeRangeOptionsComponent<Date> {
return today;
}

resetTimeRange() {
this.picker.select(undefined);
this.picker.select(undefined);
getMondayCurrent(): Date {
const yearCurrent = this.dateAdapter.getYear(this.dateAdapter.today());
const monthCurrent = this.dateAdapter.getMonth(this.dateAdapter.today());
const today = new Date();
const first = today.getDate() - today.getDay() + 1;
const monday = new Date(today.setDate(first));
const mondayDayCurrent = monday.getDate();
return this.dateAdapter.createDate(yearCurrent, monthCurrent, mondayDayCurrent);
}

}