147 lines
4.2 KiB
TypeScript
147 lines
4.2 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { Storage } from '@ionic/storage-angular';
|
|
import { ToastController } from '@ionic/angular';
|
|
import { IdentitasServiceService } from '../services/identitas-service.service';
|
|
import { Router, NavigationEnd } from '@angular/router';
|
|
import { filter } from 'rxjs/operators';
|
|
|
|
@Component({
|
|
selector: 'app-identitas',
|
|
templateUrl: './identitas.page.html',
|
|
styleUrls: ['./identitas.page.scss'],
|
|
})
|
|
export class IdentitasPage implements OnInit {
|
|
public userData = {
|
|
nama: '',
|
|
tempat_lahir: '',
|
|
tanggal_lahir: '',
|
|
jenis_kelamin: '',
|
|
tinggi_badan: null,
|
|
berat_badan: null,
|
|
suku: '',
|
|
riwayat_perokok: '',
|
|
pekerjaan: '',
|
|
nomor_hp: '',
|
|
status_pernikahan: '',
|
|
alamat: '',
|
|
tanggal_pertama: ''
|
|
};
|
|
public isEdit = false;
|
|
private idIdentitas: number | null = null;
|
|
|
|
constructor(
|
|
private storage: Storage,
|
|
private toastController: ToastController,
|
|
private identitasService: IdentitasServiceService,
|
|
private router: Router
|
|
) {
|
|
// Subscribe to router events and refresh data on NavigationEnd
|
|
this.router.events.pipe(
|
|
filter((event): event is NavigationEnd => event instanceof NavigationEnd)
|
|
).subscribe(event => {
|
|
// Check if the event's URL matches the current page's URL
|
|
if (event.urlAfterRedirects === '/identitas') {
|
|
this.refreshData();
|
|
}
|
|
});
|
|
}
|
|
|
|
async ngOnInit() {
|
|
await this.storage.create();
|
|
this.refreshData();
|
|
}
|
|
async refreshData() {
|
|
const id_identitas = await this.storage.get('id_identitas');
|
|
console.log(id_identitas, "id identitas")
|
|
this.idIdentitas = await this.storage.get('id_identitas');
|
|
this.isEdit = !!id_identitas;
|
|
if (id_identitas) {
|
|
this.identitasService.getIdentitasById(id_identitas).subscribe({
|
|
next: (response) => {
|
|
if (response.status === 'success' && response.data) {
|
|
this.populateUserData(response.data);
|
|
} else {
|
|
this.clearUserData();
|
|
this.showToast('No data found for this ID', 'middle', 'warning');
|
|
}
|
|
},
|
|
error: async (err) => {
|
|
await this.showToast(`Error fetching data: ${err.message}`, 'top', 'danger');
|
|
}
|
|
});
|
|
} else {
|
|
this.clearUserData();
|
|
}
|
|
}
|
|
populateUserData(data: any) {
|
|
this.userData = {
|
|
...data,
|
|
tanggal_lahir: this.formatDate(data.tanggal_lahir),
|
|
tanggal_pertama: this.formatDate(data.tanggal_pertama)
|
|
};
|
|
console.log(this.userData)
|
|
}
|
|
formatDate(isoDate: string): string {
|
|
return isoDate.split('T')[0];
|
|
}
|
|
|
|
clearUserData() {
|
|
this.userData = {
|
|
nama: '',
|
|
tempat_lahir: '',
|
|
tanggal_lahir: '',
|
|
jenis_kelamin: '',
|
|
tinggi_badan: null,
|
|
berat_badan: null,
|
|
suku: '',
|
|
riwayat_perokok: '',
|
|
pekerjaan: '',
|
|
nomor_hp: '',
|
|
status_pernikahan: '',
|
|
alamat: '',
|
|
tanggal_pertama: ''
|
|
};
|
|
}
|
|
async showToast(message: string, position: 'top' | 'middle' | 'bottom', color: string) {
|
|
const toast = await this.toastController.create({
|
|
message: message,
|
|
duration: 2000,
|
|
position: position,
|
|
color: color
|
|
});
|
|
toast.present();
|
|
}
|
|
async simpanData() {
|
|
if (this.isEdit && this.idIdentitas) {
|
|
this.identitasService.updateIdentitas(this.idIdentitas, this.userData).subscribe(
|
|
response => this.handleResponse(response),
|
|
error => this.handleError(error)
|
|
);
|
|
} else {
|
|
this.identitasService.simpanIdentitas(this.userData).subscribe(
|
|
async (response) => {
|
|
if (!this.isEdit) {
|
|
await this.storage.set('id_identitas', response.id || response.data.id);
|
|
this.idIdentitas = response.id || response.data.id; // Update local idIdentitas
|
|
this.isEdit = true;
|
|
}
|
|
this.handleResponse(response);
|
|
},
|
|
error => this.handleError(error)
|
|
);
|
|
}
|
|
}
|
|
|
|
handleResponse(response: any) {
|
|
this.showToast('Data successfully saved!', 'top', 'success');
|
|
}
|
|
|
|
handleError(error: { message: any; }) {
|
|
this.showToast(`Error saving data: ${error.message}`, 'top', 'danger');
|
|
}
|
|
getIdentitas(){
|
|
this.router.navigate(['/get-identitas']);
|
|
}
|
|
|
|
}
|