Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit} from '@angular/core';
- import axios from 'axios';
- import swal from 'sweetalert';
- import { environment } from 'src/environments/environment.prod';
- import {CategoryService} from '../../services/category.service';
- @Component({
- selector: 'foot-play-home-athlete',
- templateUrl: './home-athlete.page.html'
- })
- export class HomeAthletePage implements OnInit {
- private homeFeed: any[] = [];
- public modalLikes = false;
- public totalPages = 0;
- public currentPage = 0;
- category: string;
- comments: any[] = [];
- constructor(private categoryService: CategoryService) { }
- ngOnInit() {
- this.categoryService.currentCategory.subscribe(category => this.category = category);
- console.log(this.category);
- this.fetchData(0, this.category);
- }
- handleModalLikes() {
- this.modalLikes = !this.modalLikes;
- }
- fetchData(page: number = 0, category: string = null) {
- if (category === null) {
- axios.get(`${environment.apiUrl}/api/player/post?linesPerPage=2&page=${page}`, {
- headers: {
- Authorization: localStorage.getItem('_user_token')
- }
- }).then((response) => {
- this.homeFeed = this.homeFeed.concat(response.data.content);
- this.totalPages = response.data.totalPages;
- for (let i = 0; i < this.homeFeed.length; i++) {
- const feed = this.homeFeed[i];
- for (let j = 0; j < feed.comments.length; j++) {
- const comment = feed.comments[j];
- if (typeof comment !== 'string') {
- this.homeFeed[i].comments[j] = JSON.stringify(comment);
- }
- }
- }
- }).catch((error) => {
- swal('Erro', 'Não foi possível carregar o feed', 'error');
- });
- } else {
- axios.get(`${environment.apiUrl}/api/player/post/categoria/${category}?linesPerPage=2&page=${page}`, {
- headers: {
- Authorization: localStorage.getItem('_user_token')
- }
- }).then((response) => {
- this.homeFeed = this.homeFeed.concat(response.data.content);
- console.log('reposta'+this.homeFeed)
- this.totalPages = response.data.totalPages;
- for (let i = 0; i < this.homeFeed.length; i++) {
- const feed = this.homeFeed[i];
- for (let j = 0; j < feed.comments.length; j++) {
- const comment = feed.comments[j];
- if (typeof comment !== 'string') {
- this.homeFeed[i].comments[j] = JSON.stringify(comment);
- }
- }
- }
- }).catch((error) => {
- swal('Erro', ' Não foi possível carregar o feed', 'error');
- console.log(error.response);
- });
- }
- }
- onScroll() {
- if (this.currentPage < this.totalPages) {
- this.currentPage++;
- this.fetchData(this.currentPage, this.category);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement