Skip to content

Commit baa2cb9

Browse files
authored
Merge pull request #303 from ioet/180_Show_time-entries_for_selected_month
fix: #180 Show time-entries for selected month
2 parents a64be95 + 6bb89b2 commit baa2cb9

File tree

9 files changed

+26
-35
lines changed

9 files changed

+26
-35
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ describe('MonthPickerComponent', () => {
2424
});
2525

2626
it('should emit activeMonth event', () => {
27-
const month = 1;
27+
const month = 2;
2828
spyOn(component.monthSelected, 'emit');
2929
component.getMonth(month);
30-
expect(component.monthSelected.emit).toHaveBeenCalledWith(month);
30+
expect(component.monthSelected.emit).toHaveBeenCalledWith(month + 1);
3131
});
3232
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class MonthPickerComponent implements OnInit {
1717
}
1818

1919
getMonth(month: number) {
20-
this.monthSelected.emit(month);
20+
this.monthSelected.emit(month + 1);
2121
this.activeMonth = month;
2222
}
2323
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('EntryService', () => {
5656

5757
it('loads all Entries', () => {
5858
service.baseUrl = 'time-entries';
59-
service.loadEntries().subscribe((response) => {
59+
service.loadEntries(new Date().getMonth).subscribe((response) => {
6060
const loadEntryRequest = httpMock.expectOne(`${service.baseUrl}`);
6161
expect(loadEntryRequest.request.method).toBe('GET');
6262
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ export class EntryService {
1717
return this.http.get(`${this.baseUrl}/running`);
1818
}
1919

20-
loadEntries(): Observable<any> {
21-
return this.http.get(`${this.baseUrl}`);
20+
loadEntries(month): Observable<any> {
21+
return this.http.get(`${this.baseUrl}?month=${month}`);
2222
}
2323

2424
createEntry(entryData): Observable<any> {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class LoadActiveEntryFail implements Action {
5959

6060
export class LoadEntries implements Action {
6161
public readonly type = EntryActionTypes.LOAD_ENTRIES;
62+
constructor(public month: number) {}
6263
}
6364

6465
export class LoadEntriesSuccess implements Action {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as actions from './entry.actions';
1010

1111
@Injectable()
1212
export class EntryEffects {
13-
constructor(private actions$: Actions, private entryService: EntryService, private toastrService: ToastrService) { }
13+
constructor(private actions$: Actions, private entryService: EntryService, private toastrService: ToastrService) {}
1414

1515
@Effect()
1616
loadEntriesSummary$: Observable<Action> = this.actions$.pipe(
@@ -46,8 +46,9 @@ export class EntryEffects {
4646
@Effect()
4747
loadEntries$: Observable<Action> = this.actions$.pipe(
4848
ofType(actions.EntryActionTypes.LOAD_ENTRIES),
49-
mergeMap(() =>
50-
this.entryService.loadEntries().pipe(
49+
map((action: actions.LoadEntries) => action.month),
50+
mergeMap((month) =>
51+
this.entryService.loadEntries(month).pipe(
5152
map((entries) => new actions.LoadEntriesSuccess(entries)),
5253
catchError((error) => {
5354
this.toastrService.warning(`The data could not be loaded`);

src/app/modules/time-clock/store/entry.reducer.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe('entryReducer', () => {
7979
});
8080

8181
it('on LoadEntries, isLoading is true', () => {
82-
const action = new actions.LoadEntries();
82+
const action = new actions.LoadEntries(new Date().getMonth() + 1);
8383
const state = entryReducer(initialState, action);
8484
expect(state.isLoading).toEqual(true);
8585
});

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22
import { provideMockStore, MockStore } from '@ngrx/store/testing';
33
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
44

5-
import {
6-
MonthPickerComponent,
7-
DetailsFieldsComponent,
8-
EmptyStateComponent,
9-
} from '../../shared/components';
5+
import { MonthPickerComponent, DetailsFieldsComponent, EmptyStateComponent } from '../../shared/components';
106
import { GroupByDatePipe } from '../../shared/pipes';
117
import { TechnologyState } from '../../shared/store/technology.reducers';
128
import { allTechnologies } from '../../shared/store/technology.selectors';
@@ -16,8 +12,8 @@ import { getProjects } from '../../customer-management/components/projects/compo
1612
import { EntryState } from '../../time-clock/store/entry.reducer';
1713
import { allEntries } from '../../time-clock/store/entry.selectors';
1814
import * as entryActions from '../../time-clock/store/entry.actions';
19-
import {TechnologiesComponent} from '../../shared/components/technologies/technologies.component';
20-
import {TimeEntriesSummaryComponent} from '../../time-clock/components/time-entries-summary/time-entries-summary.component';
15+
import { TechnologiesComponent } from '../../shared/components/technologies/technologies.component';
16+
import { TimeEntriesSummaryComponent } from '../../time-clock/components/time-entries-summary/time-entries-summary.component';
2117

2218
describe('TimeEntriesComponent', () => {
2319
type Merged = TechnologyState & ProjectState & EntryState;
@@ -70,7 +66,7 @@ describe('TimeEntriesComponent', () => {
7066
MonthPickerComponent,
7167
TimeEntriesComponent,
7268
TechnologiesComponent,
73-
TimeEntriesSummaryComponent
69+
TimeEntriesSummaryComponent,
7470
],
7571
providers: [provideMockStore({ initialState: state })],
7672
imports: [FormsModule, ReactiveFormsModule],
@@ -147,7 +143,7 @@ describe('TimeEntriesComponent', () => {
147143
};
148144
mockEntriesSelector = store.overrideSelector(allEntries, [newEntry]);
149145
component.ngOnInit();
150-
expect(component.dataByMonth.length).toEqual(0);
146+
expect(component.dataByMonth.length).toEqual(1);
151147
}));
152148

153149
it('should set entry and entryid to null', () => {
@@ -157,9 +153,9 @@ describe('TimeEntriesComponent', () => {
157153
});
158154

159155
it('should set entry and entryid to with data', () => {
160-
component.entryList = [entry];
156+
component.dataByMonth = [entry];
161157
component.editEntry('entry_1');
162-
expect(component.entry).toBe(entry);
158+
expect(component.entry).toEqual(entry);
163159
expect(component.entryId).toBe('entry_1');
164160
});
165161

@@ -198,8 +194,8 @@ describe('TimeEntriesComponent', () => {
198194

199195
it('should get the entry List by Month', () => {
200196
const month = 1;
201-
component.entryList = [entry];
197+
spyOn(store, 'dispatch');
202198
component.getMonth(month);
203-
expect(component.dataByMonth.length).toEqual(1);
199+
expect(store.dispatch).toHaveBeenCalledWith(new entryActions.LoadEntries(month));
204200
});
205201
});

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@ export class TimeEntriesComponent implements OnInit {
1414
entryId: string;
1515
entry: Entry;
1616
dataByMonth = [];
17-
entryList: Entry[];
1817

1918
constructor(private store: Store<EntryState>) {}
2019

2120
ngOnInit(): void {
22-
this.store.dispatch(new entryActions.LoadEntries());
21+
this.store.dispatch(new entryActions.LoadEntries(new Date().getMonth() + 1));
2322
const dataByMonth$ = this.store.pipe(select(allEntries));
2423
dataByMonth$.subscribe((response) => {
25-
this.entryList = response;
26-
this.dataByMonth = this.entryList.reduce((acc: any, entry: any) => {
27-
if (new Date(entry.start_date).getMonth() === new Date().getMonth()) {
28-
const item = { ...entry };
29-
return [...acc, item];
30-
}
31-
return [];
32-
}, []);
24+
this.dataByMonth = response;
3325
});
3426
}
3527

@@ -39,8 +31,9 @@ export class TimeEntriesComponent implements OnInit {
3931
}
4032

4133
editEntry(entryId: string) {
34+
console.log(this.dataByMonth);
4235
this.entryId = entryId;
43-
this.entry = this.entryList.find((entry) => entry.id === entryId);
36+
this.entry = this.dataByMonth.find((entry) => entry.id === entryId);
4437
}
4538

4639
saveEntry(entry): void {
@@ -57,6 +50,6 @@ export class TimeEntriesComponent implements OnInit {
5750
}
5851

5952
getMonth(month: number) {
60-
this.dataByMonth = this.entryList.filter((entry) => new Date(entry.start_date).getMonth() === month);
53+
this.store.dispatch(new entryActions.LoadEntries(month));
6154
}
6255
}

0 commit comments

Comments
 (0)