@@ -5,15 +5,19 @@ import { DataTableDirective } from 'angular-datatables';
55import * as moment from 'moment' ;
66import { Observable , Subject , Subscription } from 'rxjs' ;
77import { filter } from 'rxjs/operators' ;
8- import { Entry , Project } from 'src/app/modules/shared/models' ;
8+ import { Activity , Entry , Project } from 'src/app/modules/shared/models' ;
99import { DataSource } from 'src/app/modules/shared/models/data-source.model' ;
1010import { EntryState } from '../../../time-clock/store/entry.reducer' ;
1111import { getReportDataSource , getResultSumEntriesSelected } from '../../../time-clock/store/entry.selectors' ;
1212import { TotalHours } from '../../models/total-hours-report' ;
1313import { User } from 'src/app/modules/users/models/users' ;
1414import { LoadUsers , UserActionTypes } from 'src/app/modules/users/store/user.actions' ;
1515import { ParseDateTimeOffset } from '../../../shared/formatters/parse-date-time-offset/parse-date-time-offset' ;
16- import { LoadProjects , ProjectActionTypes } from 'src/app/modules/customer-management/components/projects/components/store/project.actions' ;
16+ import {
17+ LoadProjects ,
18+ ProjectActionTypes ,
19+ } from 'src/app/modules/customer-management/components/projects/components/store/project.actions' ;
20+ import { ActivityManagementActionTypes , LoadActivities } from 'src/app/modules/activities-management/store' ;
1721
1822@Component ( {
1923 selector : 'app-time-entries-table' ,
@@ -23,12 +27,14 @@ import { LoadProjects, ProjectActionTypes } from 'src/app/modules/customer-manag
2327export class TimeEntriesTableComponent implements OnInit , OnDestroy , AfterViewInit {
2428 @Output ( ) selectedUserId = new EventEmitter < string > ( ) ;
2529 @Output ( ) selectedProjectId = new EventEmitter < string > ( ) ;
30+ @Output ( ) selectedActivityId = new EventEmitter < string > ( ) ;
2631
2732 selectOptionValues = [ 15 , 30 , 50 , 100 , - 1 ] ;
2833 selectOptionNames = [ 15 , 30 , 50 , 100 , 'All' ] ;
2934 totalTimeSelected : moment . Duration ;
3035 users : User [ ] = [ ] ;
3136 projects : Project [ ] = [ ] ;
37+ activities : Activity [ ] = [ ] ;
3238 removeFirstColumn = 'th:not(:first)' ;
3339
3440 dtOptions : any = {
@@ -40,7 +46,7 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
4046 {
4147 text : 'Column Visibility' + ' ▼' ,
4248 extend : 'colvis' ,
43- columns : ':not(.hidden-col)'
49+ columns : ':not(.hidden-col)' ,
4450 } ,
4551 {
4652 extend : 'print' ,
@@ -52,27 +58,34 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
5258 extend : 'excel' ,
5359 exportOptions : {
5460 format : {
55- body : this . bodyExportOptions
61+ body : this . bodyExportOptions ,
5662 } ,
5763 columns : this . removeFirstColumn ,
5864 } ,
5965 text : 'Excel' ,
60- filename : `time-entries-${ formatDate ( new Date ( ) , 'MM_dd_yyyy-HH_mm' , 'en' ) } `
66+ filename : `time-entries-${ formatDate ( new Date ( ) , 'MM_dd_yyyy-HH_mm' , 'en' ) } ` ,
6167 } ,
6268 {
6369 extend : 'csv' ,
6470 exportOptions : {
6571 format : {
66- body : this . bodyExportOptions
72+ body : this . bodyExportOptions ,
6773 } ,
6874 columns : this . removeFirstColumn ,
6975 } ,
7076 text : 'CSV' ,
71- filename : `time-entries-${ formatDate ( new Date ( ) , 'MM_dd_yyyy-HH_mm' , 'en' ) } `
77+ filename : `time-entries-${ formatDate ( new Date ( ) , 'MM_dd_yyyy-HH_mm' , 'en' ) } ` ,
7278 } ,
7379 ] ,
74- columnDefs : [ { type : 'date' , targets : 3 } , { orderable : false , targets : [ 0 ] } ] ,
75- order : [ [ 1 , 'asc' ] , [ 2 , 'desc' ] , [ 4 , 'desc' ] ]
80+ columnDefs : [
81+ { type : 'date' , targets : 3 } ,
82+ { orderable : false , targets : [ 0 ] } ,
83+ ] ,
84+ order : [
85+ [ 1 , 'asc' ] ,
86+ [ 2 , 'desc' ] ,
87+ [ 4 , 'desc' ] ,
88+ ] ,
7689 } ;
7790 dtTrigger : Subject < any > = new Subject ( ) ;
7891 @ViewChild ( DataTableDirective , { static : false } )
@@ -87,15 +100,17 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
87100 dateTimeOffset : ParseDateTimeOffset ;
88101 listProjects : Project [ ] = [ ] ;
89102
90- constructor ( private store : Store < EntryState > ,
91- private actionsSubject$ : ActionsSubject ,
92- private storeUser : Store < User > ,
93- private storeProject : Store < Project >
94- ) {
95- this . reportDataSource$ = this . store . pipe ( select ( getReportDataSource ) ) ;
96- this . resultSumEntriesSelected$ = this . store . pipe ( select ( getResultSumEntriesSelected ) ) ;
97- this . dateTimeOffset = new ParseDateTimeOffset ( ) ;
98- this . resultSumEntriesSelected = new TotalHours ( ) ;
103+ constructor (
104+ private store : Store < EntryState > ,
105+ private actionsSubject$ : ActionsSubject ,
106+ private storeUser : Store < User > ,
107+ private storeProject : Store < Project > ,
108+ private storeActivity : Store < Activity >
109+ ) {
110+ this . reportDataSource$ = this . store . pipe ( select ( getReportDataSource ) ) ;
111+ this . resultSumEntriesSelected$ = this . store . pipe ( select ( getResultSumEntriesSelected ) ) ;
112+ this . dateTimeOffset = new ParseDateTimeOffset ( ) ;
113+ this . resultSumEntriesSelected = new TotalHours ( ) ;
99114 }
100115
101116 uploadUsers ( ) : void {
@@ -117,7 +132,18 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
117132 const sortProjects = [ ...action . payload ] ;
118133 sortProjects . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
119134 this . projects = sortProjects ;
120- this . projects = this . projects . filter ( project => project . status === 'active' ) ;
135+ this . projects = this . projects . filter ( ( project ) => project . status === 'active' ) ;
136+ this . projects . sort ( ( a , b ) => {
137+ const x = a . customer . name . toLowerCase ( ) ;
138+ const y = b . customer . name . toLowerCase ( ) ;
139+ if ( x > y ) {
140+ return 1 ;
141+ }
142+ if ( x < y ) {
143+ return - 1 ;
144+ }
145+ return 0 ;
146+ } ) ;
121147 this . projects . forEach ( ( project ) => {
122148 const projectWithSearchField = { ...project } ;
123149 projectWithSearchField . search_field = `${ project . customer . name } - ${ project . name } ` ;
@@ -126,17 +152,29 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
126152 } ) ;
127153 }
128154
155+ uploadActivities ( ) : void {
156+ this . storeActivity . dispatch ( new LoadActivities ( ) ) ;
157+ this . actionsSubject$
158+ . pipe ( filter ( ( action : any ) => action . type === ActivityManagementActionTypes . LOAD_ACTIVITIES_SUCCESS ) )
159+ . subscribe ( ( action ) => {
160+ const sortActivities = [ ...action . payload ] ;
161+ sortActivities . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
162+ this . activities = sortActivities ;
163+ } ) ;
164+ }
165+
129166 ngOnInit ( ) : void {
130167 this . rerenderTableSubscription = this . reportDataSource$ . subscribe ( ( ds ) => {
131168 this . totalHoursSubscription = this . resultSumEntriesSelected$ . subscribe ( ( actTotalHours ) => {
132- this . resultSumEntriesSelected = actTotalHours ;
133- this . totalTimeSelected = moment . duration ( 0 ) ;
134- } ) ;
169+ this . resultSumEntriesSelected = actTotalHours ;
170+ this . totalTimeSelected = moment . duration ( 0 ) ;
171+ } ) ;
135172 this . sumDates ( ds . data ) ;
136173 this . rerenderDataTable ( ) ;
137174 } ) ;
138175 this . uploadUsers ( ) ;
139176 this . uploadProjects ( ) ;
177+ this . uploadActivities ( ) ;
140178 }
141179
142180 ngAfterViewInit ( ) : void {
@@ -172,20 +210,17 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
172210 return data . toString ( ) . replace ( / < ( ( .| \n ) { 0 , 200 } ?) > / gi, '' ) || '' ;
173211 }
174212
175-
176213 sumDates ( arrayData : Entry [ ] ) : TotalHours {
177214 this . resultSum = new TotalHours ( ) ;
178215 const arrayDurations = new Array ( ) ;
179- arrayData . forEach ( entry => {
216+ arrayData . forEach ( ( entry ) => {
180217 const start = moment ( entry . end_date ) . diff ( moment ( entry . start_date ) ) ;
181218 arrayDurations . push ( moment . utc ( start ) . format ( 'HH:mm:ss' ) ) ;
182219 } ) ;
183220
184- const totalDurations = arrayDurations . slice ( 1 )
185- . reduce ( ( prev , cur ) => {
186- return prev . add ( cur ) ;
187- } ,
188- moment . duration ( arrayDurations [ 0 ] ) ) ;
221+ const totalDurations = arrayDurations . slice ( 1 ) . reduce ( ( prev , cur ) => {
222+ return prev . add ( cur ) ;
223+ } , moment . duration ( arrayDurations [ 0 ] ) ) ;
189224 const daysInHours = totalDurations . days ( ) * 24 ;
190225 this . resultSum . hours = totalDurations . hours ( ) + daysInHours ;
191226 this . resultSum . minutes = totalDurations . minutes ( ) ;
@@ -201,7 +236,11 @@ export class TimeEntriesTableComponent implements OnInit, OnDestroy, AfterViewIn
201236 this . selectedProjectId . emit ( projectId ) ;
202237 }
203238
204- sumHoursEntriesSelected ( entry : Entry , checked : boolean ) {
239+ activity ( activityId : string ) {
240+ this . selectedActivityId . emit ( activityId ) ;
241+ }
242+
243+ sumHoursEntriesSelected ( entry : Entry , checked : boolean ) {
205244 this . resultSumEntriesSelected = new TotalHours ( ) ;
206245 const duration = moment . duration ( moment ( entry . end_date ) . diff ( moment ( entry . start_date ) ) ) ;
207246 this . totalTimeSelected = checked ? this . totalTimeSelected . add ( duration ) : this . totalTimeSelected . subtract ( duration ) ;
0 commit comments