2021-05-24 14:55:30 +02:00

110 lines
2.8 KiB
TypeScript

import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {FeedService} from '../../services/feed.service';
import {IFeed} from '../../interfaces/feed.interface';
import {throwError} from 'rxjs';
import {Tweet} from '../../interfaces/interface';
import {SnackbarService} from '../../services/snackbar.service';
import {environment} from '../../../environments/environment';
import {DialogComponent} from '../dialog/dialog.component';
import {MatDialog} from '@angular/material/dialog';
import {NGXLogger} from 'ngx-logger';
@Component({
selector: 'app-tweets',
templateUrl: './tweets.component.html',
styleUrls: ['./tweets.component.css']
})
export class TweetsComponent implements OnInit {
tweets: Tweet[] = [];
feeds: IFeed[] = [];
constructor(private http: HttpClient,
private _feedService: FeedService,
private _snackbarService: SnackbarService,
private _dialog: MatDialog,
private _logger: NGXLogger) {
this._feedService.getFeeds().subscribe(
(data: any) => {
this.feeds = data;
},
err => {
this.errorDialog();
this._logger.error(err);
return throwError(err);
}
);
}
ngOnInit(): void {
this.fillTweets();
console.log(this.tweets);
}
loadMore() {
console.log(this.tweets);
this.http.get<Tweet[]>(
'http://' + environment.location + ':' + environment.port_be + '/getMoreTweets/' + this.tweets[this.tweets.length - 1].tweet_id + '/'
).subscribe(data => {
console.log(data);
if (data.length === 0) {
this._snackbarService.show('Keine weiteren Tweets vorhanden', 'Schließen');
}
for (const tweet of data) {
const position = tweet.text.search(': http');
tweet.text = tweet.text.substring(0, position);
this.tweets.push(tweet);
}
},
err => {
this.errorDialog();
this._logger.error(err);
return throwError(err);
}
);
}
fillTweets() {
this.http.get<Tweet[]>('http://' + environment.location + ':' + environment.port_be + '/getSixTweets/').subscribe(data => {
console.log(data);
for (const tweet of data) {
const position = tweet.text.search(': http');
tweet.text = tweet.text.substring(0, position);
this.tweets.push(tweet);
}
},
err => {
this.errorDialog();
this._logger.error(err);
return throwError(err);
}
);
}
public errorDialog() {
const dialogRef = this._dialog.open(DialogComponent, {
data: {
title: 'Serverfehler',
body: 'Fehler bei der Kommunikation mit Twitter").',
abort: 'Abbrechen',
confirm: 'OK',
hideAbort: true
}
});
}
}