2018-03-14 20:06:21 +00:00
import { Component , OnInit , EventEmitter , TemplateRef } from '@angular/core' ;
2017-09-06 16:05:35 +01:00
import { ApiService } from '../providers/api-service' ;
2018-03-14 20:06:21 +00:00
import { BsModalService , ModalDirective } from 'ngx-bootstrap/modal' ;
import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service' ;
import { PaginationInstance } from 'ngx-pagination' ;
2017-09-06 16:05:35 +01:00
import * as moment from 'moment' ;
2018-06-04 15:23:16 +01:00
2017-09-06 16:05:35 +01:00
@Component ( {
templateUrl : 'transaction-log.component.html' ,
} )
2017-09-19 15:02:48 +01:00
export class TransactionLogComponent implements OnInit {
2017-09-06 16:05:35 +01:00
2018-03-14 20:06:21 +00:00
transactionList : any ;
recurringTransactionList : any ;
2017-09-06 16:05:35 +01:00
noTransactionList = true ;
2018-03-12 17:41:01 +00:00
noRecurringList = true ;
2017-09-06 16:05:35 +01:00
myDate : any ;
minDate : any ;
2017-09-19 15:02:48 +01:00
public p : any ;
2018-03-14 20:06:21 +00:00
public modalRef : BsModalRef ;
clickedRecur : any ;
public updatedDate : string ;
public startTime : string ;
2018-03-20 19:04:05 +00:00
categoryIdList : any ;
2018-03-19 17:52:54 +00:00
categoryList : any ;
2018-03-14 20:06:21 +00:00
categoryNameList : string [ ] = [ ] ;
2018-03-20 16:53:29 +00:00
transactionFormStatus : string ;
2018-03-20 17:29:00 +00:00
transactionFormStatusSuccess : string ;
2018-03-20 16:53:29 +00:00
transactionFormStatusError = 'Error received, please try again.' ;
2018-03-20 19:04:05 +00:00
updatedTime : string ;
2017-09-06 17:51:56 +01:00
public paginateConfig : PaginationInstance = {
2019-07-12 18:50:59 +01:00
id : 'transpaginate' ,
itemsPerPage : 10 ,
currentPage : 1 ,
totalItems : 0
} ;
2017-09-06 16:05:35 +01:00
constructor (
2017-09-19 15:02:48 +01:00
private api : ApiService ,
2018-03-14 20:06:21 +00:00
private modalService : BsModalService ,
2017-09-19 15:02:48 +01:00
) {
2017-09-06 16:05:35 +01:00
this . myDate = moment ( ) . format ( 'YYYY-MM-DD[T]HH:mm' ) ;
2018-03-14 20:06:21 +00:00
this . api . categoryList ( ) . subscribe (
result = > {
2018-03-19 17:52:54 +00:00
this . categoryList = result . categories ;
this . categoryIdList = Object . keys ( this . categoryList ) ;
2018-03-14 20:06:21 +00:00
} ,
error = > {
console . log ( 'Retrieval Error' ) ;
console . log ( error . _body ) ;
}
) ;
2017-09-06 16:05:35 +01:00
// this.myDate = new Date().toISOString().slice(0, 16);
}
ngOnInit ( ) : void {
2017-09-06 17:51:56 +01:00
this . loadTransactions ( 1 ) ;
2017-09-06 16:05:35 +01:00
}
2017-09-06 17:51:56 +01:00
loadTransactions ( logPage : number ) {
this . api . transList ( logPage ) . subscribe (
2017-09-06 16:05:35 +01:00
result = > {
2017-09-19 15:02:48 +01:00
if ( result . transactions . length > 0 ) {
2017-09-06 16:05:35 +01:00
this . transactionList = result . transactions ;
2017-09-19 15:02:48 +01:00
// TODO Rename in server
2017-09-06 17:51:56 +01:00
this . paginateConfig . totalItems = result . page_no ;
this . paginateConfig . currentPage = logPage ;
2017-09-06 16:05:35 +01:00
this . noTransactionList = false ;
} else {
// handle the case when the transactionList is empty
this . transactionList = null ;
this . noTransactionList = true ;
}
2018-03-14 20:06:21 +00:00
if ( result . recurring_transactions ) {
this . recurringTransactionList = result . recurring_transactions ;
this . noRecurringList = false ;
} else {
this . recurringTransactionList = null ;
this . noRecurringList = true ;
}
} ,
error = > {
console . log ( error ) ;
}
) ;
}
2019-07-11 15:17:50 +01:00
2018-03-14 20:06:21 +00:00
recurringTransactionDetails ( clicked , template : TemplateRef < any > ) {
this . clickedRecur = clicked ;
2018-03-20 16:53:29 +00:00
this . updatedTime = moment ( this . clickedRecur . display_time , 'llll' ) . format ( 'YYYY-MM-DD[T]HH:mm' ) ;
2018-03-14 20:06:21 +00:00
this . openModal ( template ) ;
}
openModal ( template : TemplateRef < any > ) {
this . modalRef = this . modalService . show ( template ) ;
}
editRecurringTransaction() {
2018-03-20 16:53:29 +00:00
let updatedTimeSubmit = moment ( this . updatedTime , 'YYYY-MM-DD[T]HH:mm' ) . local ( ) . format ( 'YYYY-MM-DD[T]HH:mm:ss.SSSZ' ) ;
this . clickedRecur . display_time = moment ( this . updatedTime ) . format ( 'llll' ) ;
2018-03-14 20:06:21 +00:00
let myParams = {
2018-03-20 17:41:03 +00:00
category : ( this . clickedRecur . category == 0 ? undefined : this . clickedRecur . category ) ,
2018-03-14 20:06:21 +00:00
essential : this.clickedRecur.essential ,
id : this.clickedRecur.id ,
2018-03-20 16:53:29 +00:00
apply_time : updatedTimeSubmit ,
recurring_period : this.clickedRecur.recurring_period ,
2018-03-14 20:06:21 +00:00
seller : this.clickedRecur.seller ,
value : this.clickedRecur.value ,
} ;
this . api
2018-03-20 16:53:29 +00:00
. recurUpdate ( myParams )
2018-03-14 20:06:21 +00:00
. subscribe (
result = > {
if ( result . success === true ) {
2018-03-20 16:53:29 +00:00
this . transactionFormStatus = 'success' ;
2018-03-20 17:41:03 +00:00
this . transactionFormStatusSuccess = 'Edit Succeeded.' ;
2018-03-14 20:06:21 +00:00
} else {
2018-03-20 16:53:29 +00:00
this . transactionFormStatusError = JSON . stringify ( result . status ) + 'Error, ' + JSON . stringify ( result . message ) ;
this . transactionFormStatus = 'send_failed' ;
2018-03-14 20:06:21 +00:00
}
2017-09-06 16:05:35 +01:00
} ,
error = > {
2018-03-20 16:53:29 +00:00
console . log ( error ) ;
try {
this . transactionFormStatusError = '"' + error . error . error + '" Error, ' + error . error . message ;
} catch ( e ) {
this . transactionFormStatusError = 'There was a server error, please try again later.' ;
}
this . transactionFormStatus = 'send_failed' ;
2017-09-06 16:05:35 +01:00
}
) ;
}
2018-03-20 16:53:29 +00:00
deleteRecurringTransaction() {
2018-03-20 17:29:00 +00:00
let myParams = {
id : this.clickedRecur.id ,
} ;
this . api
. recurDelete ( myParams )
. subscribe (
result = > {
if ( result . success === true ) {
this . transactionFormStatus = 'success' ;
this . transactionFormStatusSuccess = 'Delete Succeeded.' ;
} else {
this . transactionFormStatusError = JSON . stringify ( result . status ) + 'Error, ' + JSON . stringify ( result . message ) ;
this . transactionFormStatus = 'send_failed' ;
}
} ,
error = > {
console . log ( error ) ;
try {
this . transactionFormStatusError = '"' + error . error . error + '" Error, ' + error . error . message ;
} catch ( e ) {
this . transactionFormStatusError = 'There was a server error, please try again later.' ;
}
this . transactionFormStatus = 'send_failed' ;
}
) ;
2018-03-20 16:53:29 +00:00
}
2017-09-06 16:05:35 +01:00
}