@@ -25,24 +25,21 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
2525 message : string ;
2626 idToDelete : string ;
2727 entriesSubscription : Subscription ;
28+ wasEditingExistingTimeEntry = false ;
2829 canMarkEntryAsWIP = true ;
2930 timeEntriesDataSource$ : Observable < DataSource < Entry > > ;
3031 selectedYearAsText : string ;
3132 selectedMonth : number ;
3233 selectedYear : number ;
3334 selectedMonthAsText : string ;
34-
3535 constructor ( private store : Store < EntryState > , private toastrService : ToastrService , private actionsSubject$ : ActionsSubject ) {
3636 this . timeEntriesDataSource$ = this . store . pipe ( delay ( 0 ) , select ( getTimeEntriesDataSource ) ) ;
3737 }
38-
3938 ngOnDestroy ( ) : void {
4039 this . entriesSubscription . unsubscribe ( ) ;
4140 }
42-
4341 ngOnInit ( ) : void {
4442 this . loadActiveEntry ( ) ;
45-
4643 this . entriesSubscription = this . actionsSubject$ . pipe (
4744 filter ( ( action : any ) => (
4845 action . type === EntryActionTypes . CREATE_ENTRY_SUCCESS ||
@@ -55,41 +52,38 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
5552 this . store . dispatch ( new entryActions . LoadEntries ( this . selectedMonth , this . selectedYear ) ) ;
5653 } ) ;
5754 }
58-
5955 newEntry ( ) {
60- this . entry = null ;
56+ if ( this . wasEditingExistingTimeEntry ) {
57+ this . entry = null ;
58+ }
6159 this . entryId = null ;
6260 this . store . pipe ( select ( getTimeEntriesDataSource ) ) . subscribe ( ds => {
6361 this . canMarkEntryAsWIP = ! this . isThereAnEntryRunning ( ds . data ) ;
6462 } ) ;
6563 }
66-
6764 private getEntryRunning ( entries : Entry [ ] ) {
6865 const runningEntry : Entry = entries . find ( entry => entry . running === true ) ;
6966 return runningEntry ;
7067 }
71-
7268 private isThereAnEntryRunning ( entries : Entry [ ] ) {
7369 return ! ! this . getEntryRunning ( entries ) ;
7470 }
75-
7671 editEntry ( entryId : string ) {
7772 this . entryId = entryId ;
7873 this . store . pipe ( select ( getTimeEntriesDataSource ) ) . subscribe ( ds => {
7974 this . entry = ds . data . find ( ( entry ) => entry . id === entryId ) ;
8075 this . canMarkEntryAsWIP = this . isEntryRunningEqualsToEntryToEdit ( this . getEntryRunning ( ds . data ) , this . entry )
8176 || this . isTheEntryToEditTheLastOne ( ds . data ) ;
8277 } ) ;
78+ this . wasEditingExistingTimeEntry = true ;
8379 }
84-
8580 private isEntryRunningEqualsToEntryToEdit ( entryRunning : Entry , entryToEdit : Entry ) {
8681 if ( entryRunning && entryToEdit ) {
8782 return entryRunning . id === entryToEdit . id ;
8883 } else {
8984 return false ;
9085 }
9186 }
92-
9387 private isTheEntryToEditTheLastOne ( entries : Entry [ ] ) {
9488 if ( entries && entries . length > 0 ) {
9589 const lastEntry = entries [ 0 ] ;
@@ -98,11 +92,9 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
9892 return false ;
9993 }
10094 }
101-
10295 private isNewEntry ( ) {
10396 return this . entryId === null ;
10497 }
105-
10698 saveEntry ( event : SaveEntryEvent ) : void {
10799 if ( this . activeTimeEntry ) {
108100 const startDateAsLocalDate = new Date ( event . entry . start_date ) ;
@@ -121,8 +113,8 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
121113 this . doSave ( event ) ;
122114 }
123115 }
124-
125116 projectSelected ( event : ProjectSelectedEvent ) : void {
117+ this . wasEditingExistingTimeEntry = false ;
126118 this . store . pipe ( select ( getTimeEntriesDataSource ) ) . subscribe ( ds => {
127119 const dataToUse = ds . data . find ( item => item . project_id === event . projectId ) ;
128120 if ( dataToUse && this . isNewEntry ( ) ) {
@@ -140,7 +132,6 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
140132 }
141133 } ) ;
142134 }
143-
144135 doSave ( event : SaveEntryEvent ) {
145136 if ( this . entryId ) {
146137 event . entry . id = this . entryId ;
@@ -152,30 +143,26 @@ export class TimeEntriesComponent implements OnInit, OnDestroy {
152143 this . store . dispatch ( new entryActions . CreateEntry ( event . entry ) ) ;
153144 }
154145 }
155-
156146 loadActiveEntry ( ) {
157147 this . store . dispatch ( new entryActions . LoadActiveEntry ( ) ) ;
158148 this . store . pipe ( select ( getActiveTimeEntry ) ) . subscribe ( ( activeTimeEntry ) => {
159149 this . activeTimeEntry = activeTimeEntry ;
160150 } ) ;
161151 }
162-
163152 removeEntry ( ) {
164153 this . store . dispatch ( new entryActions . DeleteEntry ( this . idToDelete ) ) ;
165154 this . showModal = false ;
166155 }
167-
168156 dateSelected ( event : { monthIndex : number ; year : number } ) {
169157 this . selectedYear = event . year ;
170158 this . selectedYearAsText = event . year . toString ( ) ;
171159 this . selectedMonth = event . monthIndex + 1 ;
172160 this . selectedMonthAsText = moment ( ) . month ( event . monthIndex ) . format ( 'MMMM' ) ;
173161 this . store . dispatch ( new entryActions . LoadEntries ( this . selectedMonth , this . selectedYear ) ) ;
174162 }
175-
176163 openModal ( item : any ) {
177164 this . idToDelete = item . id ;
178165 this . message = `Are you sure you want to delete ${ item . activity_name } ?` ;
179166 this . showModal = true ;
180167 }
181- }
168+ }
0 commit comments