Advertisement
willianlouzefonseca

Untitled

May 17th, 2019
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit} from '@angular/core';
  2. import axios from 'axios';
  3. import swal from 'sweetalert';
  4. import { environment } from 'src/environments/environment.prod';
  5. import {CategoryService} from '../../services/category.service';
  6. @Component({
  7.   selector: 'foot-play-home-athlete',
  8.   templateUrl: './home-athlete.page.html'
  9. })
  10. export class HomeAthletePage implements OnInit {
  11.   private homeFeed: any[] = [];
  12.   public modalLikes = false;
  13.   public totalPages = 0;
  14.   public currentPage = 0;
  15.   category: string;
  16.   comments: any[] = [];
  17.   constructor(private categoryService: CategoryService) { }
  18.  
  19.   ngOnInit() {
  20.     this.categoryService.currentCategory.subscribe(category => this.category = category);
  21.     console.log(this.category);
  22.     this.fetchData(0, this.category);
  23.   }
  24.  
  25.   handleModalLikes() {
  26.     this.modalLikes = !this.modalLikes;
  27.   }
  28.  
  29.   fetchData(page: number = 0, category: string = null) {
  30.     if (category === null) {
  31.       axios.get(`${environment.apiUrl}/api/player/post?linesPerPage=2&page=${page}`, {
  32.         headers: {
  33.           Authorization: localStorage.getItem('_user_token')
  34.         }
  35.       }).then((response) => {
  36.         this.homeFeed = this.homeFeed.concat(response.data.content);
  37.         this.totalPages = response.data.totalPages;
  38.         for (let i = 0; i < this.homeFeed.length; i++) {
  39.           const feed = this.homeFeed[i];
  40.           for (let j = 0; j < feed.comments.length; j++) {
  41.             const comment = feed.comments[j];
  42.             if (typeof comment !== 'string') {
  43.               this.homeFeed[i].comments[j] = JSON.stringify(comment);
  44.             }
  45.           }
  46.         }
  47.       }).catch((error) => {
  48.           swal('Erro', 'Não foi possível carregar o feed', 'error');
  49.       });
  50.     } else {
  51.       axios.get(`${environment.apiUrl}/api/player/post/categoria/${category}?linesPerPage=2&page=${page}`, {
  52.         headers: {
  53.           Authorization: localStorage.getItem('_user_token')
  54.         }
  55.       }).then((response) => {
  56.         this.homeFeed = this.homeFeed.concat(response.data.content);
  57.         console.log('reposta'+this.homeFeed)
  58.         this.totalPages = response.data.totalPages;
  59.         for (let i = 0; i < this.homeFeed.length; i++) {
  60.           const feed = this.homeFeed[i];
  61.           for (let j = 0; j < feed.comments.length; j++) {
  62.             const comment = feed.comments[j];
  63.             if (typeof comment !== 'string') {
  64.               this.homeFeed[i].comments[j] = JSON.stringify(comment);
  65.             }
  66.           }
  67.         }
  68.       }).catch((error) => {
  69.         swal('Erro', ' Não foi possível carregar o feed', 'error');
  70.         console.log(error.response);
  71.       });
  72.     }
  73.   }
  74.  
  75.   onScroll() {
  76.     if (this.currentPage < this.totalPages) {
  77.       this.currentPage++;
  78.       this.fetchData(this.currentPage, this.category);
  79.     }
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement