File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/pages/welcome/welcome.ts
import { AnalyticsService } from './../../services/analytics.service';
import { Component, OnInit } from "@angular/core";
import { NavController, Config, AlertController } from "@ionic/angular";
import { StorageService } from '../../services/storage.service';
import { Router } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { UserService } from "../../services/user.service";
import { InitialService } from "../../services/initial.service";
import { AuthService } from "../../services/auth.service";
import { SucursalService } from "../../services/sucursal.service";
import { UtilsService } from "../../services/utils.service";
import { ApiService } from "../../services/api.service";
import { CityService } from "../../services/city.service";
import { interval, Observable } from "rxjs";
import { startWith, take, map } from "rxjs/operators";
import { slider } from './slide.animation';
@Component({
selector: "page-welcome",
templateUrl: "welcome.html",
styleUrls: ["./welcome.scss"],
animations: [slider]
})
export class WelcomePage implements OnInit {
// Our translated text strings
private welcomeString: string;
welcomeLogoList: { path: string }[] = []
tagsUserOneSignal: any;
statusEndVersionHome: boolean = false;
_ios: any;
messageText: any;
public carouselTileItems$: Observable<number[]>;
tempData = [];
carouselBanner = {
grid: { xs: 1, sm: 1, md: 1, lg: 1, xl: 1, all: 0 },
slide: 1,
speed: 400,
interval: {
timing: 3000,
initialDelay: 1000
},
point: {
visible: false
},
load: 2,
loop: true,
touch: true
};
slideOpts = {
initialSlide: 0,
speed: 400,
autoplay: {
delay: 5000,
},
};
constructor(
public navCtrl: NavController,
public translateService: TranslateService,
public userProvider: UserService,
private storage: StorageService,
public auth: AuthService,
public initialProvider: InitialService,
public api: ApiService,
public sucursalProvider: SucursalService,
private cityService: CityService,
public utilsService: UtilsService,
public alertCtrl: AlertController,
private router: Router,
private config: Config,
private analyticsService: AnalyticsService
) {
this._ios = this.config.get("mode") === `ios`;
// traduccion para toast Bienvenida
this.welcomeString = this.translateService.instant("WELCOME_TITLE");
this.translateService.get(["ALERT_PRIVACY_POLICY", "PRIVACY_POLICY_MESSAGE"]).subscribe((values) => {
this.messageText = {
titleLogout: values.ALERT_PRIVACY_POLICY,
textLogout: values.PRIVACY_POLICY_MESSAGE,
};
});
this.storage.get("infoEndVersion").then((infoEndVersion) => {
if (infoEndVersion) {
if (infoEndVersion.end_version_home && infoEndVersion.end_version_home != null) {
this.statusEndVersionHome = true;
} else {
this.statusEndVersionHome = false;
}
}
});
this.loadWelcomeLogo()
}
ngOnInit(): void {
this.carouselTileItems$ = interval(500).pipe(
startWith(-1),
take(30),
map(val => {
const data = (this.tempData = [
...this.tempData,
this.welcomeLogoList[Math.floor(Math.random() * this.welcomeLogoList.length)]
]);
return data;
})
);
this.initialProvider.getCorporateIdentity();
}
ionViewDidLoad() { }
login() {
this.analyticsService.logEvent('Login Welcome Click', this.utilsService.categoryWelcome);
this.router.navigate(["/login"]);
}
signup() {
this.analyticsService.logEvent('Signup Welcome Click', this.utilsService.categoryWelcome);
this.router.navigate(["/signup"]);
}
goToLoginFacebook() {
// this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(async () => {
// const FACEBOOK_PERMISSIONS = [
// 'email',
// 'user_birthday',
// 'user_photos',
// 'user_gender',
// ];
// const result = await (<FacebookLoginResponse><unknown>(
// FacebookLogin.login({ permissions: FACEBOOK_PERMISSIONS })
// ));
// if (result.accessToken) {
// this.storage.set("authResponseFacebook", result);
// this.getFacebookUserDetail(result);
// }
// });
}
getFacebookUserDetail(authResponse) {
this.storage.get("infoHost").then((infoHost) => {
if (infoHost) {
this.userProvider.facebookAuthMain(authResponse.accessToken, infoHost.name_database).subscribe(
(resp) => {
if (resp["status"]) {
this.userProvider.facebookAuth(authResponse.accessToken).subscribe(
(respAuthHost) => {
if (respAuthHost["status"]) {
// se guarda la informacion del usuario en el localstorage
let infoUser = respAuthHost["userInfo"];
this.storage.set("infoUser", infoUser);
this.storage.set("isSubscriber", respAuthHost["isSubscriber"]);
setTimeout(() => {
this.userProvider.getInfoUser();
}, 300);
// se guarda token del usuario en el localstorage
this.storage.set("token", {
access_token: respAuthHost["token"].access_token,
token_refresh: respAuthHost["token"].refresh_token,
});
this.userProvider.savePlayerOnesignal();
this.userProvider.registerDiscountShipments(respAuthHost["token"].access_token);
} else {
this.utilsService.dismissLoading();
this.utilsService.presentAlertInfo("¡Alerta!", "", respAuthHost["m"]);
}
},
(error) => {
this.utilsService.dismissLoading();
console.log("error facebookAuth: ", error);
this.utilsService.presentAlertInfo("¡Alerta!", "", this.translateService.instant("all.error_request"));
}
);
} else {
this.utilsService.dismissLoading();
this.utilsService.presentAlertInfo("¡Alerta!", "", resp["m"]);
}
},
(error) => {
console.log("error facebookAuthMain", error);
this.utilsService.dismissLoading();
this.utilsService.presentAlertInfo("¡Alerta!", "", this.translateService.instant("all.error_request"));
}
);
}
});
}
continueRoute(respAuthHost) {
this.navCtrl.navigateRoot("/app/tabs/home");
this.utilsService.presentToast(3000, "tertiary", "top", this.welcomeString + respAuthHost["userInfo"].first_name);
}
loginInvited() {
this.analyticsService.logEvent('Login Invited Click', this.utilsService.categoryWelcome);
this.utilsService.presentLoading(this.translateService.instant("all.loading")).then(() => {
this.storage.get("infoHost").then((infoHost) => {
if (infoHost) {
this.userProvider.loginAuthMain("invitado@invitado.com.co", infoHost.name_database).subscribe((validate) => {
if (validate["status"] == "success") {
this.auth
.getAccessToken("invitado@invitado.com.co", "123456")
.then((token) => {
if (token["m"] != null) {
this.utilsService.presentToast(3000, "warning", "top", token["m"]);
this.utilsService.dismissLoading();
return;
}
this.storage.set("token", {
access_token: token["access_token"],
token_refresh: token["refresh_token"],
});
this.userProvider
.infoUser("invitado@invitado.com.co", "123456", token["access_token"])
.then(
(res) => {
this.utilsService._HAS_LOGGED_IN = true;
this.storage.set("HAS_LOGGED_IN", true);
this.utilsService.dismissLoading();
// se guarda la sucursal en la informacion del usuario para la logica de productos por sucursal
this.storage.get("sucursalSelected").then(async (sucursalSelected) => {
if (sucursalSelected) {
(await this.sucursalProvider.updateSucursalUser(sucursalSelected.id, token["access_token"])).subscribe(
() => {
this.continueRouteInvited(res);
},
(error) => {
console.log(error);
}
);
} else {
this.continueRouteInvited(res);
}
});
},
(error) => {
console.log("error getInfoUser login", error);
this.utilsService.dismissLoading();
}
)
.catch((error) => {
this.utilsService.dismissLoading();
console.log("error infoUser login", error);
});
})
.catch((error) => {
this.utilsService.dismissLoading();
console.log("error login invitado: ", error);
});
}
});
}
});
});
}
continueRouteInvited(res) {
if (res["status"] == "success") {
this.navCtrl.navigateRoot("/app/tabs/home");
this.utilsService.presentToast(3000, "tertiary", "top", this.welcomeString + res["user"].first_name);
}
}
returnListCities() {
this.analyticsService.logEvent('List Cities Click', this.utilsService.categoryWelcome);
this.router.navigate(["/city-connection"]);
}
async alertPrivacyPolicy(parameter) {
const confirm = await this.alertCtrl.create({
cssClass: "my-custom-class",
header: this.messageText.titleLogout,
message: this.messageText.textLogout,
backdropDismiss: false,
buttons: [
{
text: "Ver términos y condiciones",
handler: () => {
this.openTermsSI();
},
},
{
text: "Cancelar",
cssClass: "secondary",
handler: () => {
confirm.dismiss();
console.log("Confirm Cancel");
this.router.navigate(["/welcome"]);
},
},
{
text: this.translateService.instant("all.accept"),
handler: () => {
confirm.dismiss();
if (parameter == "loginFacebook") {
this.goToLoginFacebook();
console.log("Confirm Okay");
}
},
},
],
});
await confirm.present();
}
openTermsSI() {
this.storage.get("parametersMain").then((parameters) => {
if (parameters) {
let link = parameters.terms.url;
this.utilsService.openLink(link);
}
});
}
async loadWelcomeLogo() {
await this.cityService.getInfoHost();
(await this.api.getFree('welcomeLogo/list')).subscribe((res: any) => {
if (res) {
let logoList
if (Array.isArray(res.welcomeLogoList)) {
logoList = res.welcomeLogoList
} else {
logoList = Object.values(res.welcomeLogoList)
}
if (logoList && logoList.length > 0) {
var index = 0
this.carouselTileItems$ = interval(500).pipe(
startWith(0),
take(logoList.length),
map(val => {
const data = (this.tempData = [
...this.tempData,
this.welcomeLogoList[index]
]);
index++
return data;
})
);
logoList.forEach(element => {
this.welcomeLogoList.push({
path: this.cityService._urlGallery + 'welcome_logo/' + element.image
})
});
}
}
},
(error) => {
console.error("Unable to retrieve welcome logos", error);
setTimeout(() => {
this.loadWelcomeLogo()
}, 1000)
})
}
}