Skip to content

Commit fc2587a

Browse files
author
Guido Quezada
committed
TT-39 fix: code refactor
1 parent 42aea4b commit fc2587a

File tree

9 files changed

+28
-25
lines changed

9 files changed

+28
-25
lines changed

src/app/modules/shared/components/month-picker/month-picker.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe('MonthPickerComponent', () => {
3333
expect(component.dateSelected.emit({ monthIndex: month, year: year }));
3434
});
3535

36+
3637
it('should add a year to current year', () => {
3738
component.selectedYearMoment = moment();
3839
const incrementYear = moment().add(1, 'year').format('Y');
@@ -57,4 +58,13 @@ describe('MonthPickerComponent', () => {
5758
expect(component.selectedYearMoment.format('Y')).toEqual(decrementYear);
5859
});
5960

61+
62+
it('selectMonth should call selectDates', () => {
63+
spyOn(component, 'selectDate');
64+
65+
component.selectMonth(8);
66+
67+
expect(component.selectDate).toHaveBeenCalledWith(8, 2020);
68+
});
69+
6070
});

src/app/modules/shared/components/month-picker/month-picker.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { SubstractDatePipe } from './../../pipes/substract-date/substract-date.pipe';
21
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
32
import * as moment from 'moment';
43

src/app/modules/time-clock/components/entry-fields/entry-fields.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export class EntryFieldsComponent implements OnInit {
3232
newData;
3333
lastEntry;
3434
showTimeInbuttons = false;
35-
month = new Date().getMonth();
36-
year = new Date().getFullYear();
3735

3836
constructor(
3937
private formBuilder: FormBuilder,
@@ -52,7 +50,7 @@ export class EntryFieldsComponent implements OnInit {
5250

5351
ngOnInit(): void {
5452
this.store.dispatch(new LoadActivities());
55-
this.store.dispatch(new entryActions.LoadEntries(this.month, this.year));
53+
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
5654
this.actionsSubject$
5755
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
5856
.subscribe((action) => {

src/app/modules/time-clock/services/entry.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ describe('EntryService', () => {
5858
const month = new Date().getMonth();
5959

6060
const timezoneOffset = new Date().getTimezoneOffset();
61-
service.loadEntries(year, month).subscribe();
61+
service.loadEntries({ year, month }).subscribe();
6262

6363
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}?month=${month}&year=${year}&timezone_offset=${timezoneOffset}`);
6464
expect(loadEntryRequest.request.method).toBe('GET');

src/app/modules/time-clock/services/entry.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class EntryService {
2323
return this.http.get(`${this.baseUrl}/running`);
2424
}
2525

26-
loadEntries(year, month): Observable<any> {
26+
loadEntries(date): Observable<any> {
2727
const timezoneOffset = new Date().getTimezoneOffset();
28-
return this.http.get(`${this.baseUrl}?month=${month}&year=${year}&timezone_offset=${timezoneOffset}`);
28+
return this.http.get(`${this.baseUrl}?month=${date.month}&year=${date.year}&timezone_offset=${timezoneOffset}`);
2929
}
3030

3131
createEntry(entryData): Observable<any> {

src/app/modules/time-clock/store/entry.effects.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,16 @@ export class EntryEffects {
8787
@Effect()
8888
loadEntries$: Observable<Action> = this.actions$.pipe(
8989
ofType(actions.EntryActionTypes.LOAD_ENTRIES),
90-
// tslint:disable-next-line:no-unused-expression
91-
map((action: actions.LoadEntries) => (action.month, action.year)),
92-
mergeMap((month, year) =>
93-
this.entryService.loadEntries(month, year).pipe(
90+
map((action: actions.LoadEntries) => action),
91+
mergeMap((date) =>
92+
this.entryService.loadEntries({ month: date.month, year: date.year }).pipe(
9493
map((entries) => new actions.LoadEntriesSuccess(entries)),
9594
catchError((error) => {
9695
this.toastrService.warning(`The data could not be loaded`);
9796
return of(new actions.LoadEntriesFail(error));
9897
})
9998
)
10099
)
101-
102100
);
103101

104102
@Effect()
@@ -228,7 +226,10 @@ export class EntryEffects {
228226
ofType(actions.EntryActionTypes.UPDATE_CURRENT_OR_LAST_ENTRY),
229227
map((action: actions.UpdateCurrentOrLastEntry) => action.payload),
230228
switchMap((entry) =>
231-
this.entryService.loadEntries(new Date().getMonth() + 1, new Date().getFullYear()).pipe(
229+
this.entryService.loadEntries({
230+
month : new Date().getMonth() + 1,
231+
year: new Date().getFullYear()
232+
}).pipe(
232233
map((entries) => {
233234
const lastEntry = entries[1];
234235
const isStartTimeInLastEntry = moment(entry.start_date).isBefore(lastEntry.end_date);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('TimeEntriesComponent', () => {
126126
}));
127127

128128
it('when create time entries, the time entries should be queried', () => {
129-
const currentMonth = new Date().getMonth();
129+
const currentMonth = new Date().getMonth() + 1;
130130
const year = new Date().getFullYear();
131131
const entryToSave = {
132132
entry: {

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { formatDate } from '@angular/common';
21
import { Component, OnDestroy, OnInit } from '@angular/core';
32
import { ActionsSubject, select, Store } from '@ngrx/store';
43
import { ToastrService } from 'ngx-toastr';
@@ -32,9 +31,6 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
3231
selectedMonthIndex: number;
3332
selectedMonthAsText: string;
3433

35-
currentMonth = new Date().getMonth();
36-
year = new Date().getFullYear();
37-
3834
constructor(private store: Store<EntryState>, private toastrService: ToastrService, private actionsSubject$: ActionsSubject) {
3935
this.timeEntriesDataSource$ = this.store.pipe(delay(0), select(getTimeEntriesDataSource));
4036
}
@@ -44,9 +40,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
4440
}
4541

4642
ngOnInit(): void {
47-
48-
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, this.year));
49-
43+
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
5044
this.loadActiveEntry();
5145

5246
this.entriesSubscription = this.actionsSubject$.pipe(
@@ -58,6 +52,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
5852
)
5953
).subscribe((action) => {
6054
this.loadActiveEntry();
55+
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, new Date().getFullYear()));
6156
});
6257
}
6358

@@ -116,7 +111,8 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
116111
const isEditingEntryEqualToActiveEntry = this.entryId === this.activeTimeEntry.id;
117112
const isStartDateGreaterThanActiveEntry = startDateAsLocalDate > activeEntryAsLocalDate;
118113
const isEndDateGreaterThanActiveEntry = endDateAsLocalDate > activeEntryAsLocalDate;
119-
if (!isEditingEntryEqualToActiveEntry && (isStartDateGreaterThanActiveEntry || isEndDateGreaterThanActiveEntry)){
114+
const isTimeEntryOverlapping = isStartDateGreaterThanActiveEntry || isEndDateGreaterThanActiveEntry;
115+
if (!isEditingEntryEqualToActiveEntry && isTimeEntryOverlapping) {
120116
this.toastrService.error('You are on the clock and this entry overlaps it, try with earlier times.');
121117
} else {
122118
this.doSave(event);
@@ -171,9 +167,9 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
171167

172168
dateSelected(event: { monthIndex: number; year: number }) {
173169
this.selectedYearAsText = event.year.toString();
174-
this.selectedMonthIndex = event.monthIndex;
170+
this.selectedMonthIndex = event.monthIndex + 1;
175171
this.selectedMonthAsText = moment().month(event.monthIndex).format('MMMM');
176-
this.store.dispatch(new entryActions.LoadEntries(event.monthIndex + 1, event.year));
172+
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, event.year));
177173
}
178174

179175
openModal(item: any) {

tslint.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
"no-redundant-jsdoc": true,
7777
"no-switch-case-fall-through": true,
7878
"no-var-requires": false,
79-
"object-literal-shorthand": false,
8079
"object-literal-key-quotes": [
8180
true,
8281
"as-needed"

0 commit comments

Comments
 (0)