Skip to content

Commit 607303c

Browse files
authored
Tt 319 fix calendar day week view (#738)
* fix: TT-319 Fix calendar week and day view * refactor: TT-319 select first day of the month when select on calendar * refactor: TT-319 Change moment library with date Co-authored-by: Andrés Soto <edansrock>
1 parent 40b4a67 commit 607303c

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

src/app/modules/time-entries/components/calendar/calendar.component.spec.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ describe('CalendarComponent', () => {
198198
});
199199

200200
it('emit current date and call navigationEnable when call handleChangeDateEvent', () => {
201+
const calendarView: CalendarView = CalendarView.Month;
201202
const fakeValueEmit = {
202-
date: currentDate.toDate(),
203+
date: currentDate.toDate()
203204
};
204-
const calendarView = CalendarView.Month;
205205
spyOn(component, 'navigationEnable');
206206
spyOn(component.changeDate, 'emit');
207207
spyOn(component, 'isVisibleForCurrentDate');
@@ -221,6 +221,14 @@ describe('CalendarComponent', () => {
221221
expect(component.calendarView).toEqual(fakeCalendarView);
222222
});
223223

224+
it('emit calendarView Day when call changeCalendarView', () => {
225+
const fakeCalendarView: CalendarView = CalendarView.Day;
226+
component.calendarView = CalendarView.Month;
227+
spyOn(component.changeView, 'emit');
228+
component.changeCalendarView(fakeCalendarView);
229+
expect(component.changeView.emit).toHaveBeenCalledWith({ calendarView: fakeCalendarView });
230+
});
231+
224232
it('set srcoll to current time marker in calendarView when is call scrollToCurrentTimeMarker', () => {
225233
const fakeCalendarView: CalendarView = CalendarView.Week;
226234
spyOn(component, 'scrollToCurrentTimeMarker');

src/app/modules/time-entries/components/calendar/calendar.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export class CalendarComponent implements OnInit {
4444
@Output() changeDate: EventEmitter<any> = new EventEmitter<{
4545
date: Date;
4646
}>();
47+
@Output() changeView: EventEmitter<any> = new EventEmitter<{
48+
calendarView: CalendarView;
49+
}>();
4750

4851
initialDate: Date;
4952
previusDate: Date;
@@ -119,6 +122,7 @@ export class CalendarComponent implements OnInit {
119122
this.referenceChangeDetector.detectChanges();
120123
this.scrollToCurrentTimeMarker();
121124
}
125+
this.changeView.emit({ calendarView });
122126
}
123127

124128
navigationEnable(calendarView: CalendarView) {

src/app/modules/time-entries/pages/time-entries.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
*ngIf="!dataSource.isLoading"
3333
[timeEntries$]="timeEntriesDataSource$"
3434
[currentDate]="selectedDate.toDate()"
35+
[calendarView]="calendarView"
3536
(changeDate)="changeDate($event)"
37+
(changeView)="changeView($event)"
3638
(viewModal)="editEntry($event.id)"
3739
(deleteTimeEntry)="openModal($event.timeEntry)"
3840
>

src/app/modules/time-entries/pages/time-entries.component.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { NgxMaterialTimepickerModule } from 'ngx-material-timepicker';
2121
import { CookieService } from 'ngx-cookie-service';
2222
import { DebugElement } from '@angular/core';
2323
import { FeatureToggle } from './../../../../environments/enum';
24+
import { CalendarView } from 'angular-calendar';
2425
import * as moment from 'moment';
2526

2627
describe('TimeEntriesComponent', () => {
@@ -560,6 +561,7 @@ describe('TimeEntriesComponent', () => {
560561
const dateMoment: moment.Moment = moment().month(monthIndex).year(year);
561562
jasmine.clock().mockDate(dateMoment.toDate());
562563

564+
component.actualDate.setMonth(monthIndex);
563565
component.dateSelected(eventData);
564566

565567
expect(component.selectedDate).toEqual(dateMoment);
@@ -569,7 +571,7 @@ describe('TimeEntriesComponent', () => {
569571
const incomingDate = new Date('2021-06-07');
570572
const incomingMoment: moment.Moment = moment(incomingDate);
571573
const eventData = {
572-
date: incomingDate,
574+
date: incomingDate
573575
};
574576
spyOn(component, 'dateSelected');
575577
component.selectedDate = moment(incomingMoment).subtract(1, 'day');
@@ -584,7 +586,7 @@ describe('TimeEntriesComponent', () => {
584586
const incomingDate = new Date('2021-01-07');
585587
const incomingMoment: moment.Moment = moment(incomingDate);
586588
const eventData = {
587-
date: incomingDate,
589+
date: incomingDate
588590
};
589591
const selectedDate = {
590592
monthIndex: incomingMoment.month(),
@@ -598,6 +600,37 @@ describe('TimeEntriesComponent', () => {
598600
expect(component.dateSelected).toHaveBeenCalledWith(selectedDate);
599601
});
600602

603+
it('change component selectedDate to be the first day of the month when call dateSelected', () => {
604+
const actualDate: Date = new Date(2021, 5, 15);
605+
const selectedDate: Date = new Date(2021, 2, 1);
606+
const eventDate = {
607+
monthIndex: selectedDate.getMonth(),
608+
year: selectedDate.getFullYear()
609+
};
610+
component.actualDate = actualDate;
611+
component.dateSelected(eventDate);
612+
expect(component.selectedDate.date()).toBe(selectedDate.getDate());
613+
});
614+
615+
it('change component calendarView from Month to Day when call changeView', () => {
616+
const fakeCalendarView: CalendarView = CalendarView.Day;
617+
const eventView = {
618+
calendarView: fakeCalendarView
619+
};
620+
component.calendarView = CalendarView.Month;
621+
component.changeView(eventView);
622+
expect(component.calendarView).toBe(fakeCalendarView);
623+
});
624+
625+
it('change component calendarView to Month if undefined when call changeView', () => {
626+
component.calendarView = CalendarView.Week;
627+
const eventView = {
628+
calendarView: undefined
629+
};
630+
component.changeView(eventView);
631+
expect(component.calendarView).toBe(CalendarView.Month);
632+
});
633+
601634
it('not view button onDisplayModeChange when isFeatureToggleCalendarActive is false', () => {
602635
component.isFeatureToggleCalendarActive = false;
603636

src/app/modules/time-entries/pages/time-entries.component.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { EntryActionTypes } from './../../time-clock/store/entry.actions';
1414
import { getActiveTimeEntry, getTimeEntriesDataSource } from './../../time-clock/store/entry.selectors';
1515
import { CookieService } from 'ngx-cookie-service';
1616
import { FeatureToggle } from './../../../../environments/enum';
17+
import { CalendarView } from 'angular-calendar';
1718
@Component({
1819
selector: 'app-time-entries',
1920
templateUrl: './time-entries.component.html',
@@ -38,6 +39,8 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
3839
selectedYear: number;
3940
selectedMonthAsText: string;
4041
isActiveEntryOverlapping = false;
42+
calendarView: CalendarView = CalendarView.Month;
43+
actualDate: Date;
4144
readonly NO_DATA_MESSAGE: string = 'No data available in table';
4245
constructor(
4346
private store: Store<EntryState>,
@@ -46,6 +49,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
4649
private cookiesService: CookieService) {
4750
this.displayGridView = false;
4851
this.selectedDate = moment(new Date());
52+
this.actualDate = new Date();
4953
this.timeEntriesDataSource$ = this.store.pipe(delay(0), select(getTimeEntriesDataSource));
5054
}
5155
ngOnDestroy(): void {
@@ -180,6 +184,9 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
180184
this.selectedMonthAsText = moment().month(event.monthIndex).format('MMMM');
181185
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonth, this.selectedYear));
182186
this.selectedDate = moment().month(event.monthIndex).year(event.year);
187+
if (this.actualDate.getMonth() !== event.monthIndex){
188+
this.selectedDate = this.selectedDate.startOf('month');
189+
}
183190
}
184191

185192
changeDate(event: { date: Date }){
@@ -196,6 +203,10 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
196203
this.selectedDate = newDate;
197204
}
198205

206+
changeView(event: { calendarView: CalendarView }){
207+
this.calendarView = event.calendarView || CalendarView.Month;
208+
}
209+
199210
openModal(item: any) {
200211
this.idToDelete = item.id;
201212
this.message = `Are you sure you want to delete ${item.activity_name}?`;

0 commit comments

Comments
 (0)