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
Prev Previous commit
Next Next commit
TT-39 fix: code refactor
  • Loading branch information
Guido Quezada committed Dec 29, 2020
commit fc2587ac8a9c4816c110d79ac368aaa6176b9ddf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('MonthPickerComponent', () => {
expect(component.dateSelected.emit({ monthIndex: month, year: year }));
});


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


it('selectMonth should call selectDates', () => {
spyOn(component, 'selectDate');

component.selectMonth(8);

expect(component.selectDate).toHaveBeenCalledWith(8, 2020);
});

});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { SubstractDatePipe } from './../../pipes/substract-date/substract-date.pipe';
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import * as moment from 'moment';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export class EntryFieldsComponent implements OnInit {
newData;
lastEntry;
showTimeInbuttons = false;
month = new Date().getMonth();
year = new Date().getFullYear();

constructor(
private formBuilder: FormBuilder,
Expand All @@ -52,7 +50,7 @@ export class EntryFieldsComponent implements OnInit {

ngOnInit(): void {
this.store.dispatch(new LoadActivities());
this.store.dispatch(new entryActions.LoadEntries(this.month, this.year));
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
this.actionsSubject$
.pipe(filter((action: any) => action.type === ActivityManagementActionTypes.LOAD_ACTIVITIES_SUCCESS))
.subscribe((action) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/time-clock/services/entry.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('EntryService', () => {
const month = new Date().getMonth();

const timezoneOffset = new Date().getTimezoneOffset();
service.loadEntries(year, month).subscribe();
service.loadEntries({ year, month }).subscribe();

const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}?month=${month}&year=${year}&timezone_offset=${timezoneOffset}`);
expect(loadEntryRequest.request.method).toBe('GET');
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/time-clock/services/entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export class EntryService {
return this.http.get(`${this.baseUrl}/running`);
}

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

createEntry(entryData): Observable<any> {
Expand Down
13 changes: 7 additions & 6 deletions src/app/modules/time-clock/store/entry.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,16 @@ export class EntryEffects {
@Effect()
loadEntries$: Observable<Action> = this.actions$.pipe(
ofType(actions.EntryActionTypes.LOAD_ENTRIES),
// tslint:disable-next-line:no-unused-expression
map((action: actions.LoadEntries) => (action.month, action.year)),
mergeMap((month, year) =>
this.entryService.loadEntries(month, year).pipe(
map((action: actions.LoadEntries) => action),
mergeMap((date) =>
this.entryService.loadEntries({ month: date.month, year: date.year }).pipe(
map((entries) => new actions.LoadEntriesSuccess(entries)),
catchError((error) => {
this.toastrService.warning(`The data could not be loaded`);
return of(new actions.LoadEntriesFail(error));
})
)
)

);

@Effect()
Expand Down Expand Up @@ -228,7 +226,10 @@ export class EntryEffects {
ofType(actions.EntryActionTypes.UPDATE_CURRENT_OR_LAST_ENTRY),
map((action: actions.UpdateCurrentOrLastEntry) => action.payload),
switchMap((entry) =>
this.entryService.loadEntries(new Date().getMonth() + 1, new Date().getFullYear()).pipe(
this.entryService.loadEntries({
month : new Date().getMonth() + 1,
year: new Date().getFullYear()
}).pipe(
map((entries) => {
const lastEntry = entries[1];
const isStartTimeInLastEntry = moment(entry.start_date).isBefore(lastEntry.end_date);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('TimeEntriesComponent', () => {
}));

it('when create time entries, the time entries should be queried', () => {
const currentMonth = new Date().getMonth();
const currentMonth = new Date().getMonth() + 1;
const year = new Date().getFullYear();
const entryToSave = {
entry: {
Expand Down
16 changes: 6 additions & 10 deletions src/app/modules/time-entries/pages/time-entries.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { formatDate } from '@angular/common';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActionsSubject, select, Store } from '@ngrx/store';
import { ToastrService } from 'ngx-toastr';
Expand Down Expand Up @@ -32,9 +31,6 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
selectedMonthIndex: number;
selectedMonthAsText: string;

currentMonth = new Date().getMonth();
year = new Date().getFullYear();

constructor(private store: Store<EntryState>, private toastrService: ToastrService, private actionsSubject$: ActionsSubject) {
this.timeEntriesDataSource$ = this.store.pipe(delay(0), select(getTimeEntriesDataSource));
}
Expand All @@ -44,9 +40,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
}

ngOnInit(): void {

this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, this.year));

this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1, new Date().getFullYear()));
this.loadActiveEntry();

this.entriesSubscription = this.actionsSubject$.pipe(
Expand All @@ -58,6 +52,7 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
)
).subscribe((action) => {
this.loadActiveEntry();
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, new Date().getFullYear()));
});
}

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

dateSelected(event: { monthIndex: number; year: number }) {
this.selectedYearAsText = event.year.toString();
this.selectedMonthIndex = event.monthIndex;
this.selectedMonthIndex = event.monthIndex + 1;
this.selectedMonthAsText = moment().month(event.monthIndex).format('MMMM');
this.store.dispatch(new entryActions.LoadEntries(event.monthIndex + 1, event.year));
this.store.dispatch(new entryActions.LoadEntries(this.selectedMonthIndex, event.year));
}

openModal(item: any) {
Expand Down
1 change: 0 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"no-redundant-jsdoc": true,
"no-switch-case-fall-through": true,
"no-var-requires": false,
"object-literal-shorthand": false,
"object-literal-key-quotes": [
true,
"as-needed"
Expand Down