File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/address-list/address-list.ts
import { Component, OnInit } from "@angular/core";
import { TranslateService } from "@ngx-translate/core";
import { LoadingController, AlertController } from "@ionic/angular";
import { NavigationStart, Router } from "@angular/router";
import { AddressService } from "../../services/address.service";
import { UserService } from "../../services/user.service";
import { UtilsService } from "../../services/utils.service";
import { StorageService } from '../../services/storage.service';
@Component({
selector: "page-address-list",
templateUrl: "address-list.html",
styleUrls: ["./address-list.scss"],
})
export class AddressListPage implements OnInit {
toastMessageText: any;
constructor(
private storage: StorageService,
private translateService: TranslateService,
public loadingCtrl: LoadingController,
public alertCtrl: AlertController,
public addressProvider: AddressService,
public userProvider: UserService,
public utilsService: UtilsService,
private router: Router
) {
this.translateService.get(["TITLE_ALERT_DELETE_ADDRESS", "TEXT_ALERT_DELETE_ADDRESS"]).subscribe((values) => {
this.toastMessageText = {
titleDelete: values.TITLE_ALERT_DELETE_ADDRESS,
textDelete: values.TEXT_ALERT_DELETE_ADDRESS,
};
});
router.events.subscribe((event: NavigationStart) => {
if (event.navigationTrigger === 'popstate' && event.url.includes('address-list')) {
setTimeout(() => {
this.updateInfoUser();
}, 100);
}
});
}
ngOnInit() {
}
ionViewDidEnter() {
this.updateInfoUser();
}
addNewAddress() {
this.router.navigate(["/address/addressListCreate/true"]);
}
async deleteAddress(address_id) {
const confirm = await this.alertCtrl.create({
header: this.toastMessageText.titleDelete,
message: this.toastMessageText.textDelete,
buttons: [
{
text: "No",
handler: () => {
console.log("Disagree clicked");
},
},
{
text: "Si",
handler: () => {
this.storage.get("token").then(async (token) => {
if (token) {
this.utilsService.presentLoading(this.translateService.instant("all.loading"));
(await this.addressProvider.deleteAddress(address_id, token.access_token)).subscribe(
(resp) => {
if (resp["status"] == "success") {
this.storage.get("infoUser").then((res) => {
let dataUser = res;
dataUser.addresses = resp["addresses"];
// se actualiza la informacion del usuario en el localstorage
this.storage.set("infoUser", dataUser);
this.updateInfoUser();
this.utilsService.dismissLoading();
});
}
},
(error) => {
console.log("error generateOrder", error);
this.utilsService.dismissLoading();
}
);
}
});
},
},
],
});
confirm.present();
}
updateInfoUser() {
setTimeout(() => {
this.userProvider.getInfoUser();
}, 500);
}
async changeAddressUser(id) {
await this.userProvider.updateLastUsedAddress(id);
this.updateInfoUser();
}
}