Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { AfterViewInit, Component, Injector, OnDestroy, OnInit, ViewContainerRef } from '@angular/core';
  2. import { User } from '../../model/user.model';
  3. import { ActivatedRoute, Params, Router } from '@angular/router';
  4. import { ToastsManager } from 'ng2-toastr';
  5. import { AppComponent } from '../../app.component';
  6. import { SimpleTimer } from 'ng2-simple-timer';
  7. import { TranslateService } from '@ngx-translate/core';
  8. import { environment } from '../../../environments/environment';
  9. import { JwtService, UserService } from '../../service/index';
  10.  
  11. declare const ga: Function;
  12. @Component({
  13.   selector: 'app-profile',
  14.   templateUrl: './profile.component.html',
  15. })
  16. export class ProfileComponent implements OnInit, OnDestroy, AfterViewInit {
  17.   public userModel: User;
  18.   public steamImageUrl: string = 'https://steamcommunity-a.akamaihd.net/economy/image/';
  19.   private appComponent: any;
  20.   public steamPostImageUrl: string = '/130fx97f/image.png';
  21.   private isSendingProcessed: boolean = false;
  22.   private isMessageShowed: boolean = false;
  23.   public isAuthenticated: boolean;
  24.   public facebookAuthUrl: string = `${environment.api_url}/social/facebook/authorize?token=${this.jwtService.getToken()}`;
  25.  
  26.   constructor(private inj: Injector,
  27.               vcr: ViewContainerRef,
  28.               public toastr: ToastsManager,
  29.               private st: SimpleTimer,
  30.               private userService: UserService,
  31.               private router: Router,
  32.               private jwtService: JwtService,
  33.               private route: ActivatedRoute,
  34.               private translate: TranslateService) {
  35.     this.toastr.setRootViewContainerRef(vcr);
  36.   }
  37.  
  38.   ngOnInit() {
  39.     this.userService.currentUser.subscribe(user => {
  40.       if (user) {
  41.         this.userModel = user;
  42.         this.route.queryParams.subscribe((params: Params) => {
  43.           if (!this.isMessageShowed && Object.keys(params).length) {
  44.             if (!this.userModel.isTwitterClaimed) {
  45.               this.sendTwitterTokens(params);
  46.             }
  47.             if (params.isFacebookClaimed) {
  48.               this.showFreeCreditsMessage(params);
  49.             }
  50.             if (params.isEmailClailmed) {
  51.               this.confirmEmail(params);
  52.             }
  53.             this.isMessageShowed = true;
  54.           }
  55.         });
  56.       }
  57.     });
  58.   }
  59.  
  60.   ngAfterViewInit() {
  61.     setTimeout(() => {
  62.       if (this.userModel && this.userModel.orders) {
  63.         this.userModel.orders.forEach((orderSingle: any) => {
  64.           if (orderSingle.status === 'undefined') {
  65.             orderSingle.timer = 1000 * 60 * 60 - (Date.now() - new Date(orderSingle.createdAt).getTime());
  66.             this.st.newTimer(orderSingle.createdAt, 1);
  67.             orderSingle.timerInstance = this.st.subscribe(orderSingle.createdAt, () => {
  68.               orderSingle.timer -= 1000;
  69.               this.millisToMinutesAndSeconds(orderSingle.timer);
  70.             });
  71.           }
  72.         });
  73.       }
  74.     }, 0);
  75.   }
  76.  
  77.  
  78.   millisToMinutesAndSeconds(millis: number) {
  79.     if (millis <= 1) return '00:00';
  80.     const minutes = Math.floor(millis / 60000);
  81.     const seconds = ((millis % 60000) / 1000).toFixed(0);
  82.     return minutes + ':' + (parseInt(seconds) < 10 ? '0' : '') + seconds;
  83.   }
  84.  
  85.   ngOnDestroy() {
  86.     if (this.userModel && this.userModel.orders)
  87.       this.userModel.orders.forEach((orderSingle: any) => {
  88.         if (orderSingle.timerInstance) {
  89.           this.st.unsubscribe(orderSingle.timerInstance);
  90.           orderSingle.timerInstance = undefined;
  91.         }
  92.       });
  93.   }
  94.  
  95.   displayPaymentDialog(): void {
  96.     ga('send', 'event', 'PROFILE_COMPONENT', 'DIALOG_ADD_FUNDS', 'OPEN');
  97.     if (!this.appComponent)
  98.       this.appComponent = this.inj.get(AppComponent);
  99.     this.appComponent.showAddFunds();
  100.   }
  101.  
  102.   displayNoFundsDialog(): void {
  103.     ga('send', 'event', 'PROFILE_COMPONENT', 'DIALOG_NO_FUNDS', 'OPEN');
  104.     if (!this.appComponent)
  105.       this.appComponent = this.inj.get(AppComponent);
  106.     this.appComponent.showNoFunds();
  107.   }
  108.  
  109.   getProfileUrl(): string {
  110.     return 'https://steamcommunity.com/profiles/' + this.userModel.steamId;
  111.   }
  112.  
  113.   directToMain() {
  114.     this.router.navigateByUrl('/');
  115.   }
  116.  
  117.   saveTradeUrl(tradeUrl: string): void {
  118.     ga('send', 'event', 'PROFILE_COMPONENT', 'SAVE_TRADE_URL', 'CLICK');
  119.     this.userService.updateTradeUrl(tradeUrl).subscribe(response => {
  120.       if (response.status == 202) {
  121.         this.translate.get(['profile_msg_saved', 'profile_msg_success']).subscribe(str => {
  122.           this.toastr.success(str.profile_msg_saved, str.profile_msg_success);
  123.         });
  124.       } else {
  125.         this.translate.get(['profile_msg_invalid_url', 'profile_msg_failed']).subscribe(str => {
  126.           this.toastr.error(str.profile_msg_invalid_url, str.profile_msg_failed);
  127.         });
  128.       }
  129.     },                                                  e => {
  130.       this.translate.get(['profile_msg_invalid_url', 'profile_msg_failed']).subscribe(str => {
  131.         this.toastr.error(str.profile_msg_invalid_url, str.profile_msg_failed);
  132.       });
  133.     });
  134.   }
  135.  
  136.   performSendItem(item: any): void {
  137.     ga('send', 'event', 'PROFILE_COMPONENT', 'WITHDRAW_ITEM', 'CLICK');
  138.     this.isSendingProcessed = true;
  139.     this.userService.sendItem(item.id)
  140.       .subscribe(response => {
  141.         this.isSendingProcessed = false;
  142.         switch (response.status) {
  143.           case 200:
  144.             item.status = 'processed';
  145.             this.translate.get(['profile_msg_check_offers', 'profile_msg_success']).subscribe(str => {
  146.               this.toastr.success(str.profile_msg_check_offers, str.profile_msg_success);
  147.             });
  148.             break;
  149.           case 406:
  150.             this.translate.get(['profile_msg_define_url', 'profile_msg_failed']).subscribe(str => {
  151.               this.toastr.error(str.profile_msg_define_url, str.profile_msg_failed);
  152.             });
  153.             break;
  154.           case 429:
  155.             this.translate.get(['profile_msg_have_offer', 'profile_msg_failed']).subscribe(str => {
  156.               this.toastr.error(str.profile_msg_have_offer, str.profile_msg_failed);
  157.             });
  158.             break;
  159.           case 404:
  160.             this.translate.get(['profile_msg_no_item', 'profile_msg_failed']).subscribe(str => {
  161.               this.toastr.error(str.profile_msg_no_item, str.profile_msg_failed);
  162.             });
  163.             break;
  164.           case 402:
  165.             this.translate.get(['profile_msg_make_deposit', 'profile_msg_failed']).subscribe(str => {
  166.               this.toastr.error(str.profile_msg_make_deposit, str.profile_msg_failed);
  167.             });
  168.             this.displayNoFundsDialog();
  169.             break;
  170.           case 403:
  171.             this.translate.get(['withdraw_modal_condition', 'profile_msg_failed']).subscribe(str => {
  172.               this.toastr.error(str.withdraw_modal_condition, str.profile_msg_failed);
  173.             });
  174.             this.displayNoFundsDialog();
  175.             break;
  176.           case 451:
  177.             this.translate.get(['profile_msg_no_game', 'profile_msg_failed']).subscribe(str => {
  178.               this.toastr.error(str.profile_msg_no_game, str.profile_msg_failed);
  179.             });
  180.             break;
  181.           default:
  182.             this.translate.get(['profile_msg_failed_send', 'profile_msg_failed']).subscribe(str => {
  183.               this.toastr.error(str.profile_msg_failed_send, str.profile_msg_failed);
  184.             });
  185.         }
  186.       }, e => {
  187.         this.isSendingProcessed = false;
  188.         this.translate.get(['profile_msg_failed_send', 'profile_msg_failed']).subscribe(str => {
  189.           this.toastr.error(str.profile_msg_failed_send, str.profile_msg_failed);
  190.         });
  191.       });
  192.   }
  193.  
  194.   performSellItem(item: any): void {
  195.     ga('send', 'event', 'PROFILE_COMPONENT', 'SELL_ITEM', 'CLICK');
  196.  
  197.     let itemsToSell: Array<any> = [];
  198.     itemsToSell.push(item.id);
  199.  
  200.     this.userService.sellItem(itemsToSell)
  201.       .subscribe(response => {
  202.         if (response.status == 202) {
  203.           item.status = 'sold';
  204.           this.userModel.balance = response.result.balance;
  205.           this.translate.get(['profile_msg_item_sold', 'profile_msg_success']).subscribe(str => {
  206.             this.toastr.success(str.profile_msg_item_sold, str.profile_msg_success);
  207.           });
  208.         } else {
  209.           this.translate.get(['prize_modal_failed_sell', 'profile_msg_failed']).subscribe(str => {
  210.             this.toastr.error(str.prize_modal_failed_sell, str.profile_msg_failed);
  211.           });
  212.         }
  213.       }, e => {
  214.         this.translate.get(['prize_modal_failed_sell', 'profile_msg_failed']).subscribe(str => {
  215.           this.toastr.error(str.prize_modal_failed_sell, str.profile_msg_failed);
  216.         });
  217.       });
  218.   }
  219.  
  220.   checkFacebookAccount() {
  221.     return this.userService.checkFacebookAccount()
  222.       .subscribe(response => {
  223.         if (response.result) {
  224.           window.location.href = response.result;
  225.         }
  226.       });
  227.   }
  228.  
  229.   sendTwitterTokens(params) {
  230.     const twitterOauthToken = params.oauth_token;
  231.     const twitterOauthVerifier = params.oauth_verifier;
  232.     if (twitterOauthToken && twitterOauthVerifier) {
  233.       return this.userService.sendTwitterTokens(twitterOauthToken, twitterOauthVerifier)
  234.         .subscribe(response => {
  235.           this.showFreeCreditsMessage(response);
  236.         });
  237.     }
  238.   }
  239.  
  240.   confirmEmail(params) {
  241.     this.userService.confirmEmail()
  242.       .subscribe(response => {
  243.         this.showFreeCreditsMessage(params);
  244.       });
  245.   }
  246.  
  247.   showTwitterConfirmation() {
  248.     ga('send', 'event', 'PROFILE', 'TWITTER_CONFIRM', 'OPEN');
  249.     if (!this.appComponent) {
  250.       this.appComponent = this.inj.get(AppComponent);
  251.     }
  252.     this.appComponent.showTwitterConfirmation();
  253.   }
  254.  
  255.   showEmailConfirmation() {
  256.     ga('send', 'event', 'PROFILE', 'EMAIL_CONFIRM', 'OPEN');
  257.     if (!this.appComponent) {
  258.       this.appComponent = this.inj.get(AppComponent);
  259.     }
  260.     this.appComponent.showEmailConfirmation();
  261.   }
  262.  
  263.   showFreeCreditsMessage(response) {
  264.     if (response.isFollower) {
  265.       this.userService.populate();
  266.       this.translate.get(['profile_msg_free_credits_added', 'profile_msg_success']).subscribe(str => {
  267.         this.toastr.success(str.profile_msg_free_credits_added, str.profile_msg_success);
  268.       });
  269.     } else {
  270.       this.translate.get(['profile_msg_fail_subscribe', 'profile_msg_failed']).subscribe(str => {
  271.         this.toastr.error(str.profile_msg_fail_subscribe, str.profile_msg_failed);
  272.       });
  273.     }
  274.   }
  275.  
  276.   onMouseWinItem( order: any )
  277.   {
  278.     if( order.caseName!="giveaway" ) {
  279.       order.onItemTarget=true;
  280.     }
  281.   }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement