File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/change-direction/change-direction.ts
import { Component, OnInit } from "@angular/core";
import { TranslateService } from "@ngx-translate/core";
import { ModalController, AlertController } from "@ionic/angular";
import { StorageService } from '../../services/storage.service';
import { Router } from "@angular/router";
import { UserService } from "../../services/user.service";
import { CityService } from "../../services/city.service";
import { SucursalService } from "../../services/sucursal.service";
import { UtilsService } from "../../services/utils.service";
@Component({
selector: "page-change-direction",
templateUrl: "change-direction.html",
styleUrls: ["./change-direction.scss"],
})
export class ChangeDirectionPage implements OnInit {
infoUser: any;
listCities: any;
cityConnection: any;
messageText: any;
listSucursals: any;
sucursalSelected: any;
constructor(
public userProvider: UserService,
public cityProvider: CityService,
private storage: StorageService,
public modalCtrl: ModalController,
public alertCtrl: AlertController,
private translateService: TranslateService,
public sucursalProvider: SucursalService,
public utilsService: UtilsService,
private router: Router
) {
// traduccion textos
this.translateService.get(["TITLE_CONFIRM_CHANGE_LOCATION", "TEXT_CONFIRM_CHANGE_LOCATION"]).subscribe((values) => {
this.messageText = {
titleConfirmChangeLocation: values.TITLE_CONFIRM_CHANGE_LOCATION,
textConfirmChangeLocation: values.TEXT_CONFIRM_CHANGE_LOCATION,
};
});
}
ngOnInit() {
}
ionViewWillEnter() {
this.storage.get("infoUser").then((infoUser) => {
if (infoUser) {
this.infoUser = infoUser;
}
});
this.storage.get("listCities").then((listCities) => {
if (listCities) {
this.listCities = listCities;
}
});
this.storage.get("infoHost").then((infoHost) => {
this.cityConnection = infoHost;
});
this.storage.get("listSucursals").then((listSucursals) => {
if (listSucursals) {
this.listSucursals = listSucursals;
}
});
this.storage.get("sucursalSelected").then((sucursalSelected) => {
if (sucursalSelected) {
this.sucursalSelected = sucursalSelected;
}
});
}
addNewAddress() {
this.cancel(false);
setTimeout(() => {
this.router.navigate(["/address/addressListCreate/true"]);
}, 500);
}
cancel(status, origin = "") {
this.modalCtrl.dismiss({ status, origin });
}
changeAddressUser(id, last_used) {
if (!last_used) {
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(() => {
this.userProvider.updateLastUsedAddress(id);
setTimeout(() => {
this.storage.get("infoUser").then((infoUser) => {
if (infoUser) {
this.infoUser = infoUser;
this.utilsService.dismissLoading();
}
});
}, 500);
});
}
}
async changeCity(city) {
if (this.cityConnection.id != city.id) {
const confirm = await this.alertCtrl.create({
header: this.messageText.titleConfirmChangeLocation,
message: this.messageText.textConfirmChangeLocation,
buttons: [
{
text: "No",
handler: () => {
console.log("Disagree clicked");
},
},
{
text: "Si",
handler: () => {
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(() => {
this.storage.set("infoHost", city);
setTimeout(() => {
this.utilsService.dismissLoading();
this.cityProvider.getInfoHost();
this.cancel(true);
}, 500);
});
},
},
],
});
await confirm.present();
}
}
changeSucursal(sucursal) {
if (this.sucursalSelected.id != sucursal.id) {
this.storage.get("token").then((token) => {
if (token) {
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(async () => {
(await this.sucursalProvider.updateSucursalUser(sucursal.id, token.access_token)).subscribe(
(resp) => {
this.utilsService.dismissLoading();
if (resp["status"] == "success") {
this.storage.set("sucursalSelected", sucursal);
this.sucursalSelected = sucursal;
this.cancel(false, "changeSucursal");
}
},
(error) => {
console.log("changeSucursal: ", error);
this.utilsService.dismissLoading();
}
);
});
}
});
}
}
}