diff --git a/src/pages/thread/thread.ts b/src/pages/thread/thread.ts index f3cb0f4..8969f02 100644 --- a/src/pages/thread/thread.ts +++ b/src/pages/thread/thread.ts @@ -1,8 +1,8 @@ import { Component } from '@angular/core'; -import { NavController, NavParams, ModalController } from 'ionic-angular'; +import { NavController, NavParams, ModalController, ToastController } from 'ionic-angular'; import { NewPostModal } from '../stream/stream'; -// import * as pnut from 'pnut-butter'; +import * as pnut from 'pnut-butter'; /** * Generated class for the ThreadPage page. @@ -19,7 +19,8 @@ export class ThreadPage { title: string; posts: Array = []; - constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) { + constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController, + public toastCtrl: ToastController) { this.posts = this.navParams.data.posts; } @@ -34,4 +35,66 @@ export class ThreadPage { newPostModal.present(); } + bookmark(postid, bookmarked) { + if (bookmarked) { + pnut.deleteBookmark(postid).then(res => { + console.log(res); + this.updatePost(res.data.id); + this.presentToast("Bookmark updated."); + }).catch(err => { + console.log(err); + }); + } else { + pnut.bookmark(postid).then(res => { + console.log(res); + this.updatePost(res.data.id); + this.presentToast("Bookmark updated."); + }).catch(err => { + console.log(err); + }); + } + } + + repost(postid, reposted) { + if (reposted) { + pnut.deleteRepost(postid).then(res => { + console.log(res); + this.updatePost(res.data.id); + this.presentToast("Repost updated."); + }).catch(err => { + console.log(err); + }); + } else { + pnut.repost(postid).then(res => { + console.log(res); + this.updatePost(res.data.id); + this.presentToast("Repost updated."); + }).catch(err => { + console.log(err); + }); + } + } + + updatePost(postid) { + pnut.post(postid, {include_raw: 1}).then(res => { + for (var i = 0; i < this.posts.length; i++) { + if (this.posts[i]['id'] === postid) { + this.posts[i] = res.data; + break; + } + } + }).catch(err => { + console.log(err); + }); + } + + presentToast(text) { + let toast = this.toastCtrl.create({ + position: 'top', + message: text, + duration: 2000 + }); + toast.present(); + } + }