diff --git a/src/app/content/hemat-app/master/master-building/master-building.component.ts b/src/app/content/hemat-app/master/master-building/master-building.component.ts index fa9ab21..5c35d08 100644 --- a/src/app/content/hemat-app/master/master-building/master-building.component.ts +++ b/src/app/content/hemat-app/master/master-building/master-building.component.ts @@ -86,7 +86,7 @@ export class MasterBuildingComponent { if (result) { if ( this.filteredRows.some( - (value) => value.name.toLowerCase() === result.name.toLowerCase() + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() ) ) { this.toastr.error("Warning", "Data yang anda masukan double.", { @@ -127,23 +127,57 @@ export class MasterBuildingComponent { modalRef.result.then( (result) => { if (result) { - console.log(result); - console.log(this.filteredRows); - - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status_id === result.statusId)) { - this.toastr.error("Warning", "Nama Gedung yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putMasterBuildingParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putMasterBuildingParam(result, row.id) - .subscribe((res) => { - this.fetchData(); - }); + .putMasterBuildingParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } + // if (result) { + // console.log(result); + // console.log(this.filteredRows); + + // if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status_id === result.statusId)) { + // this.toastr.error("Warning", "Nama Gedung yang anda masukan double.", { + // timeOut: 5000, + // closeButton: true, + // }); + // } else { + // this.monitoringApiService + // .putMasterBuildingParam(result, row.id) + // .subscribe((res) => { + // this.fetchData(); + // }); + // } + // } + // if (result) { // this.monitoringApiService // .putMasterBuildingParam(result, row.id) @@ -165,6 +199,12 @@ export class MasterBuildingComponent { .deleteMasterBuildingParam(row.id) .subscribe((res) => { this.fetchData(); + }, (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); }); } } diff --git a/src/app/content/hemat-app/master/master-category/master-category.component.ts b/src/app/content/hemat-app/master/master-category/master-category.component.ts index d40e9fc..55228ff 100644 --- a/src/app/content/hemat-app/master/master-category/master-category.component.ts +++ b/src/app/content/hemat-app/master/master-category/master-category.component.ts @@ -29,11 +29,13 @@ export class MasterCategoryComponent implements OnInit { private router: Router, private monitoringApiService: BuildingService, private toastr: ToastrService, - private authService: LoginService, + private authService: LoginService ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + // this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Category", links: [ @@ -57,7 +59,9 @@ export class MasterCategoryComponent implements OnInit { (item) => item.name === "master_category" ); // this.filteredRows = this.dataMasterCategori.headerDetailParam; - this.filteredRows = this.dataMasterCategori.headerDetailParam.sort((a, b) => b.id - a.id); + this.filteredRows = this.dataMasterCategori.headerDetailParam.sort( + (a, b) => b.id - a.id + ); }); } @@ -77,7 +81,7 @@ export class MasterCategoryComponent implements OnInit { rowContainsSearchTerm(row: any): boolean { const searchTermLC = this.searchTerm.toLowerCase(); console.log(searchTermLC); - + return Object.values(row).some( (value) => value !== null && value.toString().toLowerCase().includes(searchTermLC) @@ -87,8 +91,8 @@ export class MasterCategoryComponent implements OnInit { openAddMasterModal() { const modalRef = this.modalService.open(AddEditMasterComponent, { size: "lg", - backdrop: 'static', // Add this line - keyboard: false // Add this line + backdrop: "static", // Add this line + keyboard: false, // Add this line }); modalRef.componentInstance.headerId = this.dataMasterCategori.id; @@ -96,41 +100,25 @@ export class MasterCategoryComponent implements OnInit { modalRef.componentInstance.category = true; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, }); } else { this.monitoringApiService - .postHeaderDetailParam(result) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .postHeaderDetailParam(result) + .subscribe((res) => { + console.log(res); + this.fetchData(); + }); } } - - // if (result) { - // this.monitoringApiService.postHeaderDetailParam(result).subscribe( - // (res) => { - // this.fetchData(); - // this.toastr.success("Success", "Save Completed.", { - // timeOut: 2000, - // closeButton: true, - // }); - // }, - // (error) => { - // console.error(error); - // this.toastr.error("Error", "Something went wrong!", { - // timeOut: 2000, - // closeButton: true, - // }); - // } - // ); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -139,10 +127,12 @@ export class MasterCategoryComponent implements OnInit { } editRow(row) { + console.log(row); + const modalRef = this.modalService.open(AddEditMasterComponent, { size: "lg", - backdrop: 'static', // Add this line - keyboard: false // Add this line + backdrop: "static", + keyboard: false, }); modalRef.componentInstance.headerId = this.dataMasterCategori.id; @@ -151,42 +141,39 @@ export class MasterCategoryComponent implements OnInit { modalRef.componentInstance.category = true; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe( - // (res) => { - // this.fetchData(); - // this.toastr.success("Success", "Update Completed.", { - // timeOut: 2000, - // closeButton: true, - // }); - // }, - // (error) => { - // console.error(error); - // this.toastr.error("Error", "Something went wrong!", { - // timeOut: 2000, - // closeButton: true, - // }); - // } - // ); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -207,7 +194,7 @@ export class MasterCategoryComponent implements OnInit { }, (error) => { console.error(error); - this.toastr.error("Error", "Something went wrong!", { + this.toastr.error("Error", "Data sedang digunakan!", { timeOut: 2000, closeButton: true, }); diff --git a/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts b/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts index bb87a87..88148f1 100644 --- a/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts +++ b/src/app/content/hemat-app/master/master-duration-use/master-duration-use.component.ts @@ -30,7 +30,9 @@ export class MasterDurationUseComponent { ) {} ngOnInit() { - this.authService.checkTokenAndRedirect(); + // this.authService.checkTokenAndRedirect(); + this.authService.startTokenCheck(); + this.authService.startTrackingActivity(); this.breadcrumb = { mainlabel: "Master Duration Use", links: [ @@ -93,7 +95,7 @@ export class MasterDurationUseComponent { console.log(result); if (result) { if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { + if (this.filteredRows.some(value => value.name.trim().toLowerCase() === result.name.trim().toLowerCase())) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, @@ -133,30 +135,39 @@ export class MasterDurationUseComponent { modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe((res) => { - // console.log(res); - // this.fetchData(); - // }); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -167,11 +178,22 @@ export class MasterDurationUseComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/master/master-floor/master-floor.component.ts b/src/app/content/hemat-app/master/master-floor/master-floor.component.ts index a9dcc20..261f7df 100644 --- a/src/app/content/hemat-app/master/master-floor/master-floor.component.ts +++ b/src/app/content/hemat-app/master/master-floor/master-floor.component.ts @@ -133,30 +133,39 @@ export class MasterFloorComponent { modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe((res) => { - // console.log(res); - // this.fetchData(); - // }); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -167,11 +176,22 @@ export class MasterFloorComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/master/master-role/master-role.component.ts b/src/app/content/hemat-app/master/master-role/master-role.component.ts index c9d1214..34a080e 100644 --- a/src/app/content/hemat-app/master/master-role/master-role.component.ts +++ b/src/app/content/hemat-app/master/master-role/master-role.component.ts @@ -92,7 +92,7 @@ export class MasterRoleComponent { (result) => { console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { + if (this.filteredRows.some(value => value.name.trim().toLowerCase() === result.name.trim().toLowerCase())) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, @@ -133,30 +133,39 @@ export class MasterRoleComponent { modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe((res) => { - // console.log(res); - // this.fetchData(); - // }); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -167,11 +176,22 @@ export class MasterRoleComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/master/master-status/master-status.component.ts b/src/app/content/hemat-app/master/master-status/master-status.component.ts index fd3caec..d241e84 100644 --- a/src/app/content/hemat-app/master/master-status/master-status.component.ts +++ b/src/app/content/hemat-app/master/master-status/master-status.component.ts @@ -92,7 +92,7 @@ export class MasterStatusComponent { (result) => { console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { + if (this.filteredRows.some(value => value.name.trim().toLowerCase() === result.name.trim().toLowerCase())) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, @@ -133,30 +133,39 @@ export class MasterStatusComponent { modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe((res) => { - // console.log(res); - // this.fetchData(); - // }); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -167,11 +176,22 @@ export class MasterStatusComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/master/master-type/master-type.component.ts b/src/app/content/hemat-app/master/master-type/master-type.component.ts index b058de3..7912de9 100644 --- a/src/app/content/hemat-app/master/master-type/master-type.component.ts +++ b/src/app/content/hemat-app/master/master-type/master-type.component.ts @@ -91,7 +91,7 @@ export class MasterTypeComponent { modalRef.result.then( (result) => { if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { + if (this.filteredRows.some(value => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() && value.status === result.status)) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, @@ -133,28 +133,38 @@ export class MasterTypeComponent { modalRef.result.then( (result) => { if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } - // if (result) { - // this.monitoringApiService - // .putHeaderDetailParam(result, row.id) - // .subscribe((res) => { - // console.log(res); - // this.fetchData(); - // }); - // } }, (reason) => { console.log(`Dismissed: ${reason}`); @@ -165,11 +175,22 @@ export class MasterTypeComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts b/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts index 9711f4a..598977e 100644 --- a/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts +++ b/src/app/content/hemat-app/master/master-voltage/master-voltage.component.ts @@ -91,7 +91,7 @@ export class MasterVoltageComponent { modalRef.result.then( (result) => { if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { + if (this.filteredRows.some(value => value.name.trim().toLowerCase() === result.name.trim().toLowerCase())) { this.toastr.error("Warning", "Data yang anda masukan double.", { timeOut: 5000, closeButton: true, @@ -124,20 +124,37 @@ export class MasterVoltageComponent { modalRef.componentInstance.mode = "edit"; modalRef.result.then( (result) => { - console.log(result); if (result) { - if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status) ) { - this.toastr.error("Warning", "Data yang anda masukan double.", { - timeOut: 5000, - closeButton: true, - }); + if ( + this.filteredRows.some( + (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) { + this.monitoringApiService + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); + } else { + if ( + this.filteredRows.some( + (value) => + value.name.trim().toLowerCase() === result.name.trim().toLowerCase() + ) + ) { + this.toastr.error("Warning", "Data yang anda masukan double.", { + timeOut: 5000, + closeButton: true, + }); + } + } } else { this.monitoringApiService - .putHeaderDetailParam(result, row.id) - .subscribe((res) => { - console.log(res); - this.fetchData(); - }); + .putHeaderDetailParam(result, row.id) + .subscribe((res) => { + this.fetchData(); + }); } } }, @@ -150,11 +167,22 @@ export class MasterVoltageComponent { deleteRow(row) { const confirmDelete = confirm("Are you sure you want to delete this item?"); if (confirmDelete) { - this.monitoringApiService - .deleteHeaderDetailParam(row.id) - .subscribe((res) => { + this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe( + (res) => { this.fetchData(); - }); + this.toastr.success("Success", "Delete Completed.", { + timeOut: 2000, + closeButton: true, + }); + }, + (error) => { + console.error(error); + this.toastr.error("Error", "Data sedang digunakan!", { + timeOut: 2000, + closeButton: true, + }); + } + ); } } } diff --git a/src/app/content/hemat-app/service/login.service.ts b/src/app/content/hemat-app/service/login.service.ts index d4ca243..809ad66 100644 --- a/src/app/content/hemat-app/service/login.service.ts +++ b/src/app/content/hemat-app/service/login.service.ts @@ -1,8 +1,10 @@ -import { Injectable } from "@angular/core"; +// src/app/services/login.service.ts +import { Injectable, NgZone } from "@angular/core"; import { HttpClient, HttpHeaders } from "@angular/common/http"; -import { BehaviorSubject, Observable } from "rxjs"; -import { jwtDecode } from "jwt-decode"; +import { BehaviorSubject, Observable, of, Subject, timer, fromEvent, merge } from "rxjs"; +import {jwtDecode} from "jwt-decode"; import { Router } from "@angular/router"; +import { switchMap, debounceTime, mapTo, startWith } from "rxjs/operators"; import { AuthService } from "src/app/_services/auth.service"; const BASE_URL = "https://kapi.absys.ninja/hemat"; @@ -23,17 +25,29 @@ interface CustomJwtPayload { }) export class LoginService { private readonly tokenKey = "currentUser"; - private tabSelected = new BehaviorSubject(null); + private tabSelected = new BehaviorSubject(null); public _tabSelected = this.tabSelected.asObservable(); + + private activitySubject = new Subject(); + public activity$: Observable = this.activitySubject.asObservable(); + + currentUser: any; + constructor( private http: HttpClient, private router: Router, - public logoutService: AuthService - ) {} + private authService: AuthService, + private ngZone: NgZone + ) { + this.startTrackingActivity(); + this.startTokenCheck(); + this.currentUser = JSON.parse(localStorage.getItem("currentUser")); + } - setTabsSelected(e: string){ + setTabsSelected(e: string) { this.tabSelected.next(e); } + updatePassword(data: any): Observable { const endpoint = `/users`; const url = `${BASE_URL}${endpoint}/reset-password`; @@ -44,7 +58,7 @@ export class LoginService { return this.http.put(url, data, { headers }); } - getDataProfil(id): Observable { + getDataProfil(id: string): Observable { const endpoint = `/users`; const url = `${BASE_URL}${endpoint}/byUserid/${id}`; const headers = new HttpHeaders({ @@ -61,7 +75,7 @@ export class LoginService { "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT", }); - return this.http.post(`${url}`, data, { headers }); + return this.http.post(`${url}`, data, { headers }); } updateUserProfile(data: any): Observable { @@ -72,36 +86,92 @@ export class LoginService { "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT", }); - return this.http.post(`${url}`, body, { headers }); + return this.http.post(`${url}`, body, { headers }); } - isTokenExpired(): boolean { + isTokenExpired(): Observable { const tokenData = localStorage.getItem(this.tokenKey); if (tokenData) { const tokenInfo = JSON.parse(tokenData); const decodedToken = jwtDecode(tokenInfo.refresh_token); const expiryDate = decodedToken.exp * 1000; const now = new Date().getTime(); - return now > expiryDate; + const timeLeft = expiryDate - now; + return of(timeLeft <= 5 * 60 * 1000); } - return true; + return of(true); } checkTokenAndRedirect(): void { - if (this.isTokenExpired()) { - // Token sudah kedaluwarsa, arahkan ke halaman login - // console.log("Token expired, redirecting to login page..."); - this.logoutService.doLogout().then( - (res) => { - this.router.navigate(["/login"]); - }, - (err) => { - console.log(err); + this.isTokenExpired().subscribe((isExpired) => { + if (isExpired) { + this.authService.doLogout().then( + () => { + this.router.navigate(["/login"]); + }, + (err) => { + console.log(err); + } + ); + this.router.navigate(["/login"]); + } else { + console.log("Token is valid, continuing..."); + } + }); + } + + startTokenCheck(): void { + timer(0, 2 * 60 * 1000) // Check every 5 minutes + .pipe( + switchMap(() => this.isTokenExpired()) + ) + .subscribe((isExpired) => { + if (isExpired) { + this.activity$.subscribe(isActive => { + if (!isActive) { + this.checkTokenAndRedirect(); + } else { + console.log("Token expired but user is active. Refresh token not implemented."); + this.updateUserProfile(this.currentUser.refresh_token) + .subscribe((resp) => { + const decodedToken = jwtDecode(resp.access_token); + localStorage.setItem("account_info", JSON.stringify(decodedToken)); + const userProfile = { + access_token: resp.access_token, + refresh_token: resp.refresh_token, + displayName: decodedToken.name, + buildingId: 4, + }; + localStorage.setItem("currentUser", JSON.stringify(userProfile)); + // window.location.reload(); + }); + } + }); } + }); + } + + startTrackingActivity(): void { + this.ngZone.runOutsideAngular(() => { + const activityEvents$ = merge( + fromEvent(window, 'mousemove'), + fromEvent(window, 'click'), + fromEvent(window, 'keypress'), + fromEvent(window, 'scroll') ); - this.router.navigate(["/login"]); - } else { - console.log("Token is valid, continuing..."); - } + + activityEvents$ + .pipe( + startWith(null), + switchMap(() => merge( + timer(0).pipe(mapTo(true)), + timer(5 * 60 * 1000).pipe(mapTo(false)) // 5 minutes of inactivity + )), + debounceTime(300) + ) + .subscribe(active => { + this.ngZone.run(() => this.activitySubject.next(active)); + }); + }); } } diff --git a/src/app/content/hemat-app/service/user-activity.service.ts b/src/app/content/hemat-app/service/user-activity.service.ts new file mode 100644 index 0000000..b87f236 --- /dev/null +++ b/src/app/content/hemat-app/service/user-activity.service.ts @@ -0,0 +1,33 @@ +import { Injectable, NgZone } from '@angular/core'; +import { Observable, Subject } from 'rxjs'; + +@Injectable({ + providedIn: 'root' +}) +export class UserActivityService { + private activitySubject = new Subject(); + private timeoutId: any; + + constructor(private ngZone: NgZone) { + this.setupActivityListeners(); + } + + private setupActivityListeners(): void { + const events = ['click', 'mousemove', 'keypress']; + events.forEach(event => { + window.addEventListener(event, () => this.resetTimeout(), true); + }); + } + + private resetTimeout(): void { + this.ngZone.runOutsideAngular(() => { + clearTimeout(this.timeoutId); + this.timeoutId = setTimeout(() => this.activitySubject.next(false), 300000); // 5 minutes + this.activitySubject.next(true); + }); + } + + get activity$(): Observable { + return this.activitySubject.asObservable(); + } +}