perbaikan master dan ngecek token jika ada kegiatan

This commit is contained in:
Fuzi_fauzia 2024-07-16 11:39:44 +07:00
parent 751c36209e
commit dd1deb0812
10 changed files with 501 additions and 240 deletions

View File

@ -86,7 +86,7 @@ export class MasterBuildingComponent {
if (result) { if (result) {
if ( if (
this.filteredRows.some( 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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
@ -127,23 +127,57 @@ export class MasterBuildingComponent {
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
if (result) { if (result) {
console.log(result); if (
console.log(this.filteredRows); this.filteredRows.some(
(value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
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, if (row.name.trim().toLowerCase() === result.name.trim().toLowerCase()) {
closeButton: true, 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 { } else {
this.monitoringApiService this.monitoringApiService
.putMasterBuildingParam(result, row.id) .putMasterBuildingParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
this.fetchData(); 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) { // if (result) {
// this.monitoringApiService // this.monitoringApiService
// .putMasterBuildingParam(result, row.id) // .putMasterBuildingParam(result, row.id)
@ -165,6 +199,12 @@ export class MasterBuildingComponent {
.deleteMasterBuildingParam(row.id) .deleteMasterBuildingParam(row.id)
.subscribe((res) => { .subscribe((res) => {
this.fetchData(); this.fetchData();
}, (error) => {
console.error(error);
this.toastr.error("Error", "Data sedang digunakan!", {
timeOut: 2000,
closeButton: true,
});
}); });
} }
} }

View File

@ -29,11 +29,13 @@ export class MasterCategoryComponent implements OnInit {
private router: Router, private router: Router,
private monitoringApiService: BuildingService, private monitoringApiService: BuildingService,
private toastr: ToastrService, private toastr: ToastrService,
private authService: LoginService, private authService: LoginService
) {} ) {}
ngOnInit() { ngOnInit() {
this.authService.checkTokenAndRedirect(); // this.authService.checkTokenAndRedirect();
this.authService.startTokenCheck();
this.authService.startTrackingActivity();
this.breadcrumb = { this.breadcrumb = {
mainlabel: "Master Category", mainlabel: "Master Category",
links: [ links: [
@ -57,7 +59,9 @@ export class MasterCategoryComponent implements OnInit {
(item) => item.name === "master_category" (item) => item.name === "master_category"
); );
// this.filteredRows = this.dataMasterCategori.headerDetailParam; // 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 { rowContainsSearchTerm(row: any): boolean {
const searchTermLC = this.searchTerm.toLowerCase(); const searchTermLC = this.searchTerm.toLowerCase();
console.log(searchTermLC); console.log(searchTermLC);
return Object.values(row).some( return Object.values(row).some(
(value) => (value) =>
value !== null && value.toString().toLowerCase().includes(searchTermLC) value !== null && value.toString().toLowerCase().includes(searchTermLC)
@ -87,8 +91,8 @@ export class MasterCategoryComponent implements OnInit {
openAddMasterModal() { openAddMasterModal() {
const modalRef = this.modalService.open(AddEditMasterComponent, { const modalRef = this.modalService.open(AddEditMasterComponent, {
size: "lg", size: "lg",
backdrop: 'static', // Add this line backdrop: "static", // Add this line
keyboard: false // Add this line keyboard: false, // Add this line
}); });
modalRef.componentInstance.headerId = this.dataMasterCategori.id; modalRef.componentInstance.headerId = this.dataMasterCategori.id;
@ -96,41 +100,25 @@ export class MasterCategoryComponent implements OnInit {
modalRef.componentInstance.category = true; modalRef.componentInstance.category = true;
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
}); });
} else { } else {
this.monitoringApiService this.monitoringApiService
.postHeaderDetailParam(result) .postHeaderDetailParam(result)
.subscribe((res) => { .subscribe((res) => {
console.log(res); console.log(res);
this.fetchData(); 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) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -139,10 +127,12 @@ export class MasterCategoryComponent implements OnInit {
} }
editRow(row) { editRow(row) {
console.log(row);
const modalRef = this.modalService.open(AddEditMasterComponent, { const modalRef = this.modalService.open(AddEditMasterComponent, {
size: "lg", size: "lg",
backdrop: 'static', // Add this line backdrop: "static",
keyboard: false // Add this line keyboard: false,
}); });
modalRef.componentInstance.headerId = this.dataMasterCategori.id; modalRef.componentInstance.headerId = this.dataMasterCategori.id;
@ -151,42 +141,39 @@ export class MasterCategoryComponent implements OnInit {
modalRef.componentInstance.category = true; modalRef.componentInstance.category = true;
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
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) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -207,7 +194,7 @@ export class MasterCategoryComponent implements OnInit {
}, },
(error) => { (error) => {
console.error(error); console.error(error);
this.toastr.error("Error", "Something went wrong!", { this.toastr.error("Error", "Data sedang digunakan!", {
timeOut: 2000, timeOut: 2000,
closeButton: true, closeButton: true,
}); });

View File

@ -30,7 +30,9 @@ export class MasterDurationUseComponent {
) {} ) {}
ngOnInit() { ngOnInit() {
this.authService.checkTokenAndRedirect(); // this.authService.checkTokenAndRedirect();
this.authService.startTokenCheck();
this.authService.startTrackingActivity();
this.breadcrumb = { this.breadcrumb = {
mainlabel: "Master Duration Use", mainlabel: "Master Duration Use",
links: [ links: [
@ -93,7 +95,7 @@ export class MasterDurationUseComponent {
console.log(result); console.log(result);
if (result) { if (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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
@ -133,30 +135,39 @@ export class MasterDurationUseComponent {
modalRef.componentInstance.mode = "edit"; modalRef.componentInstance.mode = "edit";
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
// if (result) {
// this.monitoringApiService
// .putHeaderDetailParam(result, row.id)
// .subscribe((res) => {
// console.log(res);
// this.fetchData();
// });
// }
}, },
(reason) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -167,11 +178,22 @@ export class MasterDurationUseComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -133,30 +133,39 @@ export class MasterFloorComponent {
modalRef.componentInstance.mode = "edit"; modalRef.componentInstance.mode = "edit";
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
// if (result) {
// this.monitoringApiService
// .putHeaderDetailParam(result, row.id)
// .subscribe((res) => {
// console.log(res);
// this.fetchData();
// });
// }
}, },
(reason) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -167,11 +176,22 @@ export class MasterFloorComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -92,7 +92,7 @@ export class MasterRoleComponent {
(result) => { (result) => {
console.log(result); 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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
@ -133,30 +133,39 @@ export class MasterRoleComponent {
modalRef.componentInstance.mode = "edit"; modalRef.componentInstance.mode = "edit";
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
// if (result) {
// this.monitoringApiService
// .putHeaderDetailParam(result, row.id)
// .subscribe((res) => {
// console.log(res);
// this.fetchData();
// });
// }
}, },
(reason) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -167,11 +176,22 @@ export class MasterRoleComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -92,7 +92,7 @@ export class MasterStatusComponent {
(result) => { (result) => {
console.log(result); 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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
@ -133,30 +133,39 @@ export class MasterStatusComponent {
modalRef.componentInstance.mode = "edit"; modalRef.componentInstance.mode = "edit";
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status)) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
// if (result) {
// this.monitoringApiService
// .putHeaderDetailParam(result, row.id)
// .subscribe((res) => {
// console.log(res);
// this.fetchData();
// });
// }
}, },
(reason) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -167,11 +176,22 @@ export class MasterStatusComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -91,7 +91,7 @@ export class MasterTypeComponent {
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
if (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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
@ -133,28 +133,38 @@ export class MasterTypeComponent {
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase())) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
// if (result) {
// this.monitoringApiService
// .putHeaderDetailParam(result, row.id)
// .subscribe((res) => {
// console.log(res);
// this.fetchData();
// });
// }
}, },
(reason) => { (reason) => {
console.log(`Dismissed: ${reason}`); console.log(`Dismissed: ${reason}`);
@ -165,11 +175,22 @@ export class MasterTypeComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -91,7 +91,7 @@ export class MasterVoltageComponent {
modalRef.result.then( modalRef.result.then(
(result) => { (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.", { this.toastr.error("Warning", "Data yang anda masukan double.", {
timeOut: 5000, timeOut: 5000,
closeButton: true, closeButton: true,
@ -124,20 +124,37 @@ export class MasterVoltageComponent {
modalRef.componentInstance.mode = "edit"; modalRef.componentInstance.mode = "edit";
modalRef.result.then( modalRef.result.then(
(result) => { (result) => {
console.log(result);
if (result) { if (result) {
if (this.filteredRows.some(value => value.name.toLowerCase() === result.name.toLowerCase() && value.status === result.status) ) { if (
this.toastr.error("Warning", "Data yang anda masukan double.", { this.filteredRows.some(
timeOut: 5000, (value) => value.name.trim().toLowerCase() === result.name.trim().toLowerCase()
closeButton: true, )
}); ) {
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 { } else {
this.monitoringApiService this.monitoringApiService
.putHeaderDetailParam(result, row.id) .putHeaderDetailParam(result, row.id)
.subscribe((res) => { .subscribe((res) => {
console.log(res); this.fetchData();
this.fetchData(); });
});
} }
} }
}, },
@ -150,11 +167,22 @@ export class MasterVoltageComponent {
deleteRow(row) { deleteRow(row) {
const confirmDelete = confirm("Are you sure you want to delete this item?"); const confirmDelete = confirm("Are you sure you want to delete this item?");
if (confirmDelete) { if (confirmDelete) {
this.monitoringApiService this.monitoringApiService.deleteHeaderDetailParam(row.id).subscribe(
.deleteHeaderDetailParam(row.id) (res) => {
.subscribe((res) => {
this.fetchData(); 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,
});
}
);
} }
} }
} }

View File

@ -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 { HttpClient, HttpHeaders } from "@angular/common/http";
import { BehaviorSubject, Observable } from "rxjs"; import { BehaviorSubject, Observable, of, Subject, timer, fromEvent, merge } from "rxjs";
import { jwtDecode } from "jwt-decode"; import {jwtDecode} from "jwt-decode";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { switchMap, debounceTime, mapTo, startWith } from "rxjs/operators";
import { AuthService } from "src/app/_services/auth.service"; import { AuthService } from "src/app/_services/auth.service";
const BASE_URL = "https://kapi.absys.ninja/hemat"; const BASE_URL = "https://kapi.absys.ninja/hemat";
@ -23,17 +25,29 @@ interface CustomJwtPayload {
}) })
export class LoginService { export class LoginService {
private readonly tokenKey = "currentUser"; private readonly tokenKey = "currentUser";
private tabSelected = new BehaviorSubject(null); private tabSelected = new BehaviorSubject<string | null>(null);
public _tabSelected = this.tabSelected.asObservable(); public _tabSelected = this.tabSelected.asObservable();
private activitySubject = new Subject<boolean>();
public activity$: Observable<boolean> = this.activitySubject.asObservable();
currentUser: any;
constructor( constructor(
private http: HttpClient, private http: HttpClient,
private router: Router, 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); this.tabSelected.next(e);
} }
updatePassword(data: any): Observable<any> { updatePassword(data: any): Observable<any> {
const endpoint = `/users`; const endpoint = `/users`;
const url = `${BASE_URL}${endpoint}/reset-password`; const url = `${BASE_URL}${endpoint}/reset-password`;
@ -44,7 +58,7 @@ export class LoginService {
return this.http.put<any>(url, data, { headers }); return this.http.put<any>(url, data, { headers });
} }
getDataProfil(id): Observable<any> { getDataProfil(id: string): Observable<any> {
const endpoint = `/users`; const endpoint = `/users`;
const url = `${BASE_URL}${endpoint}/byUserid/${id}`; const url = `${BASE_URL}${endpoint}/byUserid/${id}`;
const headers = new HttpHeaders({ const headers = new HttpHeaders({
@ -61,7 +75,7 @@ export class LoginService {
"x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT", "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
}); });
return this.http.post(`${url}`, data, { headers }); return this.http.post<any>(`${url}`, data, { headers });
} }
updateUserProfile(data: any): Observable<any> { updateUserProfile(data: any): Observable<any> {
@ -72,36 +86,92 @@ export class LoginService {
"x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT", "x-api-key": "j2yaYvPSQcsEEmHh3NEobfiXyyXmmnHT",
}); });
return this.http.post(`${url}`, body, { headers }); return this.http.post<any>(`${url}`, body, { headers });
} }
isTokenExpired(): boolean { isTokenExpired(): Observable<boolean> {
const tokenData = localStorage.getItem(this.tokenKey); const tokenData = localStorage.getItem(this.tokenKey);
if (tokenData) { if (tokenData) {
const tokenInfo = JSON.parse(tokenData); const tokenInfo = JSON.parse(tokenData);
const decodedToken = jwtDecode<CustomJwtPayload>(tokenInfo.refresh_token); const decodedToken = jwtDecode<CustomJwtPayload>(tokenInfo.refresh_token);
const expiryDate = decodedToken.exp * 1000; const expiryDate = decodedToken.exp * 1000;
const now = new Date().getTime(); 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 { checkTokenAndRedirect(): void {
if (this.isTokenExpired()) { this.isTokenExpired().subscribe((isExpired) => {
// Token sudah kedaluwarsa, arahkan ke halaman login if (isExpired) {
// console.log("Token expired, redirecting to login page..."); this.authService.doLogout().then(
this.logoutService.doLogout().then( () => {
(res) => { this.router.navigate(["/login"]);
this.router.navigate(["/login"]); },
}, (err) => {
(err) => { console.log(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<CustomJwtPayload>(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 { activityEvents$
console.log("Token is valid, continuing..."); .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));
});
});
} }
} }

View File

@ -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<boolean>();
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<boolean> {
return this.activitySubject.asObservable();
}
}