Advertisement
Guest User

Untitled

a guest
Mar 30th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. import { IDeal } from './../../interfaces/IDeal';
  2. import { SocialSharing } from '@ionic-native/social-sharing/ngx';
  3. import { DealService } from '../../services/deal.service';
  4. import { Component, OnInit , ViewChild } from '@angular/core';
  5. import { Clipboard } from '@ionic-native/clipboard/ngx';
  6. import { IonInfiniteScroll, IonContent, Platform } from '@ionic/angular';
  7. import { AlertController, IonSearchbar } from '@ionic/angular';
  8. import * as moment from 'moment';
  9. import { Keyboard } from '@ionic-native/keyboard/ngx';
  10. import { IImagesSlides } from '../../interfaces/IImageSlides';
  11. import { Storage } from '@ionic/storage';
  12. import { Plugins } from '@capacitor/core';
  13. const { SplashScreen } = Plugins;
  14.  
  15. @Component({
  16. selector: 'app-deals-list',
  17. templateUrl: './deals-list.page.html',
  18. styleUrls: ['./deals-list.page.scss'],
  19.  
  20. })
  21. export class DealsListPage implements OnInit {
  22. showNotFound = false;
  23. public today : string = moment().format('YYYY-MM-DD');
  24. public totalDeals : IDeal[];
  25. public dealsListAll : IDeal[];
  26. public dealsListAllNoChange : IDeal[];
  27. public dealsListOnlyDiscountCode : IDeal[];
  28. public dealsListOnlyOffer : IDeal[];
  29. public dealsListOnly20: IDeal[];
  30. public dealsListOnly20_50 : IDeal[];
  31. public dealsListOnly50_100 : IDeal[];
  32. public dealsListOnly100 : IDeal[];
  33. public dealsMinimiStorici : IDeal[];
  34. public deals : IDeal[] = [];
  35. public deals_copy : IDeal[];
  36. public i = 0;
  37. public page = 1;
  38. public isShow: boolean = false;
  39. public isShowBK: boolean = false;
  40. public flagSearch : boolean = false;
  41. public filterNumber : number = 0;
  42. imageSlides : IImagesSlides[];
  43. @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  44. @ViewChild(IonContent) ionContent: IonContent;
  45. @ViewChild('mySearchbar') searchbar: IonSearchbar;
  46. slideOptsOne = {
  47. initialSlide: 0,
  48. loop: true,
  49. //slidesPerView: 1,
  50. speed: 1000,
  51. autoplay:true
  52. };
  53. ngOnInit() {
  54. }
  55.  
  56. constructor(private ds: DealService, private clipboard: Clipboard, private socialShareing : SocialSharing, private platform : Platform,private alertController: AlertController,
  57. private keyboard: Keyboard, private storage: Storage) {
  58.  
  59. this.ds.imagesListForSlides$().subscribe( (imagesSlieds) => {
  60. this.imageSlides = imagesSlieds as IImagesSlides[];
  61. });
  62. this.loadDeals();
  63.  
  64. this.platform.backButton.subscribe( () => {
  65. this.ionContent.scrollToTop(500);
  66. }).unsubscribe;
  67.  
  68. this.platform.pause.subscribe(async () => {
  69. this.storage.set('lastDatePause', Date.now());
  70. });
  71.  
  72. this.platform.resume.subscribe(async () => {
  73. this.storage.get('lastDatePause').then((date) => {
  74. if (Date.now() - date >= 36000000) { // 10*60*60*1000
  75. SplashScreen.show({
  76. autoHide: true
  77. });
  78. location.reload();
  79. }
  80. })
  81. });
  82. }
  83. loadDeals(){
  84. //this.ds.dealsListFromDB$().subscribe( (deals) => {
  85. this.ds.dealsList$.subscribe((deals)=> {
  86. this.totalDeals = deals as IDeal[];
  87. this.storage.get("myFavoritesList").then((deals)=> {
  88. let totalFavoritesDeals = deals as IDeal[];
  89. this.ds.syncFavoritesListWithAllDeals(this.totalDeals,totalFavoritesDeals);
  90. });
  91.  
  92. this.dealsListAllNoChange = deals.filter(deal => deal.priorityLevel != 3) as IDeal[];
  93. this.dealsListAll = deals.filter(deal => deal.priorityLevel != 3) as IDeal[];
  94. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  95. this.deals.push(this.dealsListAll[this.i]);
  96. this.i++;
  97. }
  98. this.dealsListOnlyDiscountCode = this.dealsListAll.filter(deal => deal.discountCodeSingle);
  99. this.dealsListOnlyOffer = this.dealsListAll.filter(deal => !deal.discountCodeSingle);
  100. this.dealsListOnly20 = this.dealsListAll.filter(deal => this.formatPriceToNumber(deal.finalPrice,1));
  101. this.dealsListOnly20_50 = this.dealsListAll.filter(deal => this.formatPriceToNumber(deal.finalPrice,2));
  102. this.dealsListOnly50_100 = this.dealsListAll.filter(deal => this.formatPriceToNumber(deal.finalPrice,3));
  103. this.dealsListOnly100 = this.dealsListAll.filter(deal => this.formatPriceToNumber(deal.finalPrice,4));
  104. this.dealsMinimiStorici = this.dealsListAll.filter(deal => deal.priorityLevel.toString() == '2');
  105. this.deals_copy = this.deals;
  106. this.page++;
  107. }
  108. );
  109.  
  110.  
  111. }
  112.  
  113. public getScrollPos(pos: number) {
  114. if(this.deals.length>2){
  115. this.isShow = pos > this.platform.height()/4 ? true : false;
  116. if(this.isShowBK != this.isShow){
  117. this.ds.valueOfFlagScroll(!this.isShow);
  118. this.isShowBK = this.isShow;
  119. }
  120. }
  121. }
  122.  
  123. scrollContent(scroll) {
  124. if (scroll === 'top') {
  125. this.ionContent.scrollToTop(500); //300 for animate the scroll effect.
  126. } else {
  127. this.ionContent.scrollToBottom(); //300 for animate the scroll effect.
  128. }
  129. }
  130.  
  131. toggleInfiniteScroll() {
  132. this.infiniteScroll.disabled = !this.infiniteScroll.disabled;
  133. }
  134.  
  135. formatPriceToNumber(string : String,number : Number) : Boolean {
  136. string = string.replace(".","");
  137. string = string.replace("€","");
  138. let price = Number(string.replace(",","."));
  139. switch(number){
  140. case 1:
  141. return (price <= 20.00);
  142. case 2:
  143. return (price >= 20.00 && price<=50.00);
  144. case 3:
  145. return (price >= 50.00 && price<=100.00);
  146. case 4:
  147. return (price >= 100.00);
  148. }
  149. }
  150.  
  151. loadMore(event){
  152. this.page++;
  153. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  154. this.deals.push(this.dealsListAll[this.i]);
  155. this.i++;
  156. }
  157. this.deals_copy = this.deals;
  158. event.target.complete();
  159. if (this.deals.length == this.dealsListAll.length) {
  160. event.target.disabled = true;
  161. }
  162. }
  163.  
  164. CopyText(coupon : string){
  165. this.clipboard.copy(coupon).then(()=>{
  166. this._showAlert();
  167. }
  168. )
  169. }
  170.  
  171. async _showAlert() {
  172. const alert = await this.alertController.create({
  173. header: '✅CODICE COPIATO',
  174. //subHeader: 'PREFERITI',
  175. message: 'Ricorda di incollare il codice in fase di pagamento!',
  176. buttons: ['OK']
  177. });
  178.  
  179. await alert.present();
  180. }
  181.  
  182. shareDeal(deal : IDeal){
  183. let messaggio = '';
  184. if(deal.textExtra){
  185. if(deal.discountCodeSingle) {
  186. messaggio = 'Ho trovato questo prodotto in offerta sull\'app di GosuMania a soli ' + deal.finalPrice
  187. + '\n' + 'usando il Codice Sconto [ ' + deal.discountCodeSingle + ' ]'
  188. + '\n❗️' + deal.textExtra
  189. + '\n' + deal.productLink5
  190. + '\n\n' + 'Scarica anche tu l\'app � https://gosumania.it/download-app-gosumania';
  191. } else {
  192. messaggio = 'Ho trovato questo prodotto in offerta sull\'app di GosuMania a soli ' + deal.finalPrice
  193. + '\n❗️' + deal.textExtra
  194. + '\n' + deal.productLink5
  195. + '\n\n' + 'Scarica anche tu l\'app � https://gosumania.it/download-app-gosumania';
  196. }
  197. } else {
  198. if(deal.discountCodeSingle) {
  199. messaggio = 'Ho trovato questo prodotto in offerta sull\'app di GosuMania a soli ' + deal.finalPrice
  200. + '\n' + 'usando il Codice Sconto [ ' + deal.discountCodeSingle + ' ]'
  201. + '\n' + deal.productLink5
  202. + '\n\n' + 'Scarica anche tu l\'app � https://gosumania.it/download-app-gosumania';
  203. } else {
  204. messaggio = 'Ho trovato questo prodotto in offerta sull\'app di GosuMania a soli ' + deal.finalPrice
  205. + '\n' + deal.productLink5
  206. + '\n\n' + 'Scarica anche tu l\'app � https://gosumania.it/download-app-gosumania';
  207. }
  208. }
  209. this.socialShareing.share(messaggio).then(()=>{
  210. //alert("Success");
  211. }).catch(e => {
  212. alert("Abilita i permessi nelle impostazioni del tuo Smartphone!");
  213. });
  214. }
  215.  
  216. searchProduct(e){
  217. let value : string = e.detail.value;
  218. if(value.length == 0){
  219. this.showNotFound = false;
  220. this.flagSearch = false;
  221. this.showOnlyDeals(this.filterNumber);
  222. this.ionContent.scrollToTop();
  223. this.ds.valueOfFlagSearch(false);
  224. }else {
  225. this.flagSearch = true;
  226. this.ds.valueOfFlagSearch(true);
  227. this.showOnlyDeals(this.filterNumber);
  228. value = value.toLocaleUpperCase();
  229. this.dealsListAll = this.dealsListAll.filter(deal => deal.productName.toLocaleUpperCase().match(value));
  230. this.deals = [];
  231. this.i = 0;
  232. this.page=1;
  233. if(this.dealsListAll.length === 0) {
  234. this.showNotFound = true;
  235. } else {
  236. this.showNotFound = false;
  237. }
  238. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  239. this.deals.push(this.dealsListAll[this.i]);
  240. this.i++;
  241. }
  242. this.ionContent.scrollToTop();
  243. }
  244. }
  245.  
  246. filterDeal(value : Number){
  247. this.searchbar.value = '';
  248. this.showOnlyDeals(value);
  249. }
  250.  
  251. showOnlyDeals(value: Number){
  252. switch(Number(value)){
  253. case 1:
  254. this.dealsListAll = this.dealsListOnlyDiscountCode;
  255. this.deals = [];
  256. this.i = 0;
  257. this.page=1;
  258. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  259. this.deals.push(this.dealsListAll[this.i]);
  260. this.i++;
  261. }
  262. break;
  263. case 2:
  264. this.dealsListAll = this.dealsListOnlyOffer;
  265. this.deals = [];
  266. this.i = 0;
  267. this.page=1;
  268. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  269. this.deals.push(this.dealsListAll[this.i]);
  270. this.i++;
  271. }
  272. break;
  273. case 3:
  274. this.dealsListAll = this.dealsMinimiStorici;
  275. this.deals = [];
  276. this.i = 0;
  277. this.page=1;
  278. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  279. this.deals.push(this.dealsListAll[this.i]);
  280. this.i++;
  281. }
  282. break;
  283. case 4:
  284. this.dealsListAll = this.dealsListOnly20;
  285. this.deals = [];
  286. this.i = 0;
  287. this.page=1;
  288. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  289. this.deals.push(this.dealsListAll[this.i]);
  290. this.i++;
  291. }
  292. break;
  293. case 5:
  294. this.dealsListAll = this.dealsListOnly20_50;
  295. this.deals = [];
  296. this.i = 0;
  297. this.page=1;
  298. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  299. this.deals.push(this.dealsListAll[this.i]);
  300. this.i++;
  301. }
  302. break;
  303. case 6:
  304. this.dealsListAll = this.dealsListOnly50_100;
  305. this.deals = [];
  306. this.i = 0;
  307. this.page=1;
  308. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  309. this.deals.push(this.dealsListAll[this.i]);
  310. this.i++;
  311. }
  312. break;
  313. case 7:
  314. this.dealsListAll = this.dealsListOnly100;
  315. this.deals = [];
  316. this.i = 0;
  317. this.page=1;
  318. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  319. this.deals.push(this.dealsListAll[this.i]);
  320. this.i++;
  321. }
  322. break;
  323. default:
  324. this.dealsListAll = this.dealsListAllNoChange;
  325. this.deals = [];
  326. this.i = 0;
  327. this.page=1;
  328. while(this.i<this.page*10 && this.i<this.dealsListAll.length){
  329. this.deals.push(this.dealsListAll[this.i]);
  330. this.i++;
  331. }
  332.  
  333. }
  334. this.filterNumber = Number(value);
  335. }
  336.  
  337. doRefresh(event) {
  338. this.ds.dealsListFromDB$().subscribe((deals) => {
  339. this.ds.valueOfDealsList(deals as IDeal[]);
  340. this.filterDeal(this.filterNumber);
  341. event.target.complete();
  342. })
  343. }
  344.  
  345. }
  346.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement